Skip to reading
Lua Scripting

Make a Lua function a visual node

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations

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.

Code example
function Script.reward(tries, maximum)
    return math.max(0, maximum - math.max(0, tries - 1))
end

This 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
Page 14 of 133Permalink
Go to page