Event Functions
A script in Unity is not like the traditional idea of a program where the code runs continuously in a loop until it completes its task. Instead, Unity passes control to a script intermittently by calling certain functions that are declared within it. Once a function has finished executing, control is passed back to Unity.
These functions are known as event functions since they are activated by Unity in response to events that occur in the world.
Similar to Unity, ParaSpace also provides some event functions for calling in certain cases, and will take back the control after functions are executed.
Unity uses a naming scheme to identify which function to call for a particular event. For example, we have introduced the Start Function before (called just before the object's first frame update). ParaSpace uses the same way to execute a particular function when the corresponding event occurs by overriding the specifically named function.
Unity Event Functions
Lua scripts, which are associated with Para scripts, are executed in compliance with Unity MonoBehavior rules and life cycle. Unity MonoBehavior event functions that can be used in scripts are shown below:
Awake | Awake is called when the script instance is being loaded. |
Start | Start is called on the frame when a script is enabled just before any of the Update methods are called the first time. |
Update | Update is called every frame, if the MonoBehaviour is enabled. |
FixedUpdate | Frame-rate independent MonoBehaviour.FixedUpdate message for physics calculations. |
LateUpdate | LateUpdate is called every frame, if the Behaviour is enabled. |
OnDisable | This function is called when the behaviour becomes disabled. |
OnEnable | This function is called when the object becomes enabled and active. |
OnCollisionEnter | OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. |
OnCollisionExit | OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider. |
OnTriggerEnter | When a GameObject collides with another GameObject, Unity calls OnTriggerEnter. |
OnTriggerExit | OnTriggerExit is called when the Collider other has stopped touching the trigger. |
To better understand how the above events work, refer to Event Functions.
For beginners, focus on Initialization Events (Awake, Start), Regular Update Events (Update, FixedUpdate, LateUpdate), and Physics Events for now.
Further Reading
ParaSpace Event Functions
ParaSpace provides a huge number of event functions. You can find the complete event function list and details on how to use them on the ParaScript class script reference page. Here are some of the most common and important events.
Player Events
Player-related events are required for processing player-related logic. For example: Assign an identity to the player who joins the room; clear the player's game data when he/she leaves the room; trigger special behaviors when the play enters a specific area...
OnPlayerJoined(player) | Triggered when a player joins the room. The player parameter represents the player object that joins the room. |
OnPlayerLeft(player) | Triggered when a player leaves the room. The player parameter represents the player object that leaves the room. |
OnPlayerRespawn(player) | Triggered when the player respawns. |
OnPlayerTriggerEnter(player) | Triggered when the capsule collider of the player enters a trigger area. |
OnPlayerTriggerExit(player) | Triggered when the capsule collider of the player leaves a trigger area. Note: If the player logs out at the trigger area, the event cannot be received, and OnPlayerLeft is required to handle this logic. |
OnPlayerCollisionEnter(player) | Triggered when the capsule collider of the player collides with another collision area. |
OnPlayerCollisionExit(player) | Triggered when the capsule collider of the player leaves another collision area. Note: If the player logs out at the trigger area, the event cannot be received and OnPlayerLeft is required to handle this logic. |
Interaction Events
Interaction-related events are required for customizing some post-interaction logic. For example: Pick up a firework, and it starts to set off; drop a bomb to the floor, and it explodes...
OnPickup(player, handType, pickUpPoint) | Triggered when the player picks up a certain item. player represents the player who is operating, handType represents whether the player picks up the item in his/her left or right hand, and pickUpPoint corresponds to the location where the item is picked up. |
OnDrop(player, handType) | Triggered when the player drops a certain item. player represents the player who is operating, and handType represents whether the player drops the item in his/her left or right hand. |
Network Events
Network-related events are required for handling the initialization of network objects or ownership transfer problems.
For details about network-related concepts, refer to the following section: Multiplayer and Networking
OnNetSpawned() | Triggered when a network object is generated. With this function, you can handle some logic related to network initialization. |
Updated about 2 years ago