Hi everyone,
I'm sorry if it is a dumb question but in the game I'm making I want to do a level counter but I don't know how to format an int to a string.
Thanks for any advice you have
Int to String
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- CanadianGamer
- Party member
- Posts: 132
- Joined: Tue Jun 30, 2015 1:23 pm
- Location: Canada
- Contact:
Int to String
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
Re: Int to String
tostring(number)
- CanadianGamer
- Party member
- Posts: 132
- Joined: Tue Jun 30, 2015 1:23 pm
- Location: Canada
- Contact:
Re: Int to String
thanks
My serious itch.io page:
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
https://pentamonium-studios.itch.io/
My less serious itch.io page:
http://canadiangamer.itch.io
Re: Int to String
Conversely, you can do tonumber( string ) to check if a string is a valid number; something far better than my previous (and very hack-ish) method:
Code: Select all
function isNumber( str )
if pcall( function() return str + 0 end ) then
return str + 0
end
return false
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Int to String
isnt [manual]tonumber[/manual] far better?davisdude wrote:-snip-
Also 5.3 return ints and floats in different formats when using [manual]tostring[/manual] so you may prefere [manual]string.format[/manual] and some numeral format like %i
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Int to String
Positive07 wrote:isnt tonumber far better?
davisdude wrote:; something far better than my previous (and very hack-ish) method: -snip-
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Int to String
I read the exact opposite, sorry hahahaNixola wrote:-snip-
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Int to String
Hello,davisdude wrote:Conversely, you can do tonumber( string ) to check if a string is a valid number; something far better than my previous (and very hack-ish) method:Code: Select all
function isNumber( str ) if pcall( function() return str + 0 end ) then return str + 0 end return false end
You code is ulgy and be able to break the check with metatable/__add meta handler.
Code: Select all
local hack=10
print(isNumber(hack)) -- print 10, as expected
local hack="hack"
print(isNumber(hack)) -- print false, as expected
local mt = {}
local hack = setmetatable({}, mt)
mt.__add = function() return hack end
mt.__tostring = function() return "p0wn3d" end
print(isNumber(hack)) -- print p0wn3d, instead of false
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Int to String
Nixola wrote:Positive07 wrote:isnt tonumber far better?davisdude wrote:; something far better than my previous (and very hack-ish) method: -snip-
Reading is difficult!TsT wrote:You code is ulgy and be able to break the check with metatable/__add meta handler.
Re: Int to String
It's the ulgy troothbartbes wrote:Reading is difficult!
Who is online
Users browsing this forum: Bing [Bot] and 2 guests