Page 1 of 1

Writing a value as text

Posted: Sat Jan 09, 2016 8:15 am
by Buby
Hey again NubbyBuby here!
I have another question!
How do i draw (or write) values for example:

Code: Select all

money = 10

function love.load()
   fontImg = love.graphics.newImage("assets/font.png")
   font = love.graphics.newImageFont( fontImg, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&''*#=[]")
   Money = love.graphics.newText(font, "Money:" ???)

function love.draw()
   love.graphics.draw(Money, 850, 60)
end


i left out love.update since i dont need it in this situation but how to i make it so the text says "Money: ...Value" or 10 as i set it. In an old engine i used i could just do "Money:" ..money.value.."" but this does not work?

Re: Writing a value as text

Posted: Sat Jan 09, 2016 11:30 am
by zorg
For one, not using text objects and just printing things makes it easier:

Code: Select all

money = 10
function love.draw()
   love.graphics.print(money, 850, 60)
end
If you want to use them though, then you can use either the concatenation operator (..) or the string.format function, like so:
(Uncomment the line you want to test/see how it works)

Code: Select all

money = 10
function love.load()
   fontImg = love.graphics.newImage("assets/font.png")
   font = love.graphics.newImageFont( fontImg, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&''*#=[]")
   Money = love.graphics.newText(font, "Money:" .. money) -- nice and simple; the conversion from number to string happens implicitly (automatically)
   -- Money = love.graphics.newText(font, string.format("Money: %d",money)) -- format replaces the %d token with a number argument, that we gave it though the money argument.
   -- Money = love.graphics.newText(font, ("Money: %d"):format(money)) -- this works the same as the one above, the string object gets passed to format as its first parameter.
end

Re: Writing a value as text

Posted: Sat Jan 09, 2016 2:03 pm
by Buby
zorg wrote:For one, not using text objects and just printing things makes it easier:

Code: Select all

money = 10
function love.draw()
   love.graphics.print(money, 850, 60)
end
If you want to use them though, then you can use either the concatenation operator (..) or the string.format function, like so:
(Uncomment the line you want to test/see how it works)

Code: Select all

money = 10
function love.load()
   fontImg = love.graphics.newImage("assets/font.png")
   font = love.graphics.newImageFont( fontImg, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&''*#=[]")
   Money = love.graphics.newText(font, "Money:" .. money) -- nice and simple; the conversion from number to string happens implicitly (automatically)
   -- Money = love.graphics.newText(font, string.format("Money: %d",money)) -- format replaces the %d token with a number argument, that we gave it though the money argument.
   -- Money = love.graphics.newText(font, ("Money: %d"):format(money)) -- this works the same as the one above, the string object gets passed to format as its first parameter.
end
Thanks! youre a life saver!