Page 1 of 1

Today I learnt...

Posted: Mon Jun 06, 2022 4:54 pm
by Gunroar:Cannon()
... that a lua file has a limit of 200 local variables?

Code: Select all

Error

toybox/LGML.lua:125: Syntax error: hunt_TMP:201: main function has more than 200 local variables



Traceback

[C]: in function 'load'
toybox/LGML.lua:125: in function 'getSettingsToSave'
toybox/LGML.lua:165: in function 'saveData'
hunt/GameScreen.lua:1346: in function 'loadOverworld'
hunt/GameScreen.lua:1201: in function 'exitPlace'
hunt/Player.lua:393: in function 'action'
toybox/libs/chrono.lua:38: in function 'update'
toybox/entities/Room.lua:50: in function '__step'
toybox/entities/Game.lua:59: in function '__step'
toybox/LGML.lua:225: in function 'update'
[C]: in function 'xpcall'
Any fix for this? (This is the way I make save files.)

Re: Today I learnt...

Posted: Mon Jun 06, 2022 6:03 pm
by MrFariator
Yep, lua has a hard-coded limit of 200 local variables within a given scope, or at least within a function. If you wrap some of those variables into a table, you can fit in more, because a table can hold many values, while only "consuming" one local.

Usually I think that reaching this limit is a code smell.

Re: Today I learnt...

Posted: Mon Jun 06, 2022 6:47 pm
by Gunroar:Cannon()
Ahhh, heheh.

I use a lib called saveTable that saves the game data (nothing bulky, just states and triggers) in a lua file. And wince it was/is a "small" game and bitser seemed not to work on empty files or something, I used that and it's worked up to this point :P.

I guess maybe I'll try to really get bitser to work. :P :rofl:

Re: Today I learnt...

Posted: Thu Jun 09, 2022 12:31 pm
by Gunroar:Cannon()
Something sinister here...how about if I make the lib write the tables in the save file as global instead of local ...just as a quick hack (for now) :P :rofl: ?

Re: Today I learnt...

Posted: Thu Jun 09, 2022 12:47 pm
by darkfrei
The saving and loading must be such easy as

Code: Select all

function on_save ()
  love.save ("data", Data) -- big letter means for global
end

Code: Select all

function on_load ()
  Data = love.load ("data")
end
Where Data is a table with tables and values, no metatables, no userdata, no functions or methods.