User Tools

Site Tools


developer_center:developer_editor:script:rounding_a_string

Rouding a string

Finished product

UI Design

Diagram

Script

local name_of_element = nil --- they're a string like : "Coins"
local text_element = nil
local uiid = nil
local trigger_variable = nil --- they're a string
--- <The script was written by Don (UID: 209525865). please edit selectively, most parts are better left unedited.>
function round(e) 
    while true do
        Trigger:wait(0.1)
        local result, new_coins_result = VarLib2:getPlayerVarByName(e.eventobjid, 3, trigger_variable )
 
        if new_coins_result >= 1000 and new_coins_result < 1000000 then 
            new_re = string.format("%.1f", new_coins_result / 1000) .. "K"
        elseif new_coins_result >= 1000000 and new_coins_result < 1000000000 then 
            new_re = string.format("%.0f", new_coins_result / 1000000) .. "M"
        elseif new_coins_result >= 1000000000 and new_coins_result < 1000000000000 then
            new_re = string.format("%.0f", new_coins_result / 1000000000) .. "B"
        elseif new_coins_result >= 1000000000000 and new_coins_result < 1000000000000000 then
            new_re = string.format("%.0f", new_coins_result / 1000000000000) .. "T"
	---- Continue for "Qa" and "Q"
	else 
            new_re = tostring(new_coins_result)
        end
 
        Customui:setText(e.eventobjid, uiid, text_element, name_of_element .. new_re)
 
    end
end
 
ScriptSupportEvent:registerEvent("UI.Show", round)

Explanation

Variable Initialization:

local name_of_element = nil --- they're a string
local text_element = nil
local uiid = nil
local trigger_variable = nil --- they're a string

Getting the variable from variable library:

local result, new_coins_result = VarLib2:getPlayerVarByName(e.eventobjid, 3, trigger_variable )
  • VarLib2:getPlayerVarByName is used to retrieve a player's variable from variable library.

Convert it into a new string following the International System of Units (SI)

if new_coins_result >= 1000 and new_coins_result < 1000000 then 
    new_re = string.format("%.1f", new_coins_result / 1000) .. "K"
elseif new_coins_result >= 1000000 and new_coins_result < 1000000000 then 
    new_re = string.format("%.0f", new_coins_result / 1000000) .. "M"
elseif new_coins_result >= 1000000000 and new_coins_result < 1000000000000 then
    new_re = string.format("%.0f", new_coins_result / 1000000000) .. "B"
elseif new_coins_result >= 1000000000000 and new_coins_result < 1000000000000000 then
    new_re = string.format("%.0f", new_coins_result / 1000000000000) .. "T"
else 
    new_re = tostring(new_coins_result)
end
  • This section formats the new_coins_result value based on its magnitude. Depending on the range in which new_coins_result falls, it formats the number with appropriate suffixes like “K” (thousands), “M” (millions), “B” (billions), “T” (trillions), etc.

Updating UI

Customui:setText(e.eventobjid, uiid, text_element, name_of_element .. new_re)

Customui:setText updates the text in the UI, combining name_of_element with the formatted new_re.

Event Registration:

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

The round function is registered as a handler for the “UI.Show” (you can use “Game.AnyPlayer.EnterGame) event, meaning it will be called whenever 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/script/rounding_a_string.txt · Last modified: 2024/10/17 15:24 by don