Puzzle System
Search…
CorePuzzleLogic
This is the core class for the Logic elements. This means that the subclasses of this component also have all properties and methods that are present in here. Make use of it.

Default

As soon as you add the component to the game object, you will need to assign a few fields in the inspector view.
The first field is an array of all triggers that are related to the system.
Make sure to manually assign all triggers related to the puzzle to this array, unless you created the system using the editor utility tool.
Two other properties define the auto resets of the system when the puzzle is solved or failed. In other words, whether or not the puzzle needs to reset all triggers and the logic system in these cases.
There are a few public readonly properties that can be accessed from outside of the component/system:
    bool IsSolved -- is the puzzle in the solved state?
    bool IsFailed -- is the puzzle in the failed state?
    bool IsAutoResetSolution -- will the puzzle be auto reset on solved?
    bool IsAutoResetFailure -- will the puzzle be auto reset on failure?
There are a few public methods that you can also makes use of:
1
/// <summary>
2
/// Forces the solve of the puzzle.
3
/// </summary>
4
public void ForceSolve();
5
6
/// <summary>
7
/// Forces the fail of the puzzle.
8
/// </summary>
9
/// <param name="immediate">If set to <c>true</c>, resets the puzzle's elements immediately, regardless of internal implementations.</param>
10
public void ForceFail(bool immediate = true);
11
12
/// <summary>
13
/// Links the triggers for the system.
14
/// </summary>
15
/// <param name="newTriggers">New triggers.</param>
16
public void SetTriggers(CorePuzzleTrigger[] newTriggers);
Copied!

Inheritance

Unlike the CorePuzzleHandler, this class more likely needs to be extended. By default, it has a very simple logic. In order to solve it, the player must just activate all triggers.
By inheriting this class, you will get access to a few useful protected fields:
    CorePuzzleHandler handler -- the reference to the handler entity.
    int completedSteps -- the count of the successfuly completed steps. It might be used for comparing it to a total number of triggers, and if they are equal, the puzzle is solved.
There are multiple methods that can be overridden:
1
/// <summary>
2
/// A standard Unity awake function. By default, initializes variables and triggers.
3
/// </summary>
4
protected virtual void Awake();
5
6
/// <summary>
7
/// Resets the puzzle to the initial state and calls 'UnTrigger' on Triggers, so the player can interact with it again.
8
/// </summary>
9
public virtual void Reset();
10
11
/// <summary>
12
/// Accepts the solution for the puzzle.
13
/// </summary>
14
protected virtual void AcceptSolution();
15
16
/// <summary>
17
/// The method that will be passed through the delegates to all of the triggers.
18
/// It will be invoked whenever the triggers are triggered.
19
/// The id of the respective trigger will be passed.
20
/// </summary>
21
protected virtual void TriggerPuzzle(int id);
Copied!
It is highly recommeded to see what the default logic is present in all given methods.
Note that Reset defines the actions that must be taken to reset the puzzle; such as reset the count of completed steps, reset isSolved field, and more importantly UnTriggers all activated triggers.
As up to TriggerPuzzle, it provides the logic of puzzle, and when it thinks that the puzzle is solved, calls out AcceptSolution. It also may call out the failure method of the Puzzle Handler.
It is not recommended to change AcceptSolution or Awake; or at least, make sure to include the base implementation of both. Overriding these methods without including base implementation may lead to unexpected issues, unless you really know what you are doing.
Example on how to safely override Awake:
1
protected override void Awake()
2
{
3
base.Awake();
4
5
// your custom implementation
6
}
Copied!
Last modified 2yr ago
Copy link