Game Script
Usage
- Write the script by referring to the writing style of the Template and Example.
- Import the script into the editor.
- If the script uses Collision Events or Trigger Events, ensure the item has enabled and properly configured the corresponding components and features.
- Add the script in the
Executorcomponent and set up the necessary variables. - Enter the game, and the script will execute automatically.
Hooks
The game provides a total of 6 hooks, see BST/_Typings/gameApi/exportFuncs.d.ts.
init
export const init: (self: Item, vars: { [key: string]: any }) => voidExecuted when the executor loads the script. It executes before all other events and only executes once.
WARNING
Do not perform any operations on in-game objects in this function, because the scene has not finished loading at this point.
self: A reference to the item where the executor component is located.vars: The variables set on the executor, passed in as key-value pairs.
execute
export const execute: (...args: any[]) => voidExecuted when execute() is called by another executor in the game.
registerEvents
export const registerEvents: RegisterEvent[]Exports a string array to register the events it needs to receive.
Event List
WARNING
All messages passed in events are in the form of an array.
OnPhysicsUpdatePhysics updateOnLoadLevelWhen the level has finished loading, before the level startsOnStartLevelWhen the level starts, including the first time and after restartingOnQuitLevelWhen quitting the level without finishingOnTimerActiveWhen the player ball is spawned and the timer is enabledOnPreRestartLevelBefore executing a level restart- Cancellable
- Does not include restarts after completing the level
OnPostRestartLevelAfter executing a level restartOnPrePlayerDeadStartBefore the player falls into a death area / durability reaches zero / suicides- Cancellable
OnPostPlayerDeadStartAfter the player falls into a death area / durability reaches zero / suicidesOnPlayerDeadEndAfter the death animation endsOnPreCheckpointReachedBefore reaching a checkpoint- Message:
[Item]the checkpoint item - Cancellable
- Message:
OnPostCheckpointReachedAfter reaching a checkpoint- Message:
[Item]the checkpoint item
- Message:
OnPreDestinationReachedBefore reaching the destination- Message:
[Item]the destination item - Cancellable
- Message:
OnPostDestinationReachedAfter reaching the destination- Message:
[Item]the destination item
- Message:
OnPreGetCollectionBefore getting a collection- Message:
string[]the name of the collection being collected - Cancellable
- Message:
OnPostGetCollectionAfter getting a collection- Message:
string[]the name of the collection being collected
- Message:
OnPreSwitchBallStartBefore the ball switch animation starts- Message:
[BallType]the ball type being switched to - Cancellable
- Message:
OnPreSwitchBallEndBefore the ball switch animation ends- Message:
[BallType]the ball type being switched to - Cancellable
- Message:
OnPostSwitchBallEndAfter the ball switch animation ends- Message:
[BallType]the ball type being switched to
- Message:
OnPreTransferBallStartBefore the ball transfer animation starts- Message:
[Item, Item]the teleporter items; the first is the current one, the second is the target - Cancellable
- Message:
OnPreTransferBallEndBefore the ball transfer animation ends- Message:
[Item, Item]the teleporter items; the first is the current one, the second is the target - Cancellable
- Message:
OnPostTransferBallEndAfter the ball transfer animation ends- Message:
[Item, Item]the teleporter items; the first is the current one, the second is the target
- Message:
OnPlayerCollideEnterPlayer collision enter event- Message:
CollisionEvent[]the collision events
- Message:
OnPlayerCollideStayPlayer collision stay event- Message:
CollisionEvent[]the collision events
- Message:
OnPlayerCollideExitPlayer collision exit event- Message:
CollisionEvent[]the collision events
- Message:
OnReceiveCustomEventWhen a custom event is received- Message:
any[]the value of the custom event
- Message:
OnTntExplodedTNT explosion event- Message:
Float3[]the explosion positions of all TNTs
- Message:
onEvents
export const onEvents: (self: Item, events: Events) => voidExecuted when a registered event is triggered.
self: A reference to the item where the executor component is located.events: The triggered events and their passed messages, passed in as key-value pairs. The key is the event name, and the value is the passed message array.
onTrigger
export const onTrigger: (
self: Item,
triggeredItem: Item,
type: "Enter" | "Stay" | "Exit"
) => voidExecuted when a trigger is activated.
self: A reference to the item where the trigger component is located.triggeredItem: A reference to the item or player that activated the trigger.type: The type of trigger event.
onCollide
export const onCollide: (self: Item, collisionEvent: CollisionEvent) => voidExecuted when an object collides.
self: A reference to the object on which the collision occurred.collisionEvent: The collision event object, which contains information about the colliding object, the type of collision, the position of the collision, etc.
Modules
The game provides the following 10 modules:
For details, see BST/_Typings/gameApi/modules.
To import modules, use the following syntax:
import {
// player
player,
// modules
console,
scene,
variables,
settings,
audioManager,
levelManager,
inputManager,
tweenManager,
math,
uiCanvas,
// basic data types
Float2,
Float3,
Float4,
ColorRGBA,
Bool3,
Trans,
Quaternion,
} from "gameApi"TIP
To import custom modules from within the project, please use BME's resource path, for example:
import { dotProduct } from "Scripts/Utility/MathExtension.js"WARNING
The .js suffix cannot be omitted here.
Template
See BST/Templates/gameTemplate.js.