Properties¶
In many cases, your scripts will require external input to fine-tune their behavior according to specific requirements. For example, a script might need a reference to another scene object or a numeric constant to configure a particular behavior. Our engine facilitates this through properties that are exposed to the editor, which can then be set by the user.
Defining Properties¶
Properties can be defined directly in the Lua script using the Property class. These properties are then visible and editable within the engine’s editor, allowing users to configure script behavior dynamically without modifying the script’s code. Properties may be defined with or without default values.
local MyEntity = Class.new(Entity)
MyEntity.x = Property.new(Property.Number, 0)
MyEntity.y = Property.new(Property.Number, 0)
function MyEntity:init()
print("The sum of _x and _y is:", self.x:get() + self.y:get())
end
return MyEntity
Supported Property Types¶
The engine supports several types of properties that can be used according to the needs of the script. Each type serves different purposes, from simple numbers and strings to references to objects within the scene graph.
Type |
Description |
---|---|
Property.Number |
Can be an integer or float. |
Property.Bool |
A true or false value. |
Property.String |
A sequence of characters. |
Property.Node |
A reference to a scene graph node. |
Implicit Properties¶
In addition to any user-defined properties, each script automatically has access to one implicit property:
node: This is of type node and provides a reference to the scene graph node to which the script is attached. This allows scripts to interact directly with their host node, manipulating fields or responding to changes in the scene graph.
function MyEntity:init()
print("The script is attached to node:", self.node:get():getDisplayName())
end
It’s important to utilize these properties effectively to create flexible and reusable scripts. By leveraging both explicit and implicit properties, scripts can be made more adaptable to various game scenarios and design requirements.