Lifecycle
Last updated
Last updated
An app’s lifecycle refers to a cycle where an app starts, runs, and ends. You can create an app’s lifecycle by executing necessary actions in situations when the app starts, runs, or ends.
Name | Description |
---|---|
onInit | Function that is called once when running the app the first time |
onJoinPlayer | Once onInit is called, the event lets all connected players enter and operates whenever a player enters afterward. |
onStart | Function that is called once after each player enters through onJoinPlayer |
onUpdate | Function that runs periodically every 20ms |
onLeavePlayer | This function causes all players to exit an app when another app is launched or the installed Game Block is destroyed. |
onDestroy | Operates when another app is launched or the installed Game Block is destroyed. |
Lifecycle functions are necessary as they help create functions according to an app’s life cycle. As you can see in the image below, Enter-Phase functions operate when an app starts. When an app is running, Update-Phase functions operate periodically, and when it ends, Exit-Phase functions start.
Make your app’s lifecycle from onInit to onDestroy utilizing the timing of each phrase.
Lifecycle at a Glance
This guides the functions called during the lifecycle’s Enter Phase along with the execution of an app.
App.onInit.Add(function(){})
This function is called once when running the app for the first time.
Parameter
None
Example
Display chat in onInit. (Make this as a Mini-Game to check it out.)
App.onJoinPlayer.Add(function(player){})
Once onInit is called, this event lets all connected players enter and then operates whenever a new player enters afterward.
Parameter
Name | Type | Description |
---|---|---|
player | Player | The player who enters The player’s parameter names can be changed arbitrarily |
Example
Display a message when a player enters.
App.onStart.Add(function(){})
This function is called once after players have entered via onJoinPlayer.
Parameter
None
Example
Display chat in onStart. (Make this as a Mini-Game to check it out.)
Understanding the Flow of Enter-Phase Functions
Check the Enter-Phase lifecycle flow with codes. Make a Mini-Game with the code below and run it!
Update has an onUpdate function that runs periodically about every 20ms.
When an event such as onJoinPlayer or onLeavePlayer occurs, onUpdate is periodically executed again after handling the event.
App.onUpdate.Add(function(dt){})
This function runs periodically about every 20ms.
Parameter
Name | Type | Description |
---|---|---|
dt | Number | deltatime (time taken for the previous frame to complete, about 20ms) The dt parameter names can be changed arbitrarily |
Example
Make a 10-second timer using the onUpdate function.
These functions execute when an app ends.
App.onLeavePlayer.Add(function(player){})
This function operates whenever a player exits. After that, this function kicks all players from the app when another app is launched or the installed Game Block is destroyed.
Parameter
Name | Type | Description |
---|---|---|
player | Player | The player who exits The player’s parameter names can be changed arbitrarily |
Example
Display a message when a player exits.
App.onDestroy.Add(function(){})
It operates when another app is launched or the installed Game Block is destroyed.
Parameter
None
Example
Display a message when Game Block is destroyed. (Mini-Game)