| |
|
Osyris
|
  |
| Joined: 27 Oct 2007 |
| Total Posts: 4321 |
|
|
| 07 Dec 2014 11:55 PM |
The concept of a constant is to be more memory efficient than a variable. Any hackery to make a 'constant' with Lua, would only result in less memory efficiency than just a variable.
It really isn't practical.
But I guess you could do stuff like constantly set the variable or make it a table and play with metamethods. |
|
|
| Report Abuse |
|
|
|
| 08 Dec 2014 12:07 AM |
| idc about efficency i want to make define() like there is in PHP so i guess ill be playing with metamethods than |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 08 Dec 2014 02:32 AM |
from a google search this is what I understand define() in php does, I don't know php so sorry if I'm wrong
This is more of an idea, because the way I did the 'case' function doesn't really work well so yah some combos are skipped
local function case(n, x, v) for y = #n, x, -1 do getfenv()[n:sub(1, y - 1) .. n:sub(y, y):upper() .. n:sub(y + 1)] = v end if #n > x then case(n:sub(1, x):upper() .. n:sub(x + 1), x + 1, v) end end
local function define(n, v, c) if c then getfenv()[n:lower()] = v case(n:lower(), 1, v) else getfenv()[n] - v --_G[n] = v in not roblox lua end end
define("test", "Hello world!", true) define("another", "Good bye!")
print(test) -->Hello world! print(TEst) -->Hello world! print(tEsT) -->nil, shouldn't be, but is because of the way I did it
print(another) -->Good bye! print(ANoThER) -->nil |
|
|
| Report Abuse |
|
|
ohno1112
|
  |
| Joined: 23 Mar 2013 |
| Total Posts: 833 |
|
|
| 08 Dec 2014 05:27 AM |
| or, just make a variable and don't change it at all. |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 08 Dec 2014 10:21 AM |
I just declare my constants in Lua as local variables at the start of the script, all uppercase.
If you really need constants, you can do trickery with the function environment and the __index + __newindex metamethods. This assumes that you mean a const like a Readonly value, that is. |
|
|
| Report Abuse |
|
|