Sign in to follow this  
BillTheCipher

[Solved] Inconsistent coding with sanity auras

Recommended Posts

Hey there!

I've encountered with a weird coding issue today. I've wanted to add my custom character some sanity boost if it stands near a butterfly, a rabbit or a gobbler. I've found this base code, I've added my character's name and the creture names and I've added it to my modmain.lua:

AddPrefabPostInit("butterfly", function(inst)
        if GetPlayer().prefab == "spinel" then
            inst:AddComponent("sanityaura")
            inst.components.sanityaura.aura = TUNING.SANITYAURA_SMALL
        end
    end)

AddPrefabPostInit("perd", function(inst)
        if GetPlayer().prefab == "spinel" then
            inst:AddComponent("sanityaura")
            inst.components.sanityaura.aura = TUNING.SANITYAURA_SMALL
        end
    end)
    
AddPrefabPostInit("rabbit", function(inst)
        if GetPlayer().prefab == "spinel" then
            inst:AddComponent("sanityaura")
            inst.components.sanityaura.aura = TUNING.SANITYAURA_SMALL
        end
    end)

 

The issue is, it works perfectly with the butterfly and the gobbler, but when I add the rabbit line too, the game crashes after character selection. I've tried it with some other creatures and items too, they also worked well, the rabbit one is the only problematic one.

 

Is there anything I can do?

Edited by BillTheCipher

Share this post


Link to post
Share on other sites
Cleyven    11

Hi =D
 

If you send the crash log we might be able to help you better. 
Even if you don't see a crash in your screen (sometimes the game closes straight away) the crash will be logged in the log file. 

In case you are not aware, the log file I'm talking about can be found sometimes in your documents>Klei>DoNotStarveTogether.
it's the client_log.txt file. 

Cheers =D

Share this post


Link to post
Share on other sites

I've tried the same code for pigman, but it also caused a crash: 

image.thumb.png.7a14875c01abd74aad8a23a5c751528b.png

Is it because rabbits and pigmen have a spawning structure? I've tried the same code with bunnyman, it doesn't caused a crash after character selection, but I didn't tried it in the caves tho.

Edited by BillTheCipher

Share this post


Link to post
Share on other sites

I've also tried this code:

 

-- MY CUSTOM CODE!
-- This value is the highest amount of sanity that a single entity can give per second.
local maxSanityRateBonusPerEntity = TUNING.SANITYAURA_TINY
-- This function is hooked up to the sanity component, to add a custom rate-based
-- change to sanity for this particular character.
local function sanityfn(inst)
    local delta = 0
    local x, y, z = inst.Transform:GetWorldPosition()
    local max_rad = 10
    local ents = TheSim:FindEntities(x, y, z, max_rad, {
        "bee", "flower", "butterfly", "rabbit", "koalefant_summer",
        "koalefant_winter", "chester", "squid", "glommer", "hutch", "mole"
        })
    
    for i, v in ipairs(ents) do
        -- Determine whether the entity is dead, which they can only be if they have a health-component.
        -- Otherwise, we default to them not being seen as dead.
        local isDead = v.components.health ~= nil and v.components.health:IsDead() or false
        local distsq = inst:GetDistanceSqToInst(v) - 9
        -- Add the maximum sanity rate divided by distance, so you get more sanity the closer
        -- you are to the entities, but cap it so distance cannot be lower than 1,
        -- because dividing by e.g. 0.2 would multiply the value by 5 :)
        -- And at the end there, if the entity is dead, it makes the character sad instead.
        delta = delta + maxSanityRateBonusPerEntity / math.max(1, distsq) * (isDead and -1 or 1)
    end
    return delta
end

 

I've found it here:https://forums.kleientertainment.com/forums/topic/114771-solved-sanity-aura-code-crashing/page/3/

They said the code I've previously tried to use affects every player in the server, I only want it on my character.

This code however does nothing in my game. It doesn't cause crash, but doesn't improve the sanity either.

Here's my current prefab file:

 

spinel.lua

Edited by BillTheCipher

Share this post


Link to post
Share on other sites
IThatGuyI    34

I downloaded PicklePlayers mod Kaji because his character has a fear of water and he loses sanity when near it. I checked his code and heres what you can use:

local function PositiveSanity(inst, observer)
	if observer.prefab == "wilson" then -- using wilson prefab but you can use your character here
		return TUNING.SANITYAURA_SMALL -- sanity gain
	end
	
	return 0
end

AddPrefabPostInit("butterfly", function(inst) -- I checked all of these and it works
	inst:AddComponent("sanityaura")
	inst.components.sanityaura.aurafn = PositiveSanity
end)

AddPrefabPostInit("perd", function(inst) -- I checked all of these and it works
	inst:AddComponent("sanityaura")
	inst.components.sanityaura.aurafn = PositiveSanity
end)

AddPrefabPostInit("rabbit", function(inst) -- I checked all of these and it works
	inst:AddComponent("sanityaura")
	inst.components.sanityaura.aurafn = PositiveSanity
end)

All you have to do is put this code in your modmain.lua file. You don't need GetPlayer = GLOBAL.GetPlayer for this.

If that's not exactly what you want, tell me.

Share this post


Link to post
Share on other sites
On 6/20/2020 at 10:51 AM, BillTheCipher said:

@IThatGuyI

Much-much thanks pal :]

It works just as I wanted and if I see it right, it only affects my character. You're a hero!

just a thing to note, while your issue is solved, there is one thing in common with the creatures you were crashing on. they all have alternate "forms" beardlings for rabbits, werepigs for pigmen, i'd recommend testing how your currently-working scripting interacts with those alternate versions. maybe even make additional penalties for them

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this