User Tools

Site Tools


developer_center:developer_editor:timer

Timer

Advanced Document: Countdown time

Finished products

Design

Diagram

Script

local uiid = nil --- replace your UI ID here 
local text_timer = nil --- replace you text here 
local min = nil --- replace your minutes here 
local sec = nil --- replace your seconds here 

function game(e)
    repeat
        sec = sec - 1 
        Customui:setText(e.eventobjid, uiid,text_timer, min .. ":" .. sec)
        if sec == 0 and min > 0 then 
            sec = 60 
            min = min -1 
            Customui:setText(e.eventobjid, uiid,text_timer, min .. ":" .. sec)
        end
	Trigger:wait(1)
    until min == 0 and sec == 0
end

ScriptSupportEvent:registerEvent("UI.Show", game)

Explanation

Variable Declarations

local uiid = nil --- replace your UI ID here 
local text_timer = nil --- replace you text here 
local min = nil --- replace your minutes here 
local sec = nil --- replace your seconds here 
  • uiid: This variable is intended to hold the ID of the UI element that will display the countdown timer.
  • text_timer: This variable is intended to hold the text that will be displayed along with the timer.
  • min: This variable is intended to hold the number of minutes for the countdown.
  • sec: This variable is intended to hold the number of seconds for the countdown.

Game function

function game(e)
    repeat
        sec = sec - 1 
        Customui:setText(e.eventobjid, uiid, text_timer, min .. ":" .. sec)
        if sec == 0 and min > 0 then 
            sec = 60 
            min = min - 1 
            Customui:setText(e.eventobjid, uiid, text_timer, min .. ":" .. sec)
        end
	Trigger:wait(1)
    until min == 0 and sec == 0
end
  • function game(e): This defines a function named game that takes an event object e as a parameter.
  • repeat … until: This is a loop that will continue to execute the code block inside it until the condition min == 0 and sec == 0 is met.

Within the loop:

  • sec = sec - 1: Decreases the value of sec by 1.
  • Customui:setText(e.eventobjid, uiid, text_timer, min .. “:” .. sec): Updates the text of the UI element identified by uiid with the current value of min and sec.
  • if sec == 0 and min > 0 then: Checks if the seconds have reached 0 and there are still minutes left.
    • sec = 60: Resets the seconds to 60.
    • min = min - 1: Decreases the minutes by 1.
    • Customui:setText(e.eventobjid, uiid, text_timer, min .. “:” .. sec): Updates the UI text again with the new values of min and sec.
  • Trigger:wait(1): Pauses the execution of the loop for 1 second before continuing.

The loop continues to execute until both min and sec are 0.

Event Registration

  ScriptSupportEvent:registerEvent("UI.Show", game)

ScriptSupportEvent:registerEvent(“UI.Show”, game): Registers the game function to be called when the “UI.Show” event occurs. This means the countdown will start whenever the UI is shown.

Summary

In summary, this script sets up a countdown timer that updates a specified UI element every second. It decreases the seconds and minutes accordingly and updates the UI text to reflect the current countdown time. When the countdown reaches zero minutes and zero seconds, the loop stops. The timer starts when the “UI.Show” event is triggered.


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/timer.txt · Last modified: 2024/07/03 08:31 by don