Monoman Posted October 21, 2024 Share Posted October 21, 2024 So I originally used the Walter code to try and edit it but I wasn't able to succeed on that. (I'm generally very new to coding, but really want to make a character mod). Code: Spoiler local MakePlayerCharacter = require "prefabs/player_common" local assets = { Asset("SCRIPT", "scripts/prefabs/player_common.lua"), } -- Your character's stats TUNING.ASH_HEALTH = 175 TUNING.ASH_HUNGER = 125 TUNING.ASH_SANITY = 75 -- Custom starting inventory TUNING.GAMEMODE_STARTING_ITEMS.DEFAULT.ASH = { "flint", "flint", "twigs", "twigs", } local start_inv = {} for k, v in pairs(TUNING.GAMEMODE_STARTING_ITEMS) do start_inv[string.lower(k)] = v.ASH end local prefabs = FlattenTree(start_inv, true) -- When the character is revived from human local function onbecamehuman(inst) -- Set speed when not a ghost (optional) inst.components.locomotor:SetExternalSpeedMultiplier(inst, "ash_speed_mod", 1) end local function onbecameghost(inst) -- Remove speed modifier when becoming a ghost inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "ash_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 onhealthchange(inst, data, forcesilent) if inst.components.health:GetPercentWithPenalty() < 50 then inst.components.sanity.dapperness = -1 elseif inst.components.health:GetPercentWithPenalty() > 50 then inst.components.sanity.dapperness = 0 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( "ash.tex" ) end -- This initializes for the server only. Components are added here. local master_postinit = function(inst) -- Set starting inventory inst.starting_inventory = start_inv[TheNet:GetServerGameMode()] or start_inv.default -- choose which sounds this character will play inst.soundsname = "willow" -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used --inst.talker_path_override = "dontstarve_DLC001/characters/" -- Stats inst.components.health:SetMaxHealth(TUNING.ASH_HEALTH) inst.components.hunger:SetMax(TUNING.ASH_HUNGER) inst.components.sanity:SetMax(TUNING.ASH_SANITY) -- Damage multiplier (optional) inst.components.combat.damagemultiplier = 1 -- Hunger rate (optional) inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE inst.OnLoad = onload inst.OnNewSpawn = onload inst:ListenForEvent("healthdelta", onhealthchange) end return MakePlayerCharacter("ash", prefabs, assets, common_postinit, master_postinit, prefabs) I'm focused on the line "local function onhealthchange(inst, data, forcesilent) if inst.components.health:GetPercentWithPenalty() < 50 then inst.components.sanity.dapperness = -1 elseif inst.components.health:GetPercentWithPenalty() > 50 then inst.components.sanity.dapperness = 0 end end" (Edit of a code line someone else was using) but using this line just caused constant sanity drain whenever health was updated. Mainly working with this function because its the only one I was able to edit and didn't instantly crash the game. I know Walter has it in his code but I wasn't able to find the exact line, and if I were to use that line of code how would I edit it to fit the character? Link to comment https://forums.kleientertainment.com/forums/topic/160290-need-help-coding-want-to-make-a-character-gradually-lose-sanity-under-60-hp/ Share on other sites More sharing options...
yanecc Posted October 21, 2024 Share Posted October 21, 2024 (edited) Use custom_rate_fn instead. local function CustomSanityFn(inst, dt) local hp = inst.components.health:GetPercentWithPenalty() return hp < 0.6 and -1 or 0 end inst.components.sanity.custom_rate_fn = CustomSanityFn Edited October 21, 2024 by yanecc 1 Link to comment https://forums.kleientertainment.com/forums/topic/160290-need-help-coding-want-to-make-a-character-gradually-lose-sanity-under-60-hp/#findComment-1753733 Share on other sites More sharing options...
Monoman Posted October 21, 2024 Author Share Posted October 21, 2024 4 hours ago, yanecc said: Use custom_rate_fn instead. local function CustomSanityFn(inst, dt) local hp = inst.components.health:GetPercentWithPenalty() return hp < 0.6 and -1 or 0 end inst.components.sanity.custom_rate_fn = CustomSanityFn Thank you so much!! Works as expected. 1 Link to comment https://forums.kleientertainment.com/forums/topic/160290-need-help-coding-want-to-make-a-character-gradually-lose-sanity-under-60-hp/#findComment-1753750 Share on other sites More sharing options...
summerscorcher Posted October 26, 2024 Share Posted October 26, 2024 For the record, in your original code you compare health percent against 50, while the game expect a value between 0 and 1. So use 0.5 for 50%, 0.6 for 60%, etc. 2 Link to comment https://forums.kleientertainment.com/forums/topic/160290-need-help-coding-want-to-make-a-character-gradually-lose-sanity-under-60-hp/#findComment-1754686 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