Calling other mods functions in the data stage

Place to get help with not working mods / modding interface.
Post Reply
BraveCaperCat
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Mon Jan 15, 2024 10:10 pm
Contact:

Calling other mods functions in the data stage

Post by BraveCaperCat »

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.

curiosity
Filter Inserter
Filter Inserter
Posts: 325
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: Calling other mods functions in the data stage

Post by curiosity »

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.
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.

Qon
Smart Inserter
Smart Inserter
Posts: 2118
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Calling other mods functions in the data stage

Post by Qon »

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.
The solution is in the docs:
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").

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2549
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Calling other mods functions in the data stage

Post by FuryoftheStars »

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

BraveCaperCat
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Mon Jan 15, 2024 10:10 pm
Contact:

Re: Calling other mods functions in the data stage

Post by BraveCaperCat »

curiosity wrote:
Wed Jan 24, 2024 4:55 pm
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.
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.
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.
Qon wrote:
Wed Jan 24, 2024 5:51 pm
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.
The solution is in the docs:
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").
I literally saw that second documentation after posting this, but i don't know where the delete button is.
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.
Ok. That would be like an Interface Variable containing various functions to interact between mods.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2549
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Calling other mods functions in the data stage

Post by FuryoftheStars »

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

BraveCaperCat
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Mon Jan 15, 2024 10:10 pm
Contact:

Re: Calling other mods functions in the data stage

Post by BraveCaperCat »

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.
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
Smart Inserter
Smart Inserter
Posts: 2549
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Calling other mods functions in the data stage

Post by FuryoftheStars »

BraveCaperCat wrote:
Fri Mar 08, 2024 1:06 pm
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.
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.
Pretty much, yes.
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

Post Reply

Return to “Modding help”