Calling other mods functions in the data stage
- BraveCaperCat
- Filter Inserter
- Posts: 430
- Joined: Mon Jan 15, 2024 10:10 pm
- Contact:
Calling other mods functions in the data stage
In my mod, i want other mods to be able to call a function i define in the data stage to create alot of related prototypes. However, i don't think mods can communicate with each other in the data stage, only in the control stage. I would like this ability to be added to the data stage to allow other mods to use my utility functions to create prototypes.
Creator of multiple mods, including Quality Assurance - My most popular one.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
Re: Calling other mods functions in the data stage
That's because there is no barrier in data stage. All mods are mashed together into the same Lua state. The docs tell you this, by the way. That's why you are able to call table.deepcopy etc. without requiring a base game lib: another mod did it for you. So you make a Lua global function/table of functions and tell people to call them.BraveCaperCat wrote: Wed Jan 24, 2024 4:17 pm However, i don't think mods can communicate with each other in the data stage, only in the control stage.
Re: Calling other mods functions in the data stage
The solution is in the docs:BraveCaperCat wrote: Wed Jan 24, 2024 4:17 pm In my mod, i want other mods to be able to call a function i define in the data stage to create alot of related prototypes. However, i don't think mods can communicate with each other in the data stage, only in the control stage. I would like this ability to be added to the data stage to allow other mods to use my utility functions to create prototypes.
https://lua-api.factorio.com/latest/index-prototype.html wrote:Main global objects:
mods :: Mods: A dictionary of active mods and their versions.
https://lua-api.factorio.com/latest/auxiliary/libraries.html#%E2%80%A2-require() wrote:Furthermore, it is possible to require files from other mods by using require("__mod-name__.file").
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: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: Calling other mods functions in the data stage
You can also define a variable as a global (ie, not use the local keyword), then define functions as elements of the global var. I did that with my Restrictions on Artificial Tiles mod. Just be sure to make it truly unique, like using your mod's name as part of the var.
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
- BraveCaperCat
- Filter Inserter
- Posts: 430
- Joined: Mon Jan 15, 2024 10:10 pm
- Contact:
Re: Calling other mods functions in the data stage
Last time i checked, each mod ALWAYS ran in a seperate instance of LUA. Table.deepcopy is possible, since when each mod writes to data.raw (or uses data:extend) the game automatically updates the data.raw table for all the mods loaded AFTER. I guess it has changed since i started making mods.curiosity wrote: Wed Jan 24, 2024 4:55 pmThat's because there is no barrier in data stage. All mods are mashed together into the same Lua state. The docs tell you this, by the way. That's why you are able to call table.deepcopy etc. without requiring a base game lib: another mod did it for you. So you make a Lua global function/table of functions and tell people to call them.BraveCaperCat wrote: Wed Jan 24, 2024 4:17 pm However, i don't think mods can communicate with each other in the data stage, only in the control stage.
I literally saw that second documentation after posting this, but i don't know where the delete button is.Qon wrote: Wed Jan 24, 2024 5:51 pmThe solution is in the docs:BraveCaperCat wrote: Wed Jan 24, 2024 4:17 pm In my mod, i want other mods to be able to call a function i define in the data stage to create alot of related prototypes. However, i don't think mods can communicate with each other in the data stage, only in the control stage. I would like this ability to be added to the data stage to allow other mods to use my utility functions to create prototypes.https://lua-api.factorio.com/latest/index-prototype.html wrote:Main global objects:
mods :: Mods: A dictionary of active mods and their versions.https://lua-api.factorio.com/latest/auxiliary/libraries.html#%E2%80%A2-require() wrote:Furthermore, it is possible to require files from other mods by using require("__mod-name__.file").
Ok. That would be like an Interface Variable containing various functions to interact between mods.FuryoftheStars wrote: Wed Jan 24, 2024 6:50 pm You can also define a variable as a global (ie, not use the local keyword), then define functions as elements of the global var. I did that with my Restrictions on Artificial Tiles mod. Just be sure to make it truly unique, like using your mod's name as part of the var.
Creator of multiple mods, including Quality Assurance - My most popular one.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: Calling other mods functions in the data stage
Sorry, replying from a phone so it's difficult to quote out the needed part. And for some reason I didn't think to put it like this before: pre-control stage, the different mods are not very well sandbox. While they are not able to access each other's local vars, they can access each other's globals (which is something that can't be done in control stage).
So while yes, I created an interface of sorts (but not really), I only did it that way to minimize my footprint in the global environment. I could have made each individual function global directly if I wanted to.
So while yes, I created an interface of sorts (but not really), I only did it that way to minimize my footprint in the global environment. I could have made each individual function global directly if I wanted to.
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
- BraveCaperCat
- Filter Inserter
- Posts: 430
- Joined: Mon Jan 15, 2024 10:10 pm
- Contact:
Re: Calling other mods functions in the data stage
Just in case some other mod adds a global function with a similar name to yours, you can still have your functions since you use a global variable of functions instead of global functions.FuryoftheStars wrote: Wed Jan 24, 2024 11:25 pm Sorry, replying from a phone so it's difficult to quote out the needed part. And for some reason I didn't think to put it like this before: pre-control stage, the different mods are not very well sandbox. While they are not able to access each other's local vars, they can access each other's globals (which is something that can't be done in control stage).
So while yes, I created an interface of sorts (but not really), I only did it that way to minimize my footprint in the global environment. I could have made each individual function global directly if I wanted to.
Creator of multiple mods, including Quality Assurance - My most popular one.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: Calling other mods functions in the data stage
Pretty much, yes.BraveCaperCat wrote: Fri Mar 08, 2024 1:06 pmJust in case some other mod adds a global function with a similar name to yours, you can still have your functions since you use a global variable of functions instead of global functions.FuryoftheStars wrote: Wed Jan 24, 2024 11:25 pm Sorry, replying from a phone so it's difficult to quote out the needed part. And for some reason I didn't think to put it like this before: pre-control stage, the different mods are not very well sandbox. While they are not able to access each other's local vars, they can access each other's globals (which is something that can't be done in control stage).
So while yes, I created an interface of sorts (but not really), I only did it that way to minimize my footprint in the global environment. I could have made each individual function global directly if I wanted to.
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