Scripts
We use Lua 5.1.5 scripts, please refer to the official documentation for syntax and tutorials.
http://www.lua.org/manual/5.1/manual.html
Properties
All properties and methods exposed through the two reflection mechanisms can be accessed in the script.
local property = object.XXXProperty
object.XXXProperty = xxx
Calling Member Functions
Calling method:
object:XXXMethod(param0, param1)
Calling Global Functions
nx\_function(“global\_function\_name”, param0, param1,param2)
Custom Properties
object.CustomProperty = xxx
local xxx = object.CustomProperty
Global Objects
nx_set_value(“global_value_name”, global_value)
local global_value = nx_value(“global_value_name”)
Global variables in Lua script will be lost after the script is reloaded. So we recommend that you use nx_set_value and nx_value to set global objects, or use custom properties of objects to save data.
Due to the script context, game instances are isolated from each other and from the Editor.
Logs
nx\_log(“test\_log”)
Pop-up Windows
nx\_msgbox(“test\_msg”)
Coroutines
The script executed using nx_execute(script_path, script_function)
will trigger the coroutine mechanism if there is a loop in the script and nx_pause
is called.
a.lua
function test(form)
while true do
if not nx_is_valid(form) then
break
end
-- do something
nx_pause(0.0)
end
end
b.lua
nx\_execute(“a”, test, form)
Based on coroutines, some pseudo-multithreaded applications can be implemented (e.g. Updating the progress bar while doing some heavy tasks in the background can be implemented). Compared with true multithreading, coroutines are easier to write and more secure.
In addition, some general scripting methods are encapsulated in fx_core, please refer to fx_core.chm.