Lifecycle
Introduction
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.
Understanding an App’s Lifecycle
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
1️⃣ Enter-Phase Functions
This guides the functions called during the lifecycle’s Enter Phase along with the execution of an app.
onInit
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.)
onJoinPlayer
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
Example
Display a message when a player enters.
onStart
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!
2️⃣ Update-Phase Functions
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.
onUpdate
App.onUpdate.Add(function(dt){})
This function runs periodically about every 20ms.
Parameter
Example
Make a 10-second timer using the onUpdate function.
3️⃣ Exit-Phase Functions
These functions execute when an app ends.
onLeavePlayer
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
Example
Display a message when a player exits.
onDestroy
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)
Last updated