Best practice for overriding a local function
DaanVanYperen
The Netherlands Join Date: 2013-06-16 Member: 185580Members, NS2 Playtester, Squad Five Blue, Reinforced - Shadow
in Modding
Working on the Skulks WIth Shotguns mod, going great so far. I'm new to lua and ns2 modding though so having some trouble with the basics.
What would you suggest to be the best method for overriding a local function?
Specifically AssignPlayerToEgg, in AlienTeam.lua.
My best solution is overriding AlienTeam:Update (which calls AssignPlayerToEgg) in a mod file, but that leads to a lot of local functions having to end up in my mod file as well. I figure I might be overlooking some basic method of achieving just a single local function override.
Thanks!
What would you suggest to be the best method for overriding a local function?
Specifically AssignPlayerToEgg, in AlienTeam.lua.
My best solution is overriding AlienTeam:Update (which calls AssignPlayerToEgg) in a mod file, but that leads to a lot of local functions having to end up in my mod file as well. I figure I might be overlooking some basic method of achieving just a single local function override.
Thanks!
Comments
If you could supply more info, I am sure we can help
Oh and good luck with the programming, Howser told me you had finally bitten the NS2 bullet. As you have programming experience, you should find this lua coding pretty easy
I saw there is a way of injecting local variables into a class method, is there some way to inject local functions? (Specifically AssignPlayerToEgg into AlienTeam:Update).
EDIT:
Oh, didnt read carefully enough and missed that you were talkign about *local* functions.
Class_ReplaceMethod obviously replaces a class-method, not local ones. Person8880 points to the correct function to use in your case (further down this page).
Edit: Ok i memorized the function of ReplaceLocals( Function, TableOfLocals ) in a wrong way. Person8880 is totally right.
So in your case,
If you're really using the modding framework, then your files are almost certainly mixed vm scopes. So, to make it easier, just wrap a "if Client" or "if Server" , etc. conditional block around the ReplaceLocals call.
Iin my case, I needed to replace local function AssignPlayerToEgg, called by local function updateAlienSpectators, called by class method AlienTeam.Update.
Added the function GetLocalFunction to get a reference to updateAlienSpectators
After that just a question of calling ReplaceLocals.
Not sure this is the best way, but it works for me!
Hmm GetLocalFunction reminds me of Shine.GetUpValue. But BTT yes this is probably the same way i would do it.
I saw you post about Shine before. I'm simply too fresh in the whole modding process to be knowledgeable about the optional frameworks out there. One week in, still finding my way around Lua and vanilla NS2 code. I already have enough rope to hang myself with
Don't get me wrong. Some of the mods out there have been great study material. Xenoswarm's code for example is elegantly written and easy to learn from. It just addresses really common issues, I wouldn't have known what to search for with the upvalue fetching thing.