PDA

View Full Version : Calling the original function in an overridden function.



StoryWeaver
December 13th, 2019, 20:02
I'm trying to figure out how to call an original function later in the code, that has been overriden/redirected in the onInit() function.


The onInit looks as follows:



function onInit()
EffectManager.addEffect = addEffectDuplicateCheck;
end


Then in the overridden function after some functionality has taken place, I then want to call the original function again, given certain conditions.

Such as in addEffectDuplicateCheck:



if (bFoundDuplicate == false) then
EffectManager.addEffect = EffectManager.addEffect; -- attempt at redirecting function back to its original
EffectManager.addEffect(sUser, sIdentity, nodeCT, rNewEffect, bShowMsg);
end

The problem I'm having is that I'm not sure how to call the original EffectManager function again, after the custom code has ran.
Currently this functionality will put FG into and endless loop, as the function is calling itself continually.

Is there a way to do this?

I'd rather not copy the about 90 lines of code from the original EffectsManager.addEffect function, and rewrite it to work in the new .lua.
As that would be rather messy coding and harder to maintain.

Trenloe
December 13th, 2019, 20:57
If you override a function in a global script package then you've overridden it. I don't believe the "super" reference for layered scripts works with global packages.

But, the EffectManager.addEffect function has two custom function calls already built in: fCustomOnEffectAddStart and fCustomOnEffectAddEnd which are specifically there to allow developers to tie in code into that function without having to modify the base function itself. You can set the code for these functions through EffectManager.setCustomOnEffectAddStart(f) and EffectManager.fCustomOnEffectAddEnd(f) where f is a function within your custom code. You'll need to look at the EffectManager.addEffect to see where these run and what you can do with these custom functions.

Andraax
December 14th, 2019, 00:21
I'm trying to figure out how to call an original function later in the code, that has been overriden/redirected in the onInit() function.

...snip...

Is there a way to do this?

I'd rather not copy the about 90 lines of code from the original EffectsManager.addEffect function, and rewrite it to work in the new .lua.
As that would be rather messy coding and harder to maintain.

Did you try this?



function onInit()
savedFunction = EffectManager.addEffect;
EffectManager.addEffect = addEffectDuplicateCheck;
end


Then later:



if (bFoundDuplicate == false) then
savedFunction(sUser, sIdentity, nodeCT, rNewEffect, bShowMsg);
end

StoryWeaver
December 14th, 2019, 01:14
Thank you Andraax, that worked!

I had tried something similar as a global constant, which didn't work. Putting it in the onInit() function however saved the original function into memory correctly. :)

Trenloe
December 14th, 2019, 08:54
Sorry, I misunderstood what you were trying to do. Glad Andraax sorted you out! :-)