Custom Conditions
Last updated
Last updated
In the same way as in the case of own shares, two scripts are required to create a proper condition:
A script that will extend the Condition
class (or any of its subclasses), where the definition of the properties and / or comparisons available for the condition will be made, as well as the implementation of the evaluation from which it is obtained the result of the condition.
An Editor script that will extend the ConditionEditor
class (or any of its subclasses) where the interface of the condition that will be shown to the user in the Inspector will be implemented.
To help in the creation of the C# base code for these scripts, GameFlow has a code generation wizard that can be accessed through the menu option Assets > Create > GameFlow > Condition ...
In this window we can decide the namespace, name of the condition and the destination folders of the two scripts to generate once we click on the Create button.
Once the scripts are generated and the compilation is finished, our new condition should appear already available in the action selection window.
Note: GameFlow automatically suppresses the word "Condition" in the names of the conditions to avoid redundancy.
Of course we can add our condition in those actions that support conditions as if it were any other condition available as a series:
The code generated for the condition script would be this:
As we can see, the code is quite similar to the one used to create own shares, only that in this case the method to be implemented would be OnEvaluate()
and the purpose would be to return the result of the evaluation of the condition based on the value of the properties and the comparison that the user has chosen.
Finally, the code for the script corresponding to the Editor part would be this:
We will see that this code is also very similar to the one used for the Editor part of an own action, with the difference that in this case the interface is defined in the method OnConditionGUI()
and we use the method PopupLabel()
to the selection of the comparison.
The API of GameFlow offers a series of subclasses of Condition
that add certain properties and methods of utility, and that can be used to accelerate the writing of own conditions. They are the following:
Condition that makes use of a Value property, normally for comparison purposes.
Just as actions can also be executed in Edit mode, GameFlow can also perform a differentiated evaluation of a condition depending on whether it is running in Play or Edit mode.
In the following example, we will modify the previous script to implement a different (and meaningless, because it is only shown as an example) evaluation that will be performed only in Edit mode:
As we can see, it is about using the SetConditionDelegate<T>()
method within the Init()
method to indicate the method that will take care of the evaluation and implement that method (OnEvaluate()
in the example).
For more information about the operation of the delegation mechanism for evaluating the conditions in Edit mode, consult the entries for the classes Condition and ConditionEditor in the GameFlow reference.