Run a module that can use everything that's in main.lua?

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
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Run a module that can use everything that's in main.lua?

Post by Eglaios »

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!
Currently working on game music...
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Run a module that can use everything that's in main.lua?

Post by pgimeno »

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.
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Run a module that can use everything that's in main.lua?

Post by Eglaios »

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...
Currently working on game music...
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Run a module that can use everything that's in main.lua?

Post by pgimeno »

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.
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Run a module that can use everything that's in main.lua?

Post by Eglaios »

Well, I guess I finally understand this (I may or may not have just realized that these were 2 different files... :ehem: )
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...
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Run a module that can use everything that's in main.lua?

Post by pgimeno »

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:

Code: Select all

local common = {}

function common.myfunc(args)
  ...
end
...

return common
Then your event files can require it:

Code: Select all

local common = require('common')

common.myfunc(...)
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Run a module that can use everything that's in main.lua?

Post by milon »

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

Re: Run a module that can use everything that's in main.lua?

Post by grump »

milon wrote: Wed Sep 22, 2021 4:17 pm 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.
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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 56 guests