Jump to content

[FIXED] Inactivity = Insanity


Recommended Posts

I'm currently modding a football player character who gains sanity by working such as cutting trees, mining rocks, he loves to be productive.

So if he sits around a while, he starts to lose sanity.

I don't think there's really a component or tag to make the prefab "listen" for. Or is there? 

@PeterA ((I don't know if you can confirm if there is))

If there is none, how would I go about tackling this? I don't wanna do like... Listen for "working" component and give a sanity boost and if the person isn't then drop their sanity, because I feel that constant drain would be detrimental to play, and not fun to play with.

Edited by DextersComicLaboratory
This has been resolved, it can be closed.
Link to comment
Share on other sites

Replace instances of "UniqueModNameHere" with something unique to allow higher mod compatibility.

This'll make it so that chopping/mining/picking anything resets the timer.

Default 10 seconds before sanity penalty applies of -10 sanity/second rate.

local function ResetTimer(inst)
    inst.DrainSanityRate_UniqueModNameHere = nil
    if(inst.DrainSanityTimer_UniqueModNameHere~=nil)
    then
        inst.DrainSanityTimer_UniqueModNameHere:Cancel()
        inst.DrainSanityTimer_UniqueModNameHere = nil
    end
    inst.DrainSanityTimer_UniqueModNameHere = inst:DoTaskInTime(10,
        function()
            inst.DrainSanityRate_UniqueModNameHere = -10
            inst.DrainSanityTimer_UniqueModNameHere = nil
        end
    )
end
local function ResetTimer_Event(inst, data)
    ResetTimer(inst)
end
AddPrefabPostInit("wilson",
    function(inst)
        if(inst.components~=nil and inst.components.sanity~=nil)
        then
            ResetTimer(inst)
            local custom_rate_fn_old = inst.components.sanity.custom_rate_fn
            inst.components.sanity.custom_rate_fn = function(inst)
                local deltaRate = (inst.DrainSanityRate_UniqueModNameHere or 0)
                if(custom_rate_fn_old~=nil)
                then
                    deltaRate = deltaRate + custom_rate_fn_old(inst)
                end
                return deltaRate
            end
            inst:ListenForEvent("working", ResetTimer_Event)
            inst:ListenForEvent("finishedwork", ResetTimer_Event)
            inst:ListenForEvent("picksomething", ResetTimer_Event)
        end
    end
)

 

Edited by CarlZalph
Link to comment
Share on other sites

3 hours ago, CarlZalph said:

Replace instances of "UniqueModNameHere" with something unique to allow higher mod compatibility.

This'll make it so that chopping/mining/picking anything resets the timer.

Default 10 seconds before sanity penalty applies of -10 sanity/second rate.

Spoiler

 

 

local function ResetTimer(inst)
    inst.DrainSanityRate_Buster = nil
    if(inst.DrainSanityTimer_Buster~=nil)
    then
        inst.DrainSanityTimer_Buster:Cancel()
        inst.DrainSanityTimer_Buster = nil
    end
    inst.DrainSanityTimer_Buster = inst:DoTaskInTime(10,
        function()
            inst.DrainSanityRate_Buster = -10
            inst.DrainSanityTimer_Buster = nil
        end
    )
end
local function ResetTimer_Event(inst, data)
    ResetTimer(inst)
end
AddPrefabPostInit("wilson"
    function(inst)
        if(inst.components~=nil and inst.components.sanity~=nil)
        then
            ResetTimer(inst)
            local custom_rate_fn_old = inst.components.sanity.custom_rate_fn
            inst.components.sanity.custom_rate_fn = function(inst)
                local deltaRate = (inst.DrainSanityRate_Buster or 0)
                if(custom_rate_fn_old~=nil)
                then
                    deltaRate = deltaRate + custom_rate_fn_old(inst)
                end
                return deltaRate
            end
            inst:ListenForEvent("working", ResetTimer_Event)
            inst:ListenForEvent("finishedwork", ResetTimer_Event)
            inst:ListenForEvent("picksomething", ResetTimer_Event)
        end
    end
)

2af964fce0242524d2e71c4d2bcf8236.png

This is line 86 and 87 for context

3d03eb5d751da8524ce0d1149dbda65b.png

Link to comment
Share on other sites

You shouldn't be adding an AddPrefabPostInit in a prefab.

What I have provided is a standalone that affects wilson.

Use the double indented portion of that code block in your character's init callback if you want it in there.

        if(inst.components~=nil and inst.components.sanity~=nil)
        then
            ResetTimer(inst)
            local custom_rate_fn_old = inst.components.sanity.custom_rate_fn
            inst.components.sanity.custom_rate_fn = function(inst)
                local deltaRate = (inst.DrainSanityRate_Buster or 0)
                if(custom_rate_fn_old~=nil)
                then
                    deltaRate = deltaRate + custom_rate_fn_old(inst)
                end
                return deltaRate
            end
            inst:ListenForEvent("working", ResetTimer_Event)
            inst:ListenForEvent("finishedwork", ResetTimer_Event)
            inst:ListenForEvent("picksomething", ResetTimer_Event)
        end

 

Edited by CarlZalph
Link to comment
Share on other sites

10 minutes ago, DextersComicLaboratory said:

Are you certain? Because I've tried that about 4 times now and I'm getting a crash each time. I'm going to bed I don't have the patience for this.

If you want me to integrate it into your code portion, then feel free to post your prefab here.

Link to comment
Share on other sites

Just now, CarlZalph said:

If you want me to integrate it into your code portion, then feel free to post your prefab here.

Oh uhh alright. I'm sorry, this whole coding thing is still rather new to me. Aside from being huffy thank you for being patient.

Spoiler


local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = {"bustercap"
}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when reviving from ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "buster_speed_mod", 1)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "buster_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end


-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "buster.tex" )
	inst:AddTag("buster")
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "wolfgang"
	
	-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	-- Stats	
	inst.components.health:SetMaxHealth(250)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(150)
	
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1.25 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	

	
end


return MakePlayerCharacter("buster", prefabs, assets, common_postinit, master_postinit, start_inv)

 

 

Link to comment
Share on other sites

Spoiler

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = {"bustercap"}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when reviving from ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "buster_speed_mod", 1)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "buster_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

local function ResetTimer(inst)
    inst.DrainSanityRate_UniqueModNameHere = nil
    if(inst.DrainSanityTimer_UniqueModNameHere~=nil)
    then
        inst.DrainSanityTimer_UniqueModNameHere:Cancel()
        inst.DrainSanityTimer_UniqueModNameHere = nil
    end
    inst.DrainSanityTimer_UniqueModNameHere = inst:DoTaskInTime(10,
        function()
            inst.DrainSanityRate_UniqueModNameHere = -10
            inst.DrainSanityTimer_UniqueModNameHere = nil
        end
    )
end
local function ResetTimer_Event(inst, data)
    ResetTimer(inst)
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "womp.tex" )
	inst:AddTag("buster")
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "wolfgang"
	
	-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	-- Stats	
	inst.components.health:SetMaxHealth(250)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(150)
    
    ResetTimer(inst)
    local custom_rate_fn_old = inst.components.sanity.custom_rate_fn
    inst.components.sanity.custom_rate_fn = function(inst)
        local deltaRate = (inst.DrainSanityRate_UniqueModNameHere or 0)
        if(custom_rate_fn_old~=nil)
        then
            deltaRate = deltaRate + custom_rate_fn_old(inst)
        end
        return deltaRate
    end
    inst:ListenForEvent("working", ResetTimer_Event)
    inst:ListenForEvent("finishedwork", ResetTimer_Event)
    inst:ListenForEvent("picksomething", ResetTimer_Event)
    
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1.25 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	

	
end


return MakePlayerCharacter("buster", prefabs, assets, common_postinit, master_postinit, start_inv)

 

Should resemble this.

 

It should be in the master_postinit function since this is where components are initialized.

Don't forget to change the UniqueModNameHere string to something unique.

Lest someone, somewhere, reuses the exact string and creates variable name conflicts.

Edited by CarlZalph
Tested with blank character and all is well.
Link to comment
Share on other sites

6 hours ago, CarlZalph said:
  Hide contents


local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = {"bustercap"}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when reviving from ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "buster_speed_mod", 1)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "buster_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

local function ResetTimer(inst)
    inst.DrainSanityRate_UniqueModNameHere = nil
    if(inst.DrainSanityTimer_UniqueModNameHere~=nil)
    then
        inst.DrainSanityTimer_UniqueModNameHere:Cancel()
        inst.DrainSanityTimer_UniqueModNameHere = nil
    end
    inst.DrainSanityTimer_UniqueModNameHere = inst:DoTaskInTime(10,
        function()
            inst.DrainSanityRate_UniqueModNameHere = -10
            inst.DrainSanityTimer_UniqueModNameHere = nil
        end
    )
end
local function ResetTimer_Event(inst, data)
    ResetTimer(inst)
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "womp.tex" )
	inst:AddTag("buster")
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "wolfgang"
	
	-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	-- Stats	
	inst.components.health:SetMaxHealth(250)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(150)
    
    ResetTimer(inst)
    local custom_rate_fn_old = inst.components.sanity.custom_rate_fn
    inst.components.sanity.custom_rate_fn = function(inst)
        local deltaRate = (inst.DrainSanityRate_UniqueModNameHere or 0)
        if(custom_rate_fn_old~=nil)
        then
            deltaRate = deltaRate + custom_rate_fn_old(inst)
        end
        return deltaRate
    end
    inst:ListenForEvent("working", ResetTimer_Event)
    inst:ListenForEvent("finishedwork", ResetTimer_Event)
    inst:ListenForEvent("picksomething", ResetTimer_Event)
    
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1.25 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	

	
end


return MakePlayerCharacter("buster", prefabs, assets, common_postinit, master_postinit, start_inv)

 

Should resemble this.

 

It should be in the master_postinit function since this is where components are initialized.

Don't forget to change the UniqueModNameHere string to something unique.

Lest someone, somewhere, reuses the exact string and creates variable name conflicts.

Thank you man! Sorry again for being a little huffy last night.

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...