How to store functions in "global" table?

Place to post guides, observations, things related to modding that are not mods themselves.
vadcx
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Apr 19, 2024 4:12 pm
Contact:

How to store functions in "global" table?

Post by vadcx »

If I understand Factorio modding guides correctly, it's disallowed to store Lua functions inside the "global" table? (not to be confused with Lua's global variable scope!) I am combing through Biter Battles code (descendant of Comfy code base) and what I see there is a spiced spaghetti. Now I like spaghetti/pasta in real life, but when seen in code, it makes my brain go Windows 95 maze.

The event handling registration there is kind of dynamic in nature, even though I think it's not used 99% of the time for that feature. This makes it opaque to me how the event queue is handled though, the table with multiple handlers per event type is passed around like a shopping cart (and other metaphors :arrow: ). And because this table with functions cannot be stored once and for all inside "global", it must be created from code on every load, that in turn makes heavy use of upvalues, "Tokens" to generate consistent indexes on load.

This all seems like a diagonal sushi bus base with spaghetti builds on the side. I think storing not just data but functions too in "global" would greatly help to reduce the complexity. Is there a way though? As far as I understand, Factorio will error when you try to save a function in "global" because the serpent library cannot serialize functions.
Maybe ChatGPT can help?
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2768
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: How to store functions in "global" table?

Post by FuryoftheStars »

I would first maybe ask why Biter Battles is doing things the way it is and is there a better way. :D
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
vadcx
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Apr 19, 2024 4:12 pm
Contact:

Re: How to store functions in "global" table?

Post by vadcx »

:D Sure I would try to refactor it at ever greater scale once I fully grasp the current organically evolved "architecture" and simplify it a little bit.
Still, for mods that have dynamic (at runtime) event de/registration I think this is the same. That kind of use-case is convenient for closure functions (i.e. functions created with dynamic data at runtime) but "global"... oh global.
Qon
Smart Inserter
Smart Inserter
Posts: 2164
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: How to store functions in "global" table?

Post by Qon »

vadcx wrote: Thu May 02, 2024 2:29 pmAnd because this table with functions cannot be stored once and for all inside "global", it must be created from code on every load, that in turn makes heavy use of upvalues, "Tokens" to generate consistent indexes on load.
Sounds like you don't even have an issue, you just look at a code base that you don't understand and start firing off random suggestions for the API. Have you written any Factorio mods yet? I'm not sure storing functions in global would help, seems it would just make things more confusing.
vadcx wrote: Thu May 02, 2024 2:29 pm Maybe ChatGPT can help?
That's a bizarre way to end a post. The answer is no, but I'm not going to stop you from trying. Why are you even here if ChatGPT is an option?
My mods: Capsule Ammo | HandyHands - Automatic handcrafting | ChunkyChunks - Configurable Gridlines
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
vadcx
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Apr 19, 2024 4:12 pm
Contact:

Re: How to store functions in "global" table?

Post by vadcx »

This is really not a suggestion for the API. Rather I would like to know if someone has solved it or works around this peculiarity. Otherwise I would have posted in the "Modding interface requests" subforum. I am talking to ChatGPT as we speak.
Qon
Smart Inserter
Smart Inserter
Posts: 2164
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: How to store functions in "global" table?

Post by Qon »

vadcx wrote: Thu May 02, 2024 3:20 pm This is really not a suggestion for the API. Rather I would like to know if someone has solved it or works around this peculiarity.
The way people do it already works, and it is thus a solved "problem" already. There's not really any peculiarity, just your assumptions that were not on point.
My mods: Capsule Ammo | HandyHands - Automatic handcrafting | ChunkyChunks - Configurable Gridlines
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2768
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: How to store functions in "global" table?

Post by FuryoftheStars »

vadcx wrote: Thu May 02, 2024 2:56 pm :D Sure I would try to refactor it at ever greater scale once I fully grasp the current organically evolved "architecture" and simplify it a little bit.
Still, for mods that have dynamic (at runtime) event de/registration I think this is the same. That kind of use-case is convenient for closure functions (i.e. functions created with dynamic data at runtime) but "global"... oh global.
I'm not sure I follow. Dynamic event (de)registration should not require storing the whole function into global? At most, you store its current registration status (in case you have multiple points in your code that could cause it to (de)register for different reasons, otherwise not even this is needed) and any values that it's working with that persist beyond a single tick (edit: which you would need to do regardless if you could store the function in global, and storing the function in global would not solve, anyway). I'll admit, I'm not that great of a coder, but I'm struggling to find any reason to store a whole function in global beyond custom (at runtime) written functions, which seems way far above and beyond anything that needs to be done in Factorio....
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
Post Reply

Return to “Modding discussion”