Still on my project with tilemaps (using STI) :
I have multiple maps, each one has various events that can be triggered through interactions in this map.
For each map, there's a dir containing 2 files : map data and events module (functions table). Both are required when the map is selected.
When I load a map (from main.lua), I want the events module to use most (all) variables that are parts of main.lua.
Is there a "clean" way that would allow the events module to have access to everything in main.lua? (other than globals and writing the module directly in main.lua)
Thank you!
Run a module that can use everything that's in main.lua?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Run a module that can use everything that's in main.lua?
Currently working on game music...
Re: Run a module that can use everything that's in main.lua?
Well, there's debug.getlocal and debug.getupvalue, but I really don't recommend going there. I suggest you to put all those variables in a table, and then pass that table to the events.
I did that with Thrust II Reloaded; check how game.lua handles game.state if you want a tip. The game.state table contains every variable used by the game itself that is not generated on the fly, aka the state of the game at any point in time; this allows me to provide save/load functionality to the user, by serializing/de-serializing that table. I guess you can use something very similar for your events.
I did that with Thrust II Reloaded; check how game.lua handles game.state if you want a tip. The game.state table contains every variable used by the game itself that is not generated on the fly, aka the state of the game at any point in time; this allows me to provide save/load functionality to the user, by serializing/de-serializing that table. I guess you can use something very similar for your events.
Re: Run a module that can use everything that's in main.lua?
In fact, would dkjson be able to copy paste the functions table from the events module into main.lua in such a way I could use it in the main environment, as if it was originally written into main.lua?
I tried to encode my event tables with that, but it says functions are not supported...
I tried to encode my event tables with that, but it says functions are not supported...
Currently working on game music...
Re: Run a module that can use everything that's in main.lua?
dkjson is only used in T2R to serialize the table to file. You don't need that; you can pass the functions in fields of the table too. The functions don't need to be saved to file.
See also what I do with the `main` table. It also contains functions, e.g. main.deepcopy which is defined in https://notabug.org/pgimeno/Thrust-II-r ... n.lua#L100 and invoked in https://notabug.org/pgimeno/Thrust-II-r ... e.lua#L258
I declare it as a global, but I wouldn't do it that way if I rewrote it.
See also what I do with the `main` table. It also contains functions, e.g. main.deepcopy which is defined in https://notabug.org/pgimeno/Thrust-II-r ... n.lua#L100 and invoked in https://notabug.org/pgimeno/Thrust-II-r ... e.lua#L258
I declare it as a global, but I wouldn't do it that way if I rewrote it.
Re: Run a module that can use everything that's in main.lua?
Well, I guess I finally understand this (I may or may not have just realized that these were 2 different files... )
So there's the main data stored as a global, then game.lua copies it into local to avoid abusing of globals and allow saves... Now I got it!
So I'd have to store my map data as global, then copy it as local vars into events module so it doesn't abuse of globals...
Now it sounds good to me... I wanted to avoid globals, but I guess I shouldn't fear them as much as I currently do
Thank you for your example!
I'll keep that deepcopy function in mind, it could probably be useful at some point!
So there's the main data stored as a global, then game.lua copies it into local to avoid abusing of globals and allow saves... Now I got it!
So I'd have to store my map data as global, then copy it as local vars into events module so it doesn't abuse of globals...
Now it sounds good to me... I wanted to avoid globals, but I guess I shouldn't fear them as much as I currently do
Thank you for your example!
I'll keep that deepcopy function in mind, it could probably be useful at some point!
Currently working on game music...
Re: Run a module that can use everything that's in main.lua?
It can be done without globals (and I should have chosen that way when I wrote it). Rather than main.lua, place your functions in a different file:
common.lua:
Then your event files can require it:
common.lua:
Code: Select all
local common = {}
function common.myfunc(args)
...
end
...
return common
Code: Select all
local common = require('common')
common.myfunc(...)
Re: Run a module that can use everything that's in main.lua?
Side question: What's the advantage to putting everything in common rather than as globals defined in main.lua? I know it's generally bad practice, but I thought it didn't matter (as much?) in Lua.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Re: Run a module that can use everything that's in main.lua?
You don't put everything in there, only the stuff that makes sense to be global because it is, in fact, a global resource, and should only exist once. Having a lot of globals that are accessed and modified in a lot of places increases complexity, and more complexity means more mental load and more bugs that are harder to fix. There's the danger of name collisions. Excessive use of globals makes it much harder to understand what the code is doing. This is especially true if you work in a team, or if you come back to older code after a while.
The concept has nothing to do with Lua, it applies to any programming language. There is one thing though that's special with globals in Lua and that's that accessing them is a bit slower than locals because they have to be fetched by name from a table.
Who is online
Users browsing this forum: Google [Bot] and 189 guests