Scripting
You can create pretty complex puzzles with the built in SDK functions, but if you need more complex logic you can write your own C# scripts.
Where your scripts go
Your scripts must be in the Assets/PuzzleScripts folder. The SDK creates it for you when you import it (or click Tools > Big Puzzle SDK > Setup > Set up scripts folder).
- The folder has an assembly definition, so everything inside this folder is compiled into your mod DLL. A script anywhere else in the project is not part of that DLL and will not run in game
- The DLL's name comes from the assembly definition inside the folder. You can rename it, just make sure it doesn't clash with another puzzle's DLL name.
- Changes to scripts need a game restart to show up. Ctrl+F2 reloads scenes and assets, not code.
A first script
using System.Collections.Generic;
using BigPuzzleSDK;
using UnityEngine;
public class AllBoardsCorrect : MonoBehaviour {
[SerializeField] private List<PuzzlePegBoard> boards;
// Wire each Peg Board's On Correct to this.
public void BoardCorrect() {
// Only run this logic if you are host.
if (!PuzzleSDK.IsHost) return;
foreach (PuzzlePegBoard board in boards) {
if (!board.IsCorrect) return;
}
OnSuccess();
}
private void OnSuccess() {
// Do something here, like opening a door or calling PuzzleSDK.Win().
}
}
- Save it as
AllBoardsCorrect.csinAssets/PuzzleScripts(the file name must match the class name). Put the script on an object in your puzzle and drag your Peg Boards into the Boards list in the Inspector, like any Unity script. - On each Peg Board, click + under On Correct, drag in the object that has the script, and pick
AllBoardsCorrect > BoardCorrect. - Every time a board changes to correct,
BoardCorrect()runs and checks all the boards. Only when all of them are correct does it reachOnSuccess(). BoardCorrect()runs for everyone because UnityEvents from the board run for everyone. That's why we return if!PuzzleSDK.IsHost
The PuzzleSDK class
| Member | What it does |
|---|---|
PuzzleSDK.IsHost |
Static. True on the host, and in single player. |
Win() |
Host only. Fires every Confetti and releases every Gourd Vice, then On Win. |
IsWon |
Read-only. The puzzle has been won. |
IsRunning |
Read-only. A Start Switch is on. |
Synced variables
| Type | Holds | Range |
|---|---|---|
SyncedInt |
A whole number | -8,388,608 to 8,388,607 |
SyncedBool |
True or false | |
SyncedEvent |
Nothing: a call that every player runs | argument -128 to 127 |
How they work, in the same four rules as everything else:
- Only the host changes them.
SetandAddare ignored on other PCs, so you can call them from code that runs everywhere. - Everyone reads
Value. Changedruns on every PC when the value changes, the host's too.Changedalso runs for a player who joins later and finds a value different from the starting one.
SyncedInt
using BigPuzzleSDK;
using UnityEngine;
public class Score : MonoBehaviour {
// Make synced variables as fields, like this
private readonly SyncedInt score = new SyncedInt("score");
void Awake() {
score.Changed += value => Debug.Log("score is now " + value);
}
void Update() {
// Calls on other PCs are ignored, so this is safe to run everywhere
if (Input.GetKeyDown(KeyCode.Alpha1)) score.Add(1);
if (Input.GetKeyDown(KeyCode.Alpha2)) score.Add(-1);
}
}
new SyncedInt(name, initialValue = 0)
| Member | What it does |
|---|---|
Value |
The current value. Read-only. |
Changed |
event Action<int>: runs on every PC with the new value. |
Set(int) |
Host only. Kept between -8,388,608 and 8,388,607. |
Add(int) |
Host only. A negative number takes away. |
SyncedBool
private readonly SyncedBool open = new SyncedBool("open");
void Awake() {
open.Changed += isOpen => door.SetActive(!isOpen);
}
new SyncedBool(name, initialValue = false)
| Member | What it does |
|---|---|
Value |
The current value. Read-only. |
Changed |
event Action<bool>: runs on every PC with the new value. |
Set(bool) |
Host only. |
SyncedEvent
A synced event is a function call that every player runs. It doesn't hold a value.
private readonly SyncedEvent flash = new SyncedEvent("flash");
void Awake() {
// Runs on every PC, the host's too
flash.Fired += colorNumber => PlayFlash(colorNumber);
}
public void OnWrongAnswer() {
flash.Fire(2); // host only; ignored elsewhere
}
new SyncedEvent(name)
| Member | What it does |
|---|---|
Fire(int argument = 0) |
Host only. Runs Fired on every PC. The argument is kept between -128 and 127. |
Fired |
event Action<int>: runs on every PC with the argument. |
Nothing is saved. A player who joins later does not run events from before they arrived. If players should find something true when they arrive, use a SyncedInt or SyncedBool.