Constants

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
rvj
Prole
Posts: 15
Joined: Fri Feb 10, 2012 5:55 pm

Constants

Post 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).
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Constants

Post by kikito »

I just use variables, and I recommend you to do the same.
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Constants

Post by TechnoCat »

A convention is to declare "constants" in all caps.

Code: Select all

MAXHEIGHT = 24
They aren't truly constant, but you can see that they are intended to be used as one.
User avatar
slime
Solid Snayke
Posts: 3170
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Constants

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Constants

Post 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 )
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Constants

Post 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
Help us help you: attach a .love.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Constants

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

Code: Select all

Sprite.speed = 64
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).
Post Reply

Who is online

Users browsing this forum: Bing [Bot], pgimeno, Semrush [Bot] and 12 guests