[Solved] Lua constant

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
Kavaline
Prole
Posts: 8
Joined: Sun Jun 27, 2021 5:19 pm

[Solved] Lua constant

Post by Kavaline »

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? :)
Last edited by Kavaline on Fri Jul 23, 2021 1:03 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Lua constant

Post by darkfrei »

How about just not change vars?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Lua constant

Post by pgimeno »

No, I don't think it will be added in future. But you can use for example LuaPreprocess.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Lua constant

Post by ReFreezed »

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."
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Lua constant

Post by grump »

For integer constants you can do this in LuaJIT:

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
This works for integers only though; no other types are supported.
Kavaline
Prole
Posts: 8
Joined: Sun Jun 27, 2021 5:19 pm

Re: Lua constant

Post by Kavaline »

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.
This is really nice for other purposes!
ReFreezed wrote: Mon Jul 12, 2021 5:06 pm 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.
Good to know this one, specially for debug. I saw some features from updated versions, due to this I suggest the constant too :)
grump wrote: Mon Jul 12, 2021 5:08 pm For integer constants you can do this in LuaJIT:
I'm developing a client-server, this will be useful, thanks! With strings have nothing like this?
RNavega
Party member
Posts: 239
Joined: Sun Aug 16, 2020 1:28 pm

Re: Lua constant

Post by RNavega »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests