. . . Posted October 17, 2019 Share Posted October 17, 2019 Hello, I need a little help with something I can't figure out by myself ! Basically, I want to edit the wormhole.lua is "OnActivate" function and edit the sanity drain to be multiplied. Spoiler if doer.components.sanity ~= nil then doer.components.sanity:DoDelta(-TUNING.SANITY_MED * (doer.wormholefear or 1)) end I was wondering if there's an easy way to do this or should I just copy paste the wormhole.lua and make my changes or replace the "inst.components.teleporter.onActivate = OnActivate" with a prefabpostinit? thanks for any help, have a great time ! Link to comment Share on other sites More sharing options...
Serpens Posted October 17, 2019 Share Posted October 17, 2019 (edited) never replace a file (only in 0.01% of the cases this is okay). Yes, "replace" the Onactivate, but save the original one. Lets say the defaul sanity malus is -10. Then you simply execute the original function and afterwards substract additional 10 -> value doubled. So no need to copy anyhting. Edited October 17, 2019 by Serpens Link to comment Share on other sites More sharing options...
. . . Posted October 17, 2019 Author Share Posted October 17, 2019 14 hours ago, Serpens said: but save the original one. Lets say the defaul sanity malus is -10. Then you simply execute the original function and afterwards substract additional 10 -> value doubled. Do you have an example for how to save the original function or something? I don't really know how to do that and kinda lost on what I should do Link to comment Share on other sites More sharing options...
CarlZalph Posted October 17, 2019 Share Posted October 17, 2019 8 minutes ago, Warbucks said: Do you have an example for how to save the original function or something? I don't really know how to do that and kinda lost on what I should do -- Given function somefunc(a, b, c) print(a,b,c) return b end -- Test1 print("_Test 1_") print(somefunc("test", "ing", 1, 2, 3)) -- Do local oldfunc = somefunc somefunc = function(arg1, arg2, ...) arg1 = "prehook changes arguments!" local retval = {oldfunc(arg1, arg2, ...)} retval = {"posthook changes return values!"} return table.unpack(retval) end -- Test2 print("_Test 2_") print(somefunc("test", "ing", 1, 2, 3)) For DST, table.unpack may just be unpack. Output: _Test 1_ test ing 1 ing _Test 2_ prehook changes arguments! ing 1 posthook changes return values! Link to comment Share on other sites More sharing options...
. . . Posted October 17, 2019 Author Share Posted October 17, 2019 I have no idea what I'm looking at to be honest, but thank you ! Link to comment Share on other sites More sharing options...
Serpens Posted October 18, 2019 Share Posted October 18, 2019 (edited) for the wormhole you simply do an AddPrefabPostinit. And you know that OnActive is saved within inst with "inst.OnActivate" in the wormhole code. This way you can access the function. So you do first: local old_OnActivate = inst.OnActivate -- this will save the original function then you can overwrite:inst.OnActivate = function(...) -- overwrite it, put the original parameters in the brackets -- your new code return old_OnActivate(...) end Of course you can add some security checks, eg. chekcing with "if old_OnActivate~=nil" if and old fucntion did exist (maybe it was removed by devs or another modder) before you execute it. And you can add to the paramaters ",...". So lets assume the OnActavte functio only has OnActivate(inst) as argument/parameter. Then you can change it to (inst,...), same when calling the old function within your new one. This will make sure, that it still will work, even if the devs or anyone else is adding additional parameters to the function. If you still have no clue, look at my recent ~20 posts in this forum. I posted alot of code recently where I saved and called old functions. Edited October 18, 2019 by Serpens Link to comment Share on other sites More sharing options...
Hornete Posted October 18, 2019 Share Posted October 18, 2019 19 hours ago, Serpens said: for the wormhole you simply do an AddPrefabPostinit. And you know that OnActive is saved within inst with "inst.OnActivate" in the wormhole code. This way you can access the function. So you do first: local old_OnActivate = inst.OnActivate -- this will save the original function then you can overwrite:inst.OnActivate = function(...) -- overwrite it, put the original parameters in the brackets -- your new code return old_OnActivate(...) end Of course you can add some security checks, eg. chekcing with "if old_OnActivate~=nil" if and old fucntion did exist (maybe it was removed by devs or another modder) before you execute it. And you can add to the paramaters ",...". So lets assume the OnActavte functio only has OnActivate(inst) as argument/parameter. Then you can change it to (inst,...), same when calling the old function within your new one. This will make sure, that it still will work, even if the devs or anyone else is adding additional parameters to the function. If you still have no clue, look at my recent ~20 posts in this forum. I posted alot of code recently where I saved and called old functions. I don't see a inst.OnActivate? instead you should take the component function inst.components.teleporter.onActivate = OnActivate I suppose you meant that? Link to comment Share on other sites More sharing options...
Serpens Posted October 18, 2019 Share Posted October 18, 2019 2 hours ago, Hornete said: I don't see a inst.OnActivate? instead you should take the component function inst.components.teleporter.onActivate = OnActivate I suppose you meant that? yes, of course, I did not take a look at wormhole code, it was just a general example. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now