Writing a value as text

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Buby
Prole
Posts: 15
Joined: Mon Dec 07, 2015 4:28 am

Writing a value as text

Post 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?

Code: Select all

if 2+2 == 5 then
   love.event.push('quit')
end
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Writing a value as text

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Buby
Prole
Posts: 15
Joined: Mon Dec 07, 2015 4:28 am

Re: Writing a value as text

Post 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!

Code: Select all

if 2+2 == 5 then
   love.event.push('quit')
end
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests