Make a Lua function a visual node
REUSABLE FUNCTIONS
Suppose a designer wants to award fewer points for extra attempts. You can write the rule once in Lua and give the designer two number fields.
function Script.reward(tries, maximum)
return math.max(0, maximum - math.max(0, tries - 1))
endThis fragment belongs in a Lua record. The complete listing is on programming page 20. With three tries and ten possible points, the first try is free and two extra tries cost two points. The result is 8. The outer math.max keeps the score above or equal to zero.
The source name must be reward. Archive Reward is the readable label. The first parameter is tries. Scroll down to add maximum as the second parameter.
01. Apply the Lua source first. Select Functions as Nodes → Add Function. Enter reward for Function Name and Archive Reward for Display Name. Enable Expose as Action and Visual Node.
02. Add tries and maximum in that order. Choose int for both, with defaults of 1 and 10. Select Use Definitions, then Apply Changes and save.
03. In an action list or visual graph, add Call Script Function, pick Archive Reward, and enter 3 and 10. Choose a numeric Result Variable to keep the 8. The definition tells the editor what fields to draw. The Lua code supplies the result.
Lua scripting / Illustrated guide W14
