Lightact allows you to write Lua scripts where each exposed function is represented as a node. Lightact encourages you to write your Lua scripts in your favorite IDE or Text editor (such as Notepad++). Once you save them into a file, you can import them as an Asset and use the exposed functions as nodes in Layer Layouts. But let’s go one step at a time.
Importing a Lua script
Let’s say we have a simple Lua function that we want to use in Lightact.
function simpleLuaFunc (n) if n == 0 then return 1 else return n * n end end
We save the script into a file called myFirstLua.lua. In Lightact, we press Ctrl+D to import an asset and, in the dialog window, we find the Lua file, we just saved.
During the import process, the file is copied into the project’s asset folder in the scripts subfolder. Its name will appear in the Assets tab in the main window.
Now, open the myFirstLua.lua file that’s been copied to the scripts subfolder in your favorite code editor. Add the following line right at the top of the script.
-- @EXPOSED int->int
Now, the whole script should look like this:
-- @EXPOSED int->int function simpleLuaFunc (n) if n == 0 then return 1 else return n * n end end
A few seconds after you’ve saved the file, Lightact will detect that it’s been changed and its Source property in the Assets tab will turn yellow.
This means you should refresh the script in Lightact.
You can do that by right-clicking on the Name of the script and clicking on Refresh script.
Use Lua in Layouts
Now, let’s open a Layer Layout.
Right-click somewhere in the empty area, hover above Script functions and you will see the name of the Lua file you imported (myFirstLua.lua). If you hover above it you’ll see the name of your simpleLuaFunc function.
If you click on it, a new node will appear that will be based on the Lua function you created. It has an integer input and an integer output. Even though Lua doesn’t declare variable types explicitly, Lightact needs to know the variable types of function inputs (parameters) and outputs in order to be able to create a corresponding node. That’s why the
--@EXPOSED int->int
line is there. It tells Lightact that this Lua function should be visible in the Layouts and that it has one integer input and one integer output.
So, if you connect the function as shown above and change the Int input of the node, you’ll see that the output is determined by the Lua function simpleLuaFunc.
External variables and non-exposed functions
Now change the Lua file, so that it looks like this:
-- @EXPOSED int->int function simpleLuaFunc (n) if n == 0 then return 1 else return n * n end end extVar = 0 function internalIncrease(p) extVar=extVar + p end --@EXPOSED float->void function callIncrease(p) internalIncrease(p) end --@EXPOSED void->float function getExtVar() return extVar end
After you’ve saved the file, don’t forget to click on Refresh script command in Lightact.
As you can see, we’ve added one external variable called extVar, we’ve added 2 exposed functions (callIncrease and getExtVar) and one non-exposed function called internalIncrease. internalIncrease adds its input parameter to the extVar variable, callIncrease function makes a simple call of internalIncrease function and passes the parameter on. getExtVar, on the other hand, just returns the current value of extVar.
If you create the above layout you’ll see that it ads the value of the Float input to the extVar in every frame.
The above example illustrates how you can expose or hide Lua functions to Lightact and how you can use external variables and function calls from within a single Lua script.
Variable types
Now, add the following code at the bottom of your Lua file.
--@EXPOSED int->string function intToString(n) return "Input int is: "..n end -- @EXPOSED (string,float,int)->(int,float,string) function multiInAndOut(s,f,i) str="String: "..s..", Float: "..f..", Int: "..i return i+f,f-i,str end
Click on Refresh script command in Lightact. As you can see we’ve added 2 additional functions: intToString and multiInAndOut.
And they are reflected in the Layout menu as well.
The node inputs and outputs reflect what’s written right after the @EXPOSED keyword. As you can see, if you type in inputs or outputs in parenthesis, for example:
-- @EXPOSED (string,float,int)->(int,float,string)
, Lightact will create the corresponding number of node inputs and outputs.
If you connect the nodes, as shown above, you’ll see how your Lua functions work.
Conclusion
As you can see Lightact allows you to write Lua scripts and use the exposed functions in Layer Layouts as many times as you want.
For your convenience you can find the final Lua file below:
-- @EXPOSED int->int function simpleLuaFunc (n) if n == 0 then return 1 else return n * n end end extVar = 0 function internalIncrease(p) extVar=extVar + p end --@EXPOSED float->void function callIncrease(p) internalIncrease(p) end --@EXPOSED void->float function getExtVar() return extVar end --@EXPOSED int->string function intToString(n) return "Input int is: "..n end -- @EXPOSED (string,float,int)->(int,float,string) function multiInAndOut(s,f,i) str="String: "..s..", Float: "..f..", Int: "..i return i+f,f-i,str end