Page 1 of 1
How to store functions in "global" table?
Posted: Thu May 02, 2024 2:29 pm
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
). 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?
Re: How to store functions in "global" table?
Posted: Thu May 02, 2024 2:48 pm
by FuryoftheStars
I would first maybe ask
why Biter Battles is doing things the way it is and is there a better way.
Re: How to store functions in "global" table?
Posted: Thu May 02, 2024 2:56 pm
by vadcx
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.
Re: How to store functions in "global" table?
Posted: Thu May 02, 2024 3:14 pm
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?
Re: How to store functions in "global" table?
Posted: Thu May 02, 2024 3:20 pm
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.
Re: How to store functions in "global" table?
Posted: Thu May 02, 2024 3:28 pm
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.
Re: How to store functions in "global" table?
Posted: Thu May 02, 2024 3:30 pm
by FuryoftheStars
vadcx wrote: ↑Thu May 02, 2024 2:56 pm
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....