User Tools

Site Tools


developer_center:developer_editor:script:enter_string_position_to_teleport

How to make teleport UI

Finished product

Design

Script

local pos_1, pos_2, pos_3   -- Variables to store X, Y, Z coordinates
local X_input = nil        -- ID of X input box (to be replaced with actual ID)
local Y_input = nil        -- ID of Y input box (to be replaced with actual ID)
local Z_input = nil        -- ID of Z input box (to be replaced with actual ID)
local teleport_button = nil  -- ID of teleport button (to be replaced with actual ID)
 
-- Function to handle UI events
function teleport(e)
    local location = tonumber(e.content)  -- Extract the numerical content from UI event
    local player_id = e.eventobjid         -- ID of the player who triggered the event
    -- Do not edit
    -- Check which UI element triggered the event and update corresponding position variable
    if e.uielement == X_input then
        pos_1 = location  -- Store X coordinate
    end
 
    if e.uielement == Y_input then
        pos_2 = location  -- Store Y coordinate
    end
 
    if e.uielement == Z_input then
        pos_3 = location  -- Store Z coordinate
    end
 
    -- If the teleport button is clicked, attempt to teleport the player
    if e.uielement == teleport_button then
        -- Check if all coordinates (pos_1, pos_2, pos_3) are set
        if pos_1 and pos_2 and pos_3 then
            -- Teleport the player to the specified coordinates
            Player:setPosition(player_id, pos_1, pos_2, pos_3)
            -- Notify the player that teleportation was successful
            Player:notifyGameInfo2Self(player_id, "Teleportation successful")
        else
            -- Notify the player that teleportation failed due to incomplete coordinates
            Player:notifyGameInfo2Self(player_id, "Teleportation failed: Coordinates are not fully set! Please try again")
        end
    end
end
 
-- Register the teleport function to handle UI events for lost focus and button clicks
ScriptSupportEvent:registerEvent("UI.LostFocus", teleport)
ScriptSupportEvent:registerEvent("UI.Button.Click", teleport)

Explanation

Variable Declarations

  • pos_1, pos_2, pos_3: These variables are used to store the X, Y, and Z coordinates respectively for teleportation.
  • X_input, Y_input, Z_input, teleport_button: These variables are placeholders for the IDs of the input boxes and teleport button in the user interface. They are initialized to nil and are meant to be replaced with actual IDs from the UI elements.

Teleport Function (teleport)

  • This function is responsible for handling UI events related to teleportation.
  • e is the event object passed to the function containing information about the event triggered.
  • location is extracted from e.content and converted to a number (assuming e.content contains the numerical value of the input).

Event Handling

  • The function checks which UI element triggered the event (e.uielement) using conditional statements (if blocks).
  • Depending on which input box (X_input, Y_input, Z_input) received focus or which button (teleport_button) was clicked, the corresponding position variable (pos_1, pos_2, pos_3) is updated with the provided location.

Teleportation Logic

  • When the teleport button is clicked (e.uielement == teleport_button), the function checks if all three position variables (pos_1, pos_2, pos_3) have been set.
  • If all positions are set, it calls Player:setPosition(player_id, pos_1, pos_2, pos_3) to teleport the player to the specified coordinates.
  • It then notifies the player with Player:notifyGameInfo2Self(player_id, message) about the success or failure of the teleportation attempt based on whether all coordinates were set.

Event Registration

  • The ScriptSupportEvent:registerEvent function registers the teleport function to handle events of type 'UI.LostFocus' (when an input box loses focus) and “UI.Button.Click” (when a button is clicked).

If you have any questions about Developer Tools, feel free to join our official discord server Mini World Global DEV discord

developer_center/developer_editor/script/enter_string_position_to_teleport.txt · Last modified: 2024/07/19 09:24 by don