fixerhere Posted November 7, 2020 Share Posted November 7, 2020 I'm probably so close to finishing my character mod but still there is one thing I want to do but don't understand I want evil flowers to be like normal flowers (having same food stats, having same aura and when picking up getting +5 sanity) On the other hand i want normal flowers to be like evil flowers (having food stats like evil flowers, having negative aura, when picking up -5 sanity) So is it even possible that my character would be only affected? Link to comment Share on other sites More sharing options...
Thomas Die Posted November 7, 2020 Share Posted November 7, 2020 (edited) local function CalcSanityAura(inst, observer) return (observer.prefab == "character_name" and inst.prefab == "flower_evil") and 0 or (observer.prefab == "character_name" and inst.prefab == "flower") and -TUNING.SANITYAURA_SMALL or inst.components.sanityaura.aura end local function FlowerButPlanted (inst) if not GLOBAL.TheWorld.ismastersim then return inst end if not inst.components.sanityaura then inst:AddComponent("sanityaura") end inst.components.sanityaura.aurafn = CalcSanityAura if inst.components.pickable then inst._onpickedfn = inst.components.pickable.onpickedfn inst.components.pickable.onpickedfn = function(inst, picker, loot) print(picker) if picker and picker.prefab == "character_name" then --code if picker.components.sanity ~= nil and inst.prefab == "flower_evil" then picker.components.sanity:DoDelta(TUNING.SANITY_TINY) elseif picker.components.sanity ~= nil and inst.prefab == "flower" then picker.components.sanity:DoDelta(-TUNING.SANITY_TINY) end else inst._onpickedfn(inst, picker, loot) end --else inst._onpickedfn(inst, picker) inst:Remove() end end end AddPrefabPostInit("flower_evil", FlowerButPlanted) AddPrefabPostInit("flower", FlowerButPlanted) This should cover the code for: >sanity aura >picking sanity delta this should work but i haven't tested it yet But basically I've or should've replaced the functions components for the flowers specifically but this may not work well with mods that change the flowers as well please tell me if my code doesn't work with the log in modmain btwme explaining for no reason self.onpickedfn(self.inst, picker, loot) this is the function in the pick able component, we have 3 parameters :(self.inst, picker, loot) we still use this function but we change it to only apply to our character but we save the original function for others for the code we're gonna use we only use the first 2 --1) saving the origina function inst._onpickedfn = inst.components.pickable.onpickedfn inst.components.pickable.onpickedfn = function(inst, picker) --2) function if picker.prefab == "character_name" then --3) code specific for character else inst._onpickedfn(inst, picker) --4) other characters end end 1) we first save the original function as: >inst._onpickedfn 2) but then replace it with our own: >inst.components.pickable.onpickedfn = function(inst, picker)3) Add an if .. else statement to make sure the code only applies to the custom charaacter: >if picker.prefab == "character_name" then4) If the character is not yours then it should re-introduce the original function that we saved: >else inst._onpickedfn(inst,picker) function Combat:GetAttacked(attacker, damage, weapon, stimuli) --orignal code inst.components.combat.GetAttacked = function(self,attacker, damage, weapon, stimuli) --from my code I think sometimes you might need to add parameter "self" for some functions because idk but i think it's referenced in the originals code's functionbt dubs if anyone knows there's a link explaining this can you send it me so i can just link it next time Edited November 7, 2020 by thomas4846 I oofed up Link to comment Share on other sites More sharing options...
fixerhere Posted November 7, 2020 Author Share Posted November 7, 2020 I tried the code but when i created a new world it just stopped at "Generating world" Also no error so i don't know what could have happened Link to comment Share on other sites More sharing options...
Thomas Die Posted November 7, 2020 Share Posted November 7, 2020 goddamn it gimme a sec i was so proud 1 Link to comment Share on other sites More sharing options...
fixerhere Posted November 7, 2020 Author Share Posted November 7, 2020 Don't worry I'm really thankful that you even spent your time writing this code Take your time 1 Link to comment Share on other sites More sharing options...
Thomas Die Posted November 7, 2020 Share Posted November 7, 2020 (edited) local function CalcSanityAura(inst, observer) return (observer.prefab == "wilson" and inst.prefab == "flower_evil") and 0 or (observer.prefab == "wilson" and inst.prefab == "flower") and -TUNING.SANITYAURA_SMALL or inst.components.sanityaura.aura end local function FlowerButPlanted (inst) if not GLOBAL.TheWorld.ismastersim then return inst end if not inst.components.sanityaura then inst:AddComponent("sanityaura") end inst.components.sanityaura.aurafn = CalcSanityAura if inst.components.pickable then inst._onpickedfn = inst.components.pickable.onpickedfn inst.components.pickable.onpickedfn = function(inst, picker, loot) print(picker) if picker and picker.prefab == "wilson" then --code if picker.components.sanity ~= nil and inst.prefab == "flower_evil" then picker.components.sanity:DoDelta(TUNING.SANITY_TINY) elseif picker.components.sanity ~= nil and inst.prefab == "flower" then picker.components.sanity:DoDelta(-TUNING.SANITY_TINY) end else inst._onpickedfn(inst, picker, loot) end --else inst._onpickedfn(inst, picker) inst:Remove() end end end AddPrefabPostInit("flower_evil", FlowerButPlanted) AddPrefabPostInit("flower", FlowerButPlanted) @fixerhere Okay sorry but i've changed the names to wilson but jsut change it to your characters and we'll all be dandyThe problems: >functions were after addprefabpostinit >else was too far down >calculate aura didn't make normal flowers bad >didn't remove flowers this should be fixed now I'm gonna replace the original code Edited November 7, 2020 by thomas4846 Link to comment Share on other sites More sharing options...
fixerhere Posted November 7, 2020 Author Share Posted November 7, 2020 Everything works well I mean there is a graphical bug it says that when picking up evil flower it gives -5 sanity (don't worry it gives +5 sanity) other than that everything works well now I only need to change food stat values for flower and evil flower Link to comment Share on other sites More sharing options...
penguin0616 Posted November 7, 2020 Share Posted November 7, 2020 @thomas4846 "self" is a reference to the object that the function was called in. When you call a function with : the self is passed in automatically. When you call a function with . you need to pass in the self This stuff only really matters in object oriented programming Here's a page explaining it: https://www.lua.org/pil/16.html 1 1 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