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
  • 📚 API Explanation and Example
  • onMessage

Was this helpful?

  1. ZEP Script
  2. ZEP Script API
  3. ScriptWidget

Event Listeners

PreviousFieldNextMethods

Last updated 2 years ago

Was this helpful?

Introduction

Provided below are functions that activate when data is sent to the App from the widget.

Name
Description

onMessage

Function that activates when messages sent from widget are received on the App

📚 API Explanation and Example

onMessage

widget.onMessage.Add(function(player, data: any){});

Callback function that activates when a message is sent from the widget to the App.

Parameters

Name
Type
Description

player

Player

The player who owns the widget

data

Object

The message sent from the widget to the App

Example

Create a function to close the widget screen when x is pressed.

// Activates function when a player enters
App.onJoinPlayer.Add(function (player) {
	player.tag = {
		widget: null,
	};

	player.tag.widget = player.showWidget("sample.html.html", "top", 600, 500);
	player.tag.widget.onMessage.Add(function (player, msg) {
		// Closes the widget when the 'type: close' message is sent from the widget to the App 
		if (msg.type == "close") {
			player.showCenterLabel("Widget has been closed.");
			player.tag.widget.destroy();
			player.tag.widget = null;
		}
	});
});

sample.html: Button and script section

<i onclick="closeWidget()" class="fa-solid fa-xmark"></i>
<script type="text/javascript">
			// Calls the function when x button is pressed
			function closeWidget() {
				// Sends message to App 
				window.parent.postMessage(
					{
						type: "close",
					},
					"*"
				);
			}
</script>
📘
15KB
sample_EventListener.zip
archive