GreggZumbari1 Posted July 10, 2017 Share Posted July 10, 2017 Hello, it's me again. The answer to this problem is most likely very simple, but alas, I am still quite new to the world of LUA and modding. So I am making a character that gains sanity when near players, but loses it when near butterflies. I created two individual local functions for the two different effects. Here is the code for each: local function playersanfn(inst) --For gaining sanity near players local x, y, z = inst.Transform:GetWorldPosition() local delta = 0 local ents = TheSim:FindEntities(x, y, z, 30, {"player"}) for k, v in pairs(ents) do if v ~= inst then local bonus_sanity = TUNING.SANITYAURA_LARGE local distsq = math.max(inst:GetDistanceSqToInst(v), 1) delta = delta + bonus_sanity / distsq end end return delta end local function butterflysanfn(inst) --For losing sanity near evil butterfly demons local x, y, z = inst.Transform:GetWorldPosition() local delta = 0 local ents = TheSim:FindEntities(x, y, z, 30, {"butterfly"}) for k, v in pairs(ents) do if v ~= inst then local bonus_sanity = TUNING.SANITYAURA_LARGE local distsq = math.max(inst:GetDistanceSqToInst(v), 1) delta = (delta + bonus_sanity / distsq) * -1 end end return delta end And they both work just fine, but I can only use one at a time by setting inst.components.sanity.custom_rate_fn to either: inst.components.sanity.custom_rate_fn = playersanfn OR inst.components.sanity.custom_rate_fn = butterflysanfn So my question is, how do I make them both interact with inst.components.sanity.custom_rate, so that my player can gain sanity from players and lose sanity from butterflies at the same time? I've already tried: inst.components.sanity.custom_rate_fn = playersanfn + butterflysanfn but my game crashes before I can load into the game. To be more clear as to what occured, I got past the loading screen, but the second that it finished and my character was in the world, I crashed. I've also tried creating a separate local function, called totalsanityeffectfn, whose purpose is to switch in between the above functions (butterflysanfn and playersanfn) depending on whether or not there is a player around: local function totalsanityeffectfn(inst) local x, y, z = inst.Transform:GetWorldPosition() local delta = 0 local ents = TheSim:FindEntities(x, y, z, 30, {"player"}) for k, v in pairs(ents) do if v ~= inst then local totalsanityeffectfn = sanityfn else local totalsanityeffectfn = butterflyfn end end return delta end inst.components.sanity.custom_rate_fn = totalsanityeffectfn but then neither playersanfn nor butterflysanfn work, and I neither gain nor lose sanity from players or butterflies. (but the game doesn't crash, which is a plus) Previously, a user on these forums helped me learn how sanity aura works. Their name is DarkXero. I just thought i'd give credit where it's due. I would be grateful for any help offered. Thanks! And let me know if I can clarify anything. Link to comment Share on other sites More sharing options...
alainmcd Posted July 10, 2017 Share Posted July 10, 2017 local function sanityfn(inst) local x, y, z = inst.Transform:GetWorldPosition() local delta = 0 local ents = TheSim:FindEntities(x, y, z, 30, {"player", "butterfly"}) for k, v in pairs(ents) do if v ~= inst then local bonus_sanity = TUNING.SANITYAURA_LARGE local distsq = math.max(inst:GetDistanceSqToInst(v), 1) delta = (delta + bonus_sanity / distsq) * (v:HasTag("butterfly") and -1 or 1) end end return delta end -- inst.components.sanity.custom_rate_fn = sanityfn Perhaps? Link to comment Share on other sites More sharing options...
GreggZumbari1 Posted July 11, 2017 Author Share Posted July 11, 2017 Nope, it doesn't work, @alainmcd The game didn't crash, but no sanity changes occured near butterflies or players. However, I do appreciate that you are helping me at all, so thank you! Link to comment Share on other sites More sharing options...
alainmcd Posted July 11, 2017 Share Posted July 11, 2017 This works: local function sanityfn(inst) local x, y, z = inst.Transform:GetWorldPosition() local delta = 0 local ents = TheSim:FindEntities(x, y, z, 30, nil, nil, {"player", "butterfly"}) for k, v in pairs(ents) do if v ~= inst then local bonus_sanity = TUNING.SANITYAURA_LARGE local distsq = math.max(inst:GetDistanceSqToInst(v), 1) delta = delta + bonus_sanity / distsq * (v:HasTag("butterfly") and -1 or 1) end end return delta end The fifth, sixth and seventh arguments to FindEntities are tags to look for if provided (AND, NOT and OR respectively), I mixed up the fifth and the seventh, so it was looking for entities with the "player" AND "butterfly" tags. Also, this line in your butterflysanfn: delta = (delta + bonus_sanity / distsq) * -1 probably doesn't work as you intended, your cummulative delta is multiplied by -1, so if there's an even number of butterflies within reach, you receive a positive sanity bonus. Fixed in the code above. Link to comment Share on other sites More sharing options...
GreggZumbari1 Posted July 12, 2017 Author Share Posted July 12, 2017 Yup! Works perfectly. Thank you so much, @alainmcd! 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