Making Place Value in lua?

Hello there,…i have a problem in my current project…i am making now a scoreboard that was using PNG images(numbers from 0-9) in my game…currently, i assign a variable to hold the current score of the player in the game…and i want the value of that assigned variable to be reflected by the PNG images(numbers) that will be seen on my game…for example:

player_score = 5041
in-game score = 005041 (using PNG numbers)

I got an idea of using place value for the player_score so that the numbers will be divided from ones - hundred thousands and that will be the game will read individually and reflect by the PNG numbers:

0 = hundred thousands
0 = ten thousands
5 = thousands
0 = hundreds
4 = tens
1 = ones

But the problem is i don’t know how to implement it…i tried to search, read and understands everything and applied it on my game project but no luck…if somebody knows how to make this please help me…thanks!

Hi @Jhei_Krauzer

First of all, I believe today there is support for custom bitmap fonts in case you would like to have you’re own bitmap font created and used inside the game so there is not really a need for creating a more complex system for showing textured numbers.
Check out http://www.defold.com/manuals/font/#_bitmap_bmfonts if interested.

Now anyhow if you would like to continue on the path you are on:
Make sure you are building up your hud in a gui-scene.
Also make sure that the pngs are all in an atlas that is added to the gui-scene.

I havent tested the code but I would do something like this in the gui script.

local function update_score(score)
	-- convert to string
	if score < 10000 then
		score = "00"..tostring(score)
	elseif score < 100000 then
		score = "0"..tostring(score)
	else
		score = tostring(score)
	end
	
	-- This function assumes you have score nodes that are named: score_char_1, score_char_2 .... score_char_6
	-- It also assumes that the pngs in the atlas are named digit_0, digit_1, digit_2 ... digit_9
	for i = 1, #str do
    	local c = str:sub(i,i)
    	gui.play_flipbook(gui.get_node("score_char_"..i), "image_"..c)
	end
end

Hope it helps you on the way.
/A

1 Like