Jump to content

Recommended Posts

Hey all! I'm working on a character mod to help ease some friends into DST and it's almost ready!
Problem is, I'm having some trouble with his health regen code.

What I intended for it to do was to adjust the health regen speed based on hunger level, with the character saying something to indicate the change in regen speed. When I put this into the game, the character just endlessly repeats the talker line for the current regen level while the actual regen itself doesn't work.

This is what I have so far:

--Health Regen System. 3 levels of regen speed depending on hunger. No regen if hunger is below 20% (or 32).
--Currently not working.
local function healthregen(inst)
	if inst.components.hunger.current >= 120 then
	  inst.components.health:StartRegen(5,3)
      --These temporary talker lines were added so I can quickly check if the code was working properly.
	  inst.components.talker:Say("Regen Level 3")
	  
	elseif inst.components.hunger.current >= 80 then
	  inst.components.health:StartRegen(5,6)
	  inst.components.talker:Say("Regen Level 2")
	  
	elseif inst.components.hunger.current >= 32 then
	  inst.components.health:StartRegen(5,9)
	  inst.components.talker:Say("Regen Level 1")
	  
	elseif inst.components.hunger.current <= 31 then
	  inst.components.health:StartRegen(0,1)
	  inst.components.talker:Say("I'm not healing anymore... I should eat.")
   end
end

-- This initializes for both clients and the host
local common_postinit = function(inst) 
	-- choose which sounds this character will play
	inst.soundsname = "willow"
	inst:AddTag("yoru_builder")

	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "yoru.tex" )
end

-- This initializes for the host only
local master_postinit = function(inst)
	--These lines are for a level system for his attack power. Please disregard these.
	inst.level = 0
	inst.components.eater:SetOnEatFn(oneat)
	applyupgrades(inst)

	-- Stats	
	inst.components.health:SetMaxHealth(140)
	inst.components.hunger:SetMax(160)
	inst.components.sanity:SetMax(200)
	inst.components.hunger.hurtrate = 1.5
	inst.components.health.absorb = 0.25
	inst.components.temperature.mintemp = 10
	inst.components.health.fire_damage_scale = 0
	inst.components.temperature.inherentsummerinsulation = 1.5
	
	inst.components.hunger.hungerrate = 1.25 * TUNING.WILSON_HUNGER_RATE
	
	inst.components.sanity.rate_modifier = ((0.5 * TUNING.WENDY_SANITY_MULT))
	
	inst:ListenForEvent("hungerdelta", healthregen)
	
  --These are also for the level system. Please ignore these too. 
	inst.OnSave = onsave
	inst.OnPreLoad = onpreload
end



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

Could I get some help with this, please? I'm not very experienced in coding (self-taught), so I'm not sure if I even have this coded properly. 

I've attached the entire Lua file just in case it's needed. (yoru.lua)

Thanks a lot in advance!

Edited by K-KyoruMii
local function stophealthregen(inst)
	if inst.hungerregen then
		inst.hungerregen:Cancel()
		inst.hungerregen = nil
	end
	inst.hungerperiod = nil
	inst.components.talker:Say("Stopping Regen")
end

local function starthealthregen(inst, period)
	stophealthregen(inst)
	
	inst.components.talker:Say("Regenerating 5 every " .. period)
	inst.hungerperiod = period
	inst.hungerregen = inst:DoPeriodicTask(inst.hungerperiod, function()
		inst.components.health:DoDelta(5, true, "hunger-regen")
	end)
end

local function healthregen(inst, data)
	if data.newpercent >= 0.2 then
		local period = 3
		if data.newpercent >= 0.5 then
			period = 6
		end
		if data.newpercent >= 0.75 then
			period = 9
		end
		
		if inst.hungerperiod == nil or period ~= inst.hungerperiod then--only changes the task if the period is different
			starthealthregen(inst, period)
		end
	else
		stophealthregen(inst)
	end
end

-----------------------------------------------------------------------
--master_postinit:
inst:ListenForEvent("hungerdelta", healthregen)
inst:ListenForEvent("death", stophealthregen)

I avoided using the Health:StartRegen() function because if any mod decides to use it will probably cause 1 of the regenerations to be lost.

I made it change only when it hits a different threshold. and 0.2, 0.5, 0.75 are 32, 80, 120 respectively

Edited by Aquaterion
47 minutes ago, Aquaterion said:

<new code>

I avoided using the Health:StartRegen() function because if any mod decides to use it will probably cause 1 of the regenerations to be lost.

I made it change only when it hits a different threshold. and 0.2, 0.5, 0.75 are 32, 80, 120 respectively

Thank you so much! The code works like a charm!
I noticed he still kept repeating "Stopping Regen" on a loop when he was hungry enough, so I moved the line. Seems to have done the trick!

local function stophealthregen(inst)
	if inst.hungerregen then
		inst.hungerregen:Cancel()
		inst.hungerregen = nil
		inst.components.talker:Say("Stopping Regen")
	end
	inst.hungerperiod = nil
end

Once again, thank you so much for your help!

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
×
  • Create New...