|
@@ -32,76 +32,76 @@ Defold executes Lua scripts as part of the engine lifecycle and exposes the life
|
|
|
`self` is a userdata object that acts like a Lua table but you can't iterate over it with `pairs()` or `ipairs()` and you can't print it using `pprint()`.
|
|
|
:::
|
|
|
|
|
|
-`init(self)`
|
|
|
-: Called when the component is initialized.
|
|
|
-
|
|
|
- ```lua
|
|
|
- function init(self)
|
|
|
- -- These variables are available through the lifetime of the component instance
|
|
|
- self.my_var = "something"
|
|
|
- self.age = 0
|
|
|
- end
|
|
|
- ```
|
|
|
+#### `init(self)`
|
|
|
+Called when the component is initialized.
|
|
|
+
|
|
|
+```lua
|
|
|
+function init(self)
|
|
|
+ -- These variables are available through the lifetime of the component instance
|
|
|
+ self.my_var = "something"
|
|
|
+ self.age = 0
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
-`final(self)`
|
|
|
-: Called when the component is deleted. This is useful for cleaning up purposes, for instance if you have spawned game objects that should be deleted when the component is deleted.
|
|
|
+#### `final(self)`
|
|
|
+Called when the component is deleted. This is useful for cleaning up purposes, for instance if you have spawned game objects that should be deleted when the component is deleted.
|
|
|
|
|
|
- ```lua
|
|
|
- function final(self)
|
|
|
- if self.my_var == "something" then
|
|
|
- -- do some cleanup
|
|
|
- end
|
|
|
+```lua
|
|
|
+function final(self)
|
|
|
+ if self.my_var == "something" then
|
|
|
+ -- do some cleanup
|
|
|
end
|
|
|
- ```
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
-`update(self, dt)`
|
|
|
-: Called once each frame. `dt` contains the delta time since the last frame.
|
|
|
+#### `update(self, dt)`
|
|
|
+Called once each frame. `dt` contains the delta time since the last frame.
|
|
|
|
|
|
- ```lua
|
|
|
- function update(self, dt)
|
|
|
- self.age = self.age + dt -- increase age with the timestep
|
|
|
- end
|
|
|
- ```
|
|
|
+```lua
|
|
|
+function update(self, dt)
|
|
|
+ self.age = self.age + dt -- increase age with the timestep
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
-`fixed_update(self, dt)`
|
|
|
-: Frame-rate independent update. `dt` contains the delta time since the last update. This function is called when `engine.fixed_update_frequency` is enabled (!= 0). Useful when you wish to manipulate physics objects at regular intervals to achieve a stable physics simulation when `physics.use_fixed_timestep` is enabled in *game.project*.
|
|
|
+#### `fixed_update(self, dt)`
|
|
|
+Frame-rate independent update. `dt` contains the delta time since the last update. This function is called when `engine.fixed_update_frequency` is enabled (!= 0). Useful when you wish to manipulate physics objects at regular intervals to achieve a stable physics simulation when `physics.use_fixed_timestep` is enabled in *game.project*.
|
|
|
|
|
|
- ```lua
|
|
|
- function fixed_update(self, dt)
|
|
|
- msg.post("#co", "apply_force", {force = vmath.vector3(1, 0, 0), position = go.get_world_position()})
|
|
|
- end
|
|
|
- ```
|
|
|
+```lua
|
|
|
+function fixed_update(self, dt)
|
|
|
+ msg.post("#co", "apply_force", {force = vmath.vector3(1, 0, 0), position = go.get_world_position()})
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
-`on_message(self, message_id, message, sender)`
|
|
|
-: When messages are sent to the script component through [`msg.post()`](/ref/msg#msg.post) the engine calls this function of the receiver component. Learn [more about message passing](/manuals/message-passing).
|
|
|
+#### `on_message(self, message_id, message, sender)`
|
|
|
+When messages are sent to the script component through [`msg.post()`](/ref/msg#msg.post) the engine calls this function of the receiver component. Learn [more about message passing](/manuals/message-passing).
|
|
|
|
|
|
- ```lua
|
|
|
- function on_message(self, message_id, message, sender)
|
|
|
- if message_id == hash("increase_score") then
|
|
|
- self.total_score = self.total_score + message.score
|
|
|
- end
|
|
|
+```lua
|
|
|
+function on_message(self, message_id, message, sender)
|
|
|
+ if message_id == hash("increase_score") then
|
|
|
+ self.total_score = self.total_score + message.score
|
|
|
end
|
|
|
- ```
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
-`on_input(self, action_id, action)`
|
|
|
-: If this component has acquired input focus (see [`acquire_input_focus`](/ref/go/#acquire_input_focus)) the engine calls this function when input is registered. Learn [more about input handling](/manuals/input).
|
|
|
+#### `on_input(self, action_id, action)`
|
|
|
+If this component has acquired input focus (see [`acquire_input_focus`](/ref/go/#acquire_input_focus)) the engine calls this function when input is registered. Learn [more about input handling](/manuals/input).
|
|
|
|
|
|
- ```lua
|
|
|
- function on_input(self, action_id, action)
|
|
|
- if action_id == hash("touch") and action.pressed then
|
|
|
- print("Touch", action.x, action.y)
|
|
|
- end
|
|
|
+```lua
|
|
|
+function on_input(self, action_id, action)
|
|
|
+ if action_id == hash("touch") and action.pressed then
|
|
|
+ print("Touch", action.x, action.y)
|
|
|
end
|
|
|
- ```
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
-`on_reload(self)`
|
|
|
-: This function is called when the script is reloaded through the hot reload editor function (<kbd>Edit ▸ Reload Resource</kbd>). It is very useful for debugging, testing and tweaking purposes. Learn [more about hot-reload](/manuals/hot-reload).
|
|
|
+#### `on_reload(self)`
|
|
|
+This function is called when the script is reloaded through the hot reload editor function (<kbd>Edit ▸ Reload Resource</kbd>). It is very useful for debugging, testing and tweaking purposes. Learn [more about hot-reload](/manuals/hot-reload).
|
|
|
|
|
|
- ```lua
|
|
|
- function on_reload(self)
|
|
|
- print(self.age) -- print the age of this game object
|
|
|
- end
|
|
|
- ```
|
|
|
+```lua
|
|
|
+function on_reload(self)
|
|
|
+ print(self.age) -- print the age of this game object
|
|
|
+end
|
|
|
+```
|
|
|
|
|
|
|
|
|
## Reactive logic
|