[Base Class](../groups/Base Class.Base Class.md) / Script
Script Class
Base class of script
- What is a script?
The attached script is like giving game objects special abilities or behaviors. Just as the soul gives life and personality to humans, scripts give life and behavior to game objects. They define the special abilities, action modes, intelligent decision-making, and ways of interacting with players of game objects.
You can write script to achieve the control logic of the character, the mode of the enemy's behavior, the effect of the props, the trigger conditions of the level, and so on. By attaching different script, you can give objects different behaviors and capabilities, and create a variety of interesting and diverse games.
- Classification of scripts
It can be roughly divided into two categories:
- 🍄 The script class inherits from Script and enjoys the default life cycle given by the editor.
When clicking on "New Script" in the editor, a script class that inherits from Script will be generated by default:
Usage example: default script format
@Component
export default class GameStart extends Script {
// When the script is instantiated, this function will be called before the first frame update
protected onStart(): void {
}
// Periodic function executed per frame
// This function requires assigning the value 'this.useUpdate' to 'true' for execution
// @param dt Delay between current frame and previous frame in seconds
protected onUpdate(dt: number): void {
}
// Call this function after the last frame is executed when the script is destroyed
protected onDestroy(): void {
}
}
@Component
export default class GameStart extends Script {
// When the script is instantiated, this function will be called before the first frame update
protected onStart(): void {
}
// Periodic function executed per frame
// This function requires assigning the value 'this.useUpdate' to 'true' for execution
// @param dt Delay between current frame and previous frame in seconds
protected onUpdate(dt: number): void {
}
// Call this function after the last frame is executed when the script is destroyed
protected onDestroy(): void {
}
}
- 🍄 Do not inherit other classes from Script.
for example
Usage example: ordinary class script format
export default class PlayerModS extends ModuleS<PlayerModC,null> {
public net_appleChange(player:Player){
}
}
export class Person {
}
class Student extends Person {
}
export default class PlayerModS extends ModuleS<PlayerModC,null> {
public net_appleChange(player:Player){
}
}
export class Person {
}
class Student extends Person {
}
- How does the script work?
- 🍄 Script class inherited from Script.
Script classes inherited from Script can override onStart(), onUpdate (), and onDestroy() methods. After your script is placed in the object manager, the editor will automatically call these functions for you.
The onStart, onUpdate and onDestroy methods in the life cycle of script can be compared to the growth process of a plant:
onStart The process of starting the germination of plant seeds. When a script inherited from a script is placed in a game object and the script is created and started, the onStart method will execute immediately after the script is loaded.
onUpdate The growth and maturation stages of plants. During the game runtime, the onUpdate method is called at every frame, during which logical code can be written to control the behavior, state, and interaction of game objects with other objects.
onDestroy The withering and ending stages of plants. When the game object is destroyed or removed from the scene, the onDestroy method will be call. This stage can be used to clean up, release asset, and save data to ensure that the exit process of the game object is correct and complete.
Note: The onUpdate function is call in every frame, not call at a fixed time interval. Each frame represents the time unit for update of the game editor, and its frequency depends on the frame rate (FPS) of the game.
Frame rate refers to the number of frames rendered per second, usually expressed in FPS (frames per second) units. The common frame rate is 60 FPS or 30 FPS. If the game runs at 60 FPS, the onUpdate function will be called once per frame, which is 60 times per second. Similarly, if the game runs at 30 FPS, the onUpdate function will be called 30 times per second.
It should be noted that the frame rate may be affected by factors such as the complexity of the game, the number of objects in the scene, graphics effects, and computing load. If the performance of the game decreases, the frame rate may decrease, resulting in a decrease in the call frequency of the onUpdate function. Therefore, it cannot be assumed that the onUpdate function is called at fixed time intervals, but should be treated as a function that is called every frame.
- 🍄 Do not inherit other classes from Script.
Hierarchy
Base
↳
Script
↳↳
PlayerState
Table of contents
Properties
Accessors
gameObject(): GameObject other |
---|
Get the gameobject mounted by the script |
useUpdate(): boolean other |
Get whether script enable onUpdate lifecycle function |
Methods
destroy(): void other |
---|
Destroy component object |
onDestroy(): void other |
Lifecycle function - call when destroyed |
onReplicated(path : string , value : unknown , oldVal : unknown ): boolean void other |
The attribute is synchronized with the event Client Only |
onStart(): void other |
Lifecycle function - called at the beginning of script execution |
onUpdate(dt : number ): void other |
Lifecycle Function - Execute Function per Frame |
Properties
Accessors
gameObject
• | • | ||||
---|---|---|---|---|---|
Get the gameobject mounted by the script Returns
| Set the gameobject object mounted by the script Parameters
|
useUpdate
• | • | ||||
---|---|---|---|---|---|
Get whether script enable onUpdate lifecycle function Returns
| Set whether the script enables the onUpdate lifecycle function Parameters
|
Methods
destroy
• destroy(): void
other
Destroy component object
onDestroy
• Protected
onDestroy(): void
other
Lifecycle function - call when destroyed
onReplicated
• Protected
onReplicated(path
, value
, oldVal
): boolean
void
other
The attribute is synchronized with the event Client Only
Parameters
path string | Usage: attribute path range: determined by path length |
---|---|
value unknown | Usage: property value |
oldVal unknown | Usage: Value before synchronization |
Returns
boolean void |
---|
onStart
• Protected
onStart(): void
other
Lifecycle function - called at the beginning of script execution
onUpdate
• Protected
onUpdate(dt
): void
other
Lifecycle Function - Execute Function per Frame
Parameters
dt number | Usage: The delay unit from the previous frame: seconds range: The size of dt will change according to the performance of the game. The performance of the game will decline, and the frame rate may decline, thus reducing the call frequency of the onUpdate function. (Please refer to the description at the beginning of the class for details) type: Floating point type numerical value |
---|
setUpdate When set to true, each frame is executed. When set to false, it will not be executed