When you need to write more complex conditions or expressions than is possible with just the COGS UI tools, you can use “Computed Conditions” or “Computed Values”.
<aside>
As of COGS 5.7, Computed Values are written in ECMAScript 2023 (a.k.a. JavaScript).
</aside>
<aside>
COGS 5.6 and below uses JEXL, a JavaScript-like expression language. For JEXL documentation click here.
</aside>
<aside>
If you’re migrating an existing project from COGS 5.6 or below see Migrating from JEXL.
</aside>
Anywhere it’s possible to use Computed Values or Computed Conditions, when you switch away from the UI template, the code input box will be populated with the code that is being used under the hood when using the UI tools. This is a good way to get started in understanding how to write Computed Values and Computed Conditions.
For example:
becomes
Version | Expression | |
---|---|---|
5.7 → | pre_show.in_progress |
Pre-show phase is in progress |
5.7 → | scene.variables.score.value * 10 |
The player’s score multiplied by 10 |
5.7 → | !this.value |
Invert on/off value |
There are two main places Computed Conditions and Computed Values can be used and they have slightly different uses:
Computed conditions are in the WHEN of a behaviour, in the condition section of an IF/THEN/OTHERWISE, or in a LOOP. You should write an expression that evaluated to true or false. In the case of a WHEN condition, the behaviour will run when the condition returns true but was previously false.
When writing computed values to display as values (such as in a scene dashboard widget), you should write an expression that returns a value of the correct type. e.g. for a “Text widget” on a dashboard, the expression should return a string type.