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.
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.

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
// The first event called when running the app (before the user enters)
// Normal and Sidebar apps are called when the map is executed after applying the script [ Enter ]
App.onInit.Add(function(){
});
// All players enter the app through this event [ Enter ]
// Calls whenever a player enters afterward [ Events ]
App.onJoinPlayer.Add(function(player) {
});
// Event that starts first when each player enters [ Enter ]
App.onStart.Add(function(){
});
// Calls event every 20ms
// dt: deltatime (time taken for the previous frame to complete) [ Update ]
App.onUpdate.Add(function(dt){
});
// onUpdate again after handling the event callback
// Causes all players to leave the app when the app exits [ Exit ]
App.onLeavePlayer.Add(function(player){
});
// Last called when an app exits [ Exit ]
// Normal app and Sidebar app are closed separately
App.onDestroy.Add(function(){
});

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.)
App.onInit.Add(function(){
App.sayToAll("-- onInit --")
App.sayToAll(" ready.. ")
App.sayToAll("------------")
});

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
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.onJoinPlayer.Add(function(player){
App.showCenterLabel(`${player.name} has entered.`)
});

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.)
App.onStart.Add(function(){
App.sayToAll("-- App Start --")
});
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!
// main.js
App.onInit.Add(function(){
App.sayToAll("-- onInit --")
App.sayToAll(" ready.. ")
App.sayToAll("------------")
});
App.onJoinPlayer.Add(function(player){
App.sayToAll(`${player.name} has entered.`)
});
App.onStart.Add(function(){
App.sayToAll("-- App Start --")
});
Figure of Enter-phase functions in sequence

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
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.
Timer Example
let countdown = 10;
let timer = 0;
App.onUpdate.Add(function(dt){
timer += dt;
if(timer >=1){
if(countdown >= 0)
{
App.showCenterLabel(countdown--);
}
else{
App.showCenterLabel("Time Over");
}
timer = 0;
}
})

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
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.
// Executes when a player exits
App.onLeavePlayer.Add(function(player){
App.showCenterLabel(`${player.name} has left.`)
});

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)
// Executes when Game Block is destroyed
App.onDestroy.Add(function(){
App.showCenterLabel("Game Block has been destroyed.")
});


Last modified 1yr ago