Hi guys!
I apologize if this is known knowledge, and I am about to proudly proclaim advice everyone has been following for years…
but I have read a few places things along the lines of “If you modify a lua function in your mod, you are going to run into compatibility issues with other mods that modify that function” as well as mods having a compatibility section in their descriptions saying “Hey, I have overridden this method so I will break if used with another mod that does that”
However, you can use a technique I picked up from my RPGMaker days which they called “Aliasing”.
For example, I wanted in my Kai Monkey’s Traits mod to have traits be randomized the customization options are randomized. I could do this by overriding the method as follows:
function GameCreationService:_randomize_citizen(citizen, pop, gender, locked_options)
locked_options = locked_options or {}
if not locked_options.name then
pop:set_citizen_name(citizen, gender)
end
-- pop:regenerate_stats(citizen, { embarking = true }) -- COMMENT THIS OUT
local cc = citizen:get_component('stonehearth:customization')
cc:regenerate_appearance(locked_options.customizations)
pop:regenerate_stats(citizen, { embarking = true }) -- AND MOVE IT DOWN HERE
end
However this has all the compatibility issues discussed above. Instead I did the following
local old_randomize_citizen = GameCreationService._randomize_citizen
function GameCreationService:_randomize_citizen(citizen, pop, gender, locked_options)
-- Do anything I want to inject before the method does it's normal thing
old_randomize_citizen(self, citizen, pop, gender, locked_options) -- NOTE the "self" as the first parameter
-- Do anything I want to inject after the method does it's normal thing, in this case re-rerandomize the traits
pop:regenerate_stats(citizen, { embarking = true })
end
This means I can proudly announce my mod knowing that if there are any compatibility issues they are not my fault. If everyone uses this method as often as they can, we will reduce this issue greatly!
This method will only ever work when you’re injecting stuff into the code base, and will not let you (easily) remove logic. However, it is worth noting that you can do fun things with modifying the parameters the old method is called with, as well as injecting logic before and after.
Of course once it gets to actually having multiple mods working together the results may become strange and unexpected, (depending on which order the lua is executec) however a lot of the time its much much better to have them work strangely than not at all.
I can write up an example of this if someone would like