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).
Constants
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Constants
I just use variables, and I recommend you to do the same.
When I write def I mean function.
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Constants
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.
Code: Select all
MAXHEIGHT = 24
- slime
- Solid Snayke
- Posts: 3170
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Constants
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.
True constants don't make as much sense in an interpreted language compared to one which pre-compiles code.
Re: Constants
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 )
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Constants
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: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 )
Code: Select all
player.x = player.x + S.MAX_SPEED * dt
Help us help you: attach a .love.
Re: Constants
Good point.Robin wrote: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: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 )
Code: Select all
player.x = player.x + S.MAX_SPEED * dt
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:
Code: Select all
Sprite.speed = 64
Who is online
Users browsing this forum: Bing [Bot], pgimeno, Semrush [Bot] and 12 guests