Code & Custom UI
back to documentation

Code & Custom UI

Use Function Effects, JavaScript, and custom interfaces when no-code tools are not enough.

Functions

Functions are built-in helpers written as Name(value) or Name(first, second). They make score rules easier to read than a long chain of conditions.

Numeric helpers

Function What it returns Example
Min(a, b) The smaller value Min($N{Score}, 100.0)
Max(a, b) The larger value Max($N{Health}, 0.0)
Round(value) Nearest whole number Round($N{Distance})
Floor(value) Whole number rounded down Floor(3.9)
Ceiling(value) Whole number rounded up Ceiling(3.1)
Abs(value) Distance from zero Abs($N{Temperature})
Sqrt(value) Square root Sqrt(9.0)

Cap a score

This is the safe version of “add one point, but never go above 10”:

SetVariable('Score', Min($N{Score} + 1.0, 10.0), 0.0)

It is appropriate for a local score or a rule that is only triggered by one player. For a shared score that several players can change at once, use the multiplayer update helper described in Multiplayer Functions.

Keep a value in a range

Use Max() and Min() together to keep a value from going below zero or above 100:

SetVariable(
  'Health',
  Min(Max($N{Health} - 5.0, 0.0), 100.0),
  0.0
)

Use a helper in a condition

if(
  Round($N{RaceTimer}) <= 60.0,
  SetTask('FastFinish', 'Completed', 0.0),
  ''
)

Start the timer first with the no-code Start Timer effect. This rule then awards FastFinish only when the rounded time is 60 seconds or less.

Pick a value from a short list

SelectRandom() returns one value from the values you provide:

SelectRandom('Blue', 'Green', 'Orange')

Use it as a value in a deliberate task or variable update. Do not combine random selection with a Function Effect that re-runs on task changes unless repeated picks are what you want.

Tips

  • Put the closing parenthesis of every function call in place before testing.
  • Test a calculation alone before using it inside SetVariable().
  • Use Min() and Max() for limits instead of duplicating a value across several if() branches.

Continue with Portals-specific functions for task and value helpers.