Jump to content

[Mod help] Sanity Perks


Recommended Posts

Hello,

 

Thank you for stumbling upon this thread stranger!

 

I would like to add a sanity perk to my character but I don't know how to write code very we so here goes. The perks work like this;

She will have a small sanity gain when holding a bird and a slightly smaller gain when holding a feather. They are and simple but I do not know how to check for an item in an inventory.

 

Thank you for your help kind stranger!

Link to comment
Share on other sites

@PaladinPlayer, I think putting this in the master_postinit for your character should do the trick:

local birds = {"robin", "robin_winter", "crow"}local function sanityfn(inst)	local hasbird = false	local hasfeather = false	for k,v in pairs(birds) do		if inst.components.inventory:Has(v) then			hasbird = true		end		if inst.components.inventory:Has("feather_"..v) then			hasfeather = true		end	end	return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)endlocal function master_postinit(inst)    inst.components.sanity.custom_rate_fn = sanityfn

I showed the master_postinit in there because it's better to have the sanityfn outside of it.

 

I made some assumptions about how you wanted this to work, here:

  • Having multiple birds does not give you any additional sanity
  • Having a bird negates any additional sanity from having feathers

There's probably a more efficient way to do this (something like listening for birds/feathers entering/leaving your inventory), but this should work, at least.

Link to comment
Share on other sites

@PaladinPlayer, I think putting this in the master_postinit for your character should do the trick:

local birds = {"robin", "robin_winter", "crow"}local function sanityfn(inst)	local hasbird = false	local hasfeather = false	for k,v in pairs(birds) do		if inst.components.inventory:Has(v) then			hasbird = true		end		if inst.components.inventory:Has("feather_"..v) then			hasfeather = true		end	end	return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)endlocal function master_postinit(inst)    inst.components.sanity.custom_rate_fn = sanityfn

I showed the master_postinit in there because it's better to have the sanityfn outside of it.

 

I made some assumptions about how you wanted this to work, here:

  • Having multiple birds does not give you any additional sanity
  • Having a bird negates any additional sanity from having feathers

There's probably a more efficient way to do this (something like listening for birds/feathers entering/leaving your inventory), but this should work, at least.

 Hi Rez!

 

Thanks (again) for the code it was very helpful since it worked perfectly!

Also while I look through more Modding Help pages I see your posts are always helpful which is awesome!

Link to comment
Share on other sites

@rezecib

 

First of all I assume the crash log is in documents/klei/donotstarvetogether/log.txt

 

When sanityfn is here:

local birds = {"robin", "robin_winter", "crow"}

local function sanityfn(inst)
    local hasbird = false
    local hasfeather = false
    for k,v in pairs(birds) do
        if inst.components.inventory:Has(v) then
            hasbird = true
        end
        if inst.components.inventory:Has("feather_"..v) then
            hasfeather = true
        end
    end
    return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)
end
inst.components.sanity.custom_rate_fn = sanityfn
end
 
I get this crash: post-506972-0-48730100-1422139562_thumb.
and this log:log.txt
 
But when sanityfn is placed outside of master_postinit:
local birds = {"robin", "robin_winter", "crow"}
local function sanityfn(inst)
    local hasbird = false
    local hasfeather = false
    for k,v in pairs(birds) do
        if inst.components.inventory:Has(v) then
            hasbird = true
        end
        if inst.components.inventory:Has("feather_"..v) then
            hasfeather = true
        end
    end
    return hasbird and TUNING.DAPPERNESS_SMALL or (hasfeather and TUNING.DAPPERNESS_TINY or 0)
end
end
inst.components.sanity.custom_rate_fn = sanityfn
 
It will not load past this screen: post-506972-0-35018000-1422140774_thumb.
and the log is:log.txt
 
P.S. Thanks for the help and sorry for the long post
 
 
 
Link to comment
Share on other sites

@PaladinPlayer, Make a foodtype for the eggs and add it to her foodprefs. There are a ton of posts around on how to do that.

 

Hmm... As for birds/feathers, it should be working the way it is, I think, but we can try rewriting the "return" line as a few lines instead:

local delta = 0if hasfeather then delta = TUNING.DAPPERNESS_TINY endif hasbird then delta = TUNING.DAPPERNESS_SMALL end

But it may be working already, with the difference just not noticeable. Try changing the values, e.g. the SMALL to HUGE.

Link to comment
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
 Share

×
  • Create New...