Page 1 of 1
Calling other mods functions in the data stage
Posted: Wed Jan 24, 2024 4:17 pm
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.
Re: Calling other mods functions in the data stage
Posted: Wed Jan 24, 2024 4:55 pm
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.
Re: Calling other mods functions in the data stage
Posted: Wed Jan 24, 2024 5:51 pm
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:
Re: Calling other mods functions in the data stage
Posted: Wed Jan 24, 2024 6:50 pm
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.
Re: Calling other mods functions in the data stage
Posted: Wed Jan 24, 2024 9:04 pm
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:
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.
Re: Calling other mods functions in the data stage
Posted: Wed Jan 24, 2024 11:25 pm
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.
Re: Calling other mods functions in the data stage
Posted: Fri Mar 08, 2024 1:06 pm
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.
Re: Calling other mods functions in the data stage
Posted: Fri Mar 08, 2024 1:19 pm
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.