Searching about constants in lua, I found this link:
https://stackoverflow.com/questions/123 ... ob-with-it
Here was told that in lua 5.4, we can use constants without tricks. Is possile to add this to LÖVE in future?
[Solved] Lua constant
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] Lua constant
Last edited by Kavaline on Fri Jul 23, 2021 1:03 pm, edited 1 time in total.
Re: Lua constant
No, I don't think it will be added in future. But you can use for example LuaPreprocess.
Re: Lua constant
Whether Lua 5.4 style constants will be supported in LÖVE in the future depends on if the feature gets added to LuaJIT. There are some post-Lua 5.1 features that have been added to LuaJIT (like 'goto') so who knows.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: Lua constant
For integer constants you can do this in LuaJIT:
This works for integers only though; no other types are supported.
Code: Select all
local ffi = require('ffi')
local MyEnum = ffi.typeof([[
struct {
enum {
CONST_A = 23,
CONST_B = 1337,
CONST_C = 42,
};
}
]])
print(MyEnum.CONST_B) -- 1337
MyEnum.CONST_A = 1 -- error: attempt to write to constant location
Re: Lua constant
This is really nice for other purposes!pgimeno wrote: ↑Mon Jul 12, 2021 10:49 am No, I don't think it will be added in future. But you can use for example LuaPreprocess.
Good to know this one, specially for debug. I saw some features from updated versions, due to this I suggest the constant too
I'm developing a client-server, this will be useful, thanks! With strings have nothing like this?
Re: Lua constant
You can use the __newindex metamethod of a metatable to implement read-only (aka constant) tables that can have strings / int values etc, like this: https://www.lua.org/pil/13.4.5.html
What makes them read-only is the fact that if you try to modify a value or add a key, they throw an error during runtime.
What makes them read-only is the fact that if you try to modify a value or add a key, they throw an error during runtime.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests