ZEP Guidebook (EN)
  • Hello ZEP Script
  • ZEP Script
    • 💻ZEP Script Guide
      • ZEP Script Development Guide
        • JavaScript Development Tips
        • ZEP Script Deployment Guide
        • TypeScript Development Tips
      • Explore ZEP Script
        • Tutorials
          • Displaying a Message
          • Changing Avatar Image
          • Using HTML
          • Communicating with an External API
          • Creating a 2-Second Stun Effect
        • ZEP Script Example Code
          • Timer
          • Zombie Game
          • Paintman Game
          • Hangul Quiz Game
          • Avoid Poop Game
          • Boxing Game
          • Sidebar App
          • Race
      • ZEP Script FAQ
      • Appendix
        • ZEP Script Use Cases
        • Understanding Spaces and Maps
        • JavaScript Keycode List
        • Understanding Sprite Sheets
        • TileEffectType Detailed Explanation
        • What are Reference Coordinates?
        • Communicating with an External API
        • How to Use URL Query Strings
        • How to Change the Mobile Interaction Button
        • Grammar Available for Widgets
        • Object Interaction with ZEP Script
        • Object npcProperty
    • 📘ZEP Script API
      • API Summary
      • ScriptApp
        • Lifecycle
        • Field
        • Storage
        • Event Listeners
        • Callbacks
        • Methods
      • ScriptMap
        • Field
        • Methods
      • ScriptPlayer
        • Field
        • Methods
      • ScriptWidget
        • Field
        • Event Listeners
        • Methods
  • Others
    • Homepage
    • FAQ
    • Twitter
    • Discord
Powered by GitBook
On this page
  • Introduction
  • Understanding an App’s Lifecycle
  • 1️⃣ Enter-Phase Functions
  • onInit
  • onJoinPlayer
  • onStart
  • 2️⃣ Update-Phase Functions
  • onUpdate
  • 3️⃣ Exit-Phase Functions
  • onLeavePlayer
  • onDestroy

Was this helpful?

  1. ZEP Script
  2. ZEP Script API
  3. ScriptApp

Lifecycle

PreviousScriptAppNextField

Last updated 2 years ago

Was this helpful?

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 --")
});

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.

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.")
});


📘
Figure of Enter-phase functions in sequence
Timer Example