-
Notifications
You must be signed in to change notification settings - Fork 1
Scripting tips
SDraw edited this page Jun 3, 2023
·
11 revisions
- Static properties are accesses as
<ClassName>.<propertyName>
. Example:
Log(tostring(Time.timeDelta)) -- Will print value in seconds
- Static methods are accesses as
<ClassName>.<MethodName>(...)
. Example:
Log(tostring(Mathf.Lerp(0,10,0.5))) -- will print `5`
- Class constructors (if present) are accessed as
<ClassName>(...)
. Example:
local l_obj = GameObject("Test")
- Instance properties are accesses as
<ClassObject>.<propertyName>
. Example:
local l_obj = GameObject("Test")
Log(l_obj.name) -- Will print `Test`
- Instance properties assignment is done as
<ClassObject>.<propertyName> = <value>
. Example:
local l_obj = GameObject("Test1")
l_obj.name = "Test2"
Log(l_obj.name) -- Will print `Test2`
- Instance methods are accesses as
<ClassObject>:<MethodName>(...)
. Example:
local l_obj = GameObject("Test")
l_obj:SetActive(false)
- Enumerators are treated as strings. Example:
local l_obj = GameObject.CreatePrimitive("Sphere")
- Unknown properties and methods of binded classes return
nil
. Example:
local l_obj = GameObject("Test")
Log(tostring(l_obj.foo)) -- Will print `nil`
l_obj.foo = 42 -- Assignment is discarded without notification
Log(tostring(l_obj.foo)) -- Still will print `nil`
- Failed methods calls of binded classes return
false
and warning is logged into MelonLoader console. Example:
local l_obj = GameObject("Test")
Log(tostring(l_obj:SetActive("foo"))) -- Will print warning about expecting boolean as first argument and print `false`
- Optional arguments can be skipped by
nil
or_
(_
counts as variable, but its value isnil
unless you set it). Example:
local l_vecA = Vector3(25,nil,30) -- Created vector will have values (25,0,30)
local l_vecB = Vector3(25,_,30) -- Same, created vector will have values (25,0,30)
- Highly recommended to not rely on using userdata objects as keys for Lua tables, because theirs inner ID can be reused for new objects. If you insist, make sure to nil variables containing this userdata upon linked object destruction.
- Try to avoid excessive creation of wrapped objects, such as
Vector2/3/4
,Quaternion
and etc. Sometimes you can create single global object and use it further in your code. This will help to minimize problems of garbage collection on CSharp side.