I need help a file. Moon

Asked

Viewed 58 times

0

I have recently been "learning" mecher with programming, but I’m still very lay. And recently I developed a HUD where I play, but I’d like to change the string format. his.

local function AddLoot(id, new)
    local newVal = new * GetValue(id)
    Looted = Looted + newVal

    local hud = LootedHUDs[id]
    if hud then
        hud.count = hud.count + new
        hud.hud:SetText(hud.count .." (".. GetValue(id) * hud.count .." gp)")
    else
        local hud = {}
        hud.count = new
        NewlyLooted = NewlyLooted + 1
        hud.posy = TotalLoot.posy
        hud.img = HUD.New(Hudx-5, hud.posy-5, id, textColor.r, textColor.g, textColor.b)
        hud.img:SetItemSize(10)
        hud.name = HUD.New(Hudx+30, hud.posy, Item.GetName(id), countColor.r, countColor.g, countColor.b)
        hud.hud = HUD.New(Hudx+165, hud.posy, new .." (".. newVal .." gp)", textColor.r, textColor.g, textColor.b)
        LootedHUDs[id] = hud
        
        TotalLoot.posy = TotalLoot.posy + 15
        TotalLoot.hud:SetPosition(Hudx + 80, TotalLoot.posy + 15)
        TotalLootTitle.hud:SetPosition(Hudx, TotalLoot.posy + 15)
    end
end

That part [ Hud.Hud = HUD.New(Hudx+165, Hud.Posy, new ." (".. newVal .." gp)", textColor. r, textColor. g, textColor. b. b) ] is where it shows what was counted, but it does not separate the thousands. For example:

total loot: 1000000 gps <- so it gets

total loot: 1,000,000 gps <- so I want it to stay.

Could someone help me ?

1 answer

0


You can create a function that uses regex to identify each 3 digits and insert a dot between them to help you with this, it would look something like this:

local function addSeparators(number) -- Declaração da função, recebe um numero
    local k -- Variavel que indica em qual algarismo estamos

    repeat
        -- Regex que procura por vários numeros seguidos por 3 numeros e os separa em grupos
        -- Adiciona um '.' entre os grupos
        number, k = string.gsub(number, "^(-?%d+)(%d%d%d)", "%1.%2")
    until k == 0 -- Processou todos os algarismos

    return number -- Retorna a nova string processada
end

And then to use in your code:

local separatedVal = addSeparators(newVal)
hud.hud = HUD.New(Hudx+165, hud.posy, new .." (".. separatedVal .." gp)", textColor.r, textColor.g, textColor.b)
  • Francisco! Thanks for the information but adding it is not transforming the value.

  • @Thanatos probably you would have to change the if from above too: hud.hud:SetText(hud.count .." (".. addSeparators(GetValue(id) * hud.count) .." gp)")

  • Thank you my friend! I made the adjustment and was!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.