developer_center:what_is_register_event

ScriptSupportEvent:registerEvent Explanation (For Miniworld: Creata)

In Miniworld: Creata, the game provides a special API called ScriptSupportEvent:registerEvent. This API is used to register new events in the game that your Lua script can listen to and respond to.

How it Works

The function ScriptSupportEvent:registerEvent is used to bind a specific game event to a Lua function, so that whenever the event occurs in the game, the corresponding Lua function gets executed. This allows you to create event-driven behavior in your scripts.

Syntax:

ScriptSupportEvent:registerEvent(EventName, FunctionToExecute)
  • EventName: The name of the event you want to listen for (e.g., “Game.Start” or “Player.ClickBlock”).
  • FunctionToExecute: The Lua function that will be called whenever the event happens.

List Of All Event Here

Example 1: Game Start Event

The “Game.Start” event is triggered whenever the game starts. You can use this event to run code when the game begins. In this case, the event does not pass any parameters to the function.

ScriptSupportEvent:registerEvent("Game.Start", function()
    print("The game has started!")
end)

In this example, every time the game starts, the message “The game has started!” will be printed.

  • Event Name: “Game.Start”
  • Function Parameter: None (no parameters are passed to the function in this case).

Example 2: Player Click Block Event

The “Player.ClickBlock” event is triggered whenever a player clicks on a block in the game. When this event is fired, it passes a table as a parameter to the function, which contains the following information:

  • eventobjid: The object ID of the event.
  • x, y, z: The coordinates of the block the player clicked.
  • blockid: The ID of the block that was clicked.

Example:

ScriptSupportEvent:registerEvent("Player.ClickBlock", function(eventData)
    local objid = eventData.eventobjid
    local x = eventData.x
    local y = eventData.y
    local z = eventData.z
    local blockid = eventData.blockid
 
    print("Player clicked block at position:", x, y, z, "with block ID:", blockid)
end)

In this case, every time a player clicks on a block, it will print the coordinates and the block ID to the console.

  • Event Name: “Player.ClickBlock”
  • Function Parameter: A table containing the details of the block clicked.

Key Points:

ScriptSupportEvent:registerEvent is specific to Miniworld: Creata and is not part of standard Lua.

It takes two arguments: the event name (string) and the function to execute. Different events pass different types of parameters to the function. For “Game.Start”, no parameters are passed. For “Player.ClickBlock”, a table with details about the block is passed. By using this API, you can make your scripts respond to various game events dynamically, allowing you to create more interactive and responsive game behavior.

developer_center/what_is_register_event.txt · Last modified: 2024/09/14 07:37 by hhuxx