PDA

View Full Version : Add to a class' onInit() - replace / merge?



zuilin
November 21st, 2020, 15:33
If I want to add functionality to the end of a class' onInit() function, do I have to completely replace the onInit() and add my code to the end?

Is there any way to effectively merge it? That is to say, have the original onInit() run, then have my additional function run right afterwards?

Thanks.

Talyn
November 21st, 2020, 16:17
This might at least get you going in the right direction:



function onInit()
-- ensure the CoreRPG scripts load
if super and super.onInit then
super.onInit()
end
-- new code goes here
end


I use this in one of the windowclasses in a ruleset I work on so that I only have to maintain my code and not have to duplicate the CoreRPG scripts (and maintain them every time they get updated).

zuilin
November 21st, 2020, 16:30
This might at least get you going in the right direction:



function onInit()
-- ensure the CoreRPG scripts load
if super and super.onInit then
super.onInit()
end
-- new code goes here
end


I use this in one of the windowclasses in a ruleset I work on so that I only have to maintain my code and not have to duplicate the CoreRPG scripts (and maintain them every time they get updated).

That is exactly what I was looking for. Thanks!

zuilin
November 21st, 2020, 17:13
Now let's say that script, the one with the original onInit() has a local variable declared at the top of the script like so:


local nSomeVariable;

Is there any way for my code to set/get it?

Talyn
November 21st, 2020, 20:09
No clue, I'm too new at the Lua side of things to have a grip on how the layering works, but my (extremely limited) understanding is that local variables/tables only exist within the functions they're defined in. If you go poking around the CoreRPG + ruleset scripts you'll find a lot of similar things being defined in each script so I'm guessing it'd be easier to just replicate the one you're wanting in your own function to make sure you're getting the correct value.

damned
November 21st, 2020, 22:45
Im pretty sure local is local...