Page 1 of 1
Constants
Posted: Sat Mar 24, 2012 6:18 pm
by rvj
Does anyone else find it really annoying that there are no constants in Lua? Or does anyone have a way of using them?
I know you can just declare a global variable instead. I also appreciate that lua is a scripting language, so using a little extra memory for variables that should be constants is no big deal. However it still bothers me to the point where I find myself adding a lot of 'magic numbers' (numbers that should be declared constants, but just up with little explanation in the code).
So my two questions:
1. does anyone have a program or something to pre-process a script for constants?
2. does the lack of constants bother everyone else like it does me? because I imagine it would be a fairly simple matter to add some pre-processing to LOVE for them (I guess lua purists might take offense to that though).
Re: Constants
Posted: Sat Mar 24, 2012 6:30 pm
by kikito
I just use variables, and I recommend you to do the same.
Re: Constants
Posted: Sat Mar 24, 2012 6:43 pm
by TechnoCat
A convention is to declare "constants" in all caps.
They aren't truly constant, but you can see that they are intended to be used as one.
Re: Constants
Posted: Sat Mar 24, 2012 7:53 pm
by slime
It should be fairly simple to put your constants in a table with a metatable that prevents any changes, but as TechnoCat said if you have them in all caps (or some other clear convention), then it's hard to change them by accident at runtime anyway.
True constants don't make as much sense in an interpreted language compared to one which pre-compiles code.
Re: Constants
Posted: Sun Mar 25, 2012 12:56 am
by Inny
The Lua way is to use strings as constant. For instance, with love.graphics.rectangle, you can specify "fill" or "line". If you hate using quotes, however, you can always abuse a metatable to produce a symbol like behavior:
Code: Select all
S = setmetatable({},{__index=function(_,k) return k end})
print( S.fill, S.line )
Re: Constants
Posted: Sun Mar 25, 2012 8:29 am
by Robin
Inny wrote:The Lua way is to use strings as constant. For instance, with love.graphics.rectangle, you can specify "fill" or "line". If you hate using quotes, however, you can always abuse a metatable to produce a symbol like behavior:
Code: Select all
S = setmetatable({},{__index=function(_,k) return k end})
print( S.fill, S.line )
That is for enum-like behavior. I think the OP is talking about MAX_SPEED, and things like that. It doesn't work if you do something like:
Code: Select all
player.x = player.x + S.MAX_SPEED * dt
Re: Constants
Posted: Sun Mar 25, 2012 5:48 pm
by Inny
Robin wrote:Inny wrote:The Lua way is to use strings as constant. For instance, with love.graphics.rectangle, you can specify "fill" or "line". If you hate using quotes, however, you can always abuse a metatable to produce a symbol like behavior:
Code: Select all
S = setmetatable({},{__index=function(_,k) return k end})
print( S.fill, S.line )
That is for enum-like behavior. I think the OP is talking about MAX_SPEED, and things like that. It doesn't work if you do something like:
Code: Select all
player.x = player.x + S.MAX_SPEED * dt
Good point.
In that case, my usual convention is to make the constant a class member of where I use it. So, lets say the regular speeds of sprites is 64 pixels a second, then as part of the sprite class I do this:
All sprites and sprite subclasses would use the self.speed to access it, or even override it for their own purposes (like faster or slower moving enemies).