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?
How to store functions in "global" table?
-
- Smart Inserter
- Posts: 2768
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: How to store functions in "global" table?
I would first maybe ask why Biter Battles is doing things the way it is and is there a better way.
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
Re: How to store functions in "global" table?
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.
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?
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.
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
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
Re: How to store functions in "global" table?
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?
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
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
-
- Smart Inserter
- Posts: 2768
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: How to store functions in "global" table?
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....vadcx wrote: ↑Thu May 02, 2024 2:56 pmSure 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.
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