Community Forum

How can I overwrite an appended function?

Forum Overview >> Scripting

CategoryScripting
Created18.09.2017 23:05


Tim Derks (timmiej93) 18.09.2017 23:05
Say I want to overwrite the 'setUnitFillLevel' function of the baler specification.
This function is appended to the main setUnitFillLevel function of the baler, as you can see on line 65 in the load function of the baler.

So basically, you've got the following setup:
self.setUnitFillLevel (main)
--> Baler.setUnitFillLevel (appended)

If I simply overwrite self.setUnitFillLevel, the main method is overwritten. If I overwrite Baler.setUnitFillLevel, I found the result being quite unstable. Sometimes it works, sometimes it doesn't. What's the best way to overwrite the appended function?

I personally though of overwriting the load function, where the appended function is appended, and append the other, proper function instead.

Emil Drefers (Unknown) 19.09.2017 07:06
Hi,

overwriting a function can also be done like this:

local originalBalerSetUnitFillLevel = Baler.setUnitFillLevel;

Baler.setUnitFillLevel = function()
...
end

But watch the function arguments.

Cheers,
Emil

Tim Derks (timmiej93) 19.09.2017 21:16
Would that have the same effect as the section below, or would that be different?

Baler.setUnitFillLevel = Utils.overwrittenFunction(.., ...)

Emil Drefers (Unknown) 26.09.2017 09:56
Hi,

that would have a different effect.

Your approach delivers the overwritten fuction to the overwriting function ...

function MySpecialization:load()
self.functionA = Utils.overwrittenFunction(self.functionA, MySpecialization.functionA)
end

function MySpecialization:functionA(superFunc, ...)
-- superFunc is a reference to the overwritten function
-- therefore it is motsly called
superFunc(self, ...);
end

So, this approach is somewhat cleaner than my previously mentioned approach.

Cheers,
Emil

Tim Derks (timmiej93) 26.09.2017 11:25
Thank you for your reply Emil!


Note: Log in to post. Create a new account here.