|
| 23 Jul 2015 01:53 AM |
In other words, variables that can't be changed. In C, you can declare a constant integer by doing:
const int myVar = 0; |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Jul 2015 01:57 AM |
| Shame, I cringe a little bit when I hard code in numbers, but I'll get over it. |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:02 AM |
Some people do
local MYVAR = 0
That's how they know it's constant
No implemented thing in Lua for it tho. |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:03 AM |
Good point bosswalrus. I'll title my constant variables like so
_MYVAR
With all caps and an underscore before it. |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:05 AM |
| ew constant typing of underscores |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:06 AM |
| eh, maybe i won't use underscores. i normally only ever use underscores in C++ when i'm declaring private variables. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 23 Jul 2015 02:12 AM |
Just use globals then.
thing = 0
"Talk is cheap. Show me the code." - Linus Torvalds |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:15 AM |
You can make read-only tables!
do local t = { FLORIDA = 'state', 'AFGHANISTAN' = country } local Constants = setmetatable( { }, { __index = t, __newindex = error( 'attempt to write to const value', 2 ) } ) end |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:16 AM |
| "'AFGHANISTAN' = country" should be "AFGHANISTAN = 'country'" |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:17 AM |
also __newindex should be set to function() error( 'attempt to write to const value', 2 ) end.
Sorry I'm tired and don't proofread enough |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 23 Jul 2015 02:18 AM |
| What's the point of making a variable that will never change? Variable literally means that it VARIES. |
|
|
| Report Abuse |
|
|
| |
|
murcury57
|
  |
| Joined: 30 Jun 2010 |
| Total Posts: 90299 |
|
| |
|
|
| 23 Jul 2015 02:28 AM |
@murcury57 wow, that actually works, i don't know why, i don't know how, but that helps a lot, thanks. |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 23 Jul 2015 02:33 AM |
| That's interesting that the __newindex metamethod fires when changing the value of the variable without invilving tab[index] = value .. I didn't expect that to work. Noice job mate. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 23 Jul 2015 02:36 AM |
What a useless thread.
"Talk is cheap. Show me the code." - Linus Torvalds |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2015 02:53 AM |
@TimeTicks Oh, shut up already. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 23 Jul 2015 02:55 AM |
Like I said, making a constant variable in Lua is the most useless thing I have ever seen. You shut up already.
"Talk is cheap. Show me the code." - Linus Torvalds |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jul 2015 03:50 AM |
If you name your constants by uppercase, you'd already know not to change it. I don't see why you would need to go so complicated and alter the env... |
|
|
| Report Abuse |
|
|