Page 1 of 1
How do I override a base function?
Posted: Tue Aug 16, 2016 7:23 pm
by ARS
Pulling my hair out at my failure to find an answer to this anywhere. So fundamental a programming question in a game that allows programmed mods seems like it should be answered *everywhere*, but I can't figure it out.
In the file base/prototypes/entity/demo_enemy_autoplace.lua, there is the function
Code: Select all
function enemy_autoplace(distance, order, is_turret)
I want to override this function with my mod. How do I do that? I have tried simply creating a function with the same signature in data.lua for a mod, but it is never called. It seems the base function is called instead.
If there is, somehow, no way to override a function... how do I change biter autoplace settings? Since they are embedded in a function, I can't access them through data.raw like I would for altering an entity, right?
Thank you!
Re: How do I override a base function?
Posted: Tue Aug 16, 2016 7:28 pm
by prg
Have a look at how that function is used. It's getting called at data load time and its result is assigned to the autoplace setting. So you don't need to change that function at all, you can just change the setting via data.raw like normal.
Re: How do I override a base function?
Posted: Tue Aug 16, 2016 7:38 pm
by ARS
Thanks for the hasty reply, that's what I'm looking at now. However, it is used in about 5 different places, it seems quite silly to muck around with the specific outcomes of the function call than to merely change the function, and the feature I'm looking for strikes me as rather fundamental.
Is there truly no way to override the behavior of a function? Like, what if the function was defined in a mod instead of in base? Could I override it then? Not being able to do so seems... crazy... unbelievable, even!
EDIT: I suppose I can just use data.raw to replace entirely the result of the base function with a call to the new function, instead of changing each property one at a time. Still seems rather roundabout to achieve a result that is so fundamental to what functions are in programming.
Re: How do I override a base function?
Posted: Tue Aug 16, 2016 7:47 pm
by prg
The file defining the function is required at the beginning of the file using it, the function is then called later in that file. I don't see much opportunity for changing it inbetween.
Re: How do I override a base function?
Posted: Tue Aug 16, 2016 7:53 pm
by ARS
Okay, that makes sense. Kinda sound more like macros than functions then. Can you call functions from in-game? I'm guessing no?
Re: How do I override a base function?
Posted: Tue Aug 16, 2016 7:56 pm
by prg
Re: How do I override a base function?
Posted: Tue Aug 16, 2016 8:03 pm
by ARS
Okay, right, but you have to register them first. That's the key. I was thinking of them as... sort of entities of their own. I'm not super familiar with LUA, I come from Java/Scala/C#/etc. I was thinking of the whole top level as a single scope that would load all the functions prior to calling them--in which case later definitions (in the mod load list) of the same function signature would override earlier ones before they ever had a chance to be called. I modded another game that did this, though I can't remember what it was.
Thanks for your help!