Jump to content

Vampire/Health on kills and Day sanity drain


Recommended Posts

I want to make a vampire character. I've already made her not take hits from the night. I want to add the ability to gain health on kills and to lose sanity during daytime rather than nighttime. I have no idea how to do the latter, but I figured that I could use the "hunger on kill" code from my Chomper mod...

local function onkilled(inst, data)
    local victim = data.victim
    if not inst.components.health:IsDead() then
        local total_health = victim.components.health:GetMaxWithPenalty()
        local hunger_steal = inst.hunger_steal or 0.1
        inst.components.hunger:DoDelta(hunger_steal*total_health)
    end
end

...and simply replace all instances of hunger with health...

local function onkilled(inst, data)
    local victim = data.victim
    if not inst.components.health:IsDead() then
        local total_health = victim.components.health:GetMaxWithPenalty()
        local health_steal = inst.health_steal or 0.1
        inst.components.health:DoDelta(health_steal*total_health)
    end
end

...shows what I know. Any help on either of these things?

Link to comment
Share on other sites

This code in yourchar.lua inside the master_postinit I think will make lose sanity in day and gain at night

Spoiler

local function sanityfn(inst)
	local delta = 0
	
	if TheWorld.state.isday then
		delta = -0.25
		else
	if TheWorld.state.isdusk then
		delta = 0.5
		elseif TheWorld.state.isnight then 
		delta = 0.95
	end
	return delta
end)

inst.components.sanity.custom_rate_fn = sanityfn

 

@icantevenname

and this code will drain health of entity and give to you put outside master_postinit

local function vampire_heal(inst, data)
    local victim = data.victim
    if not inst.components.health:IsDead() then
        local total_health = victim.components.health:GetMaxWithPenalty()
        inst.components.health:DoDelta(total_health * .5) -- you can like replace .5 with any number u want to like heal, .5 is like you heal 50% health of the entity you killed is max health
    end
end

put this one in master_postinit

inst:ListenForEvent("killed", vampire_heal)

 

Edited by SuperDavid
Link to comment
Share on other sites

The Vampire Heal works beautifully! Too bad I can't say the same about the sanity drain...

When I put it in like you said, it crashes (Screenshot 1). I tried to put the function out of the Master (Screenshot 2) but it still crashes. The problem seems to be that the coding makes an open bracket that extends past the Master and the MakePlayerCharacter things. If I close it with an End, the game softlocks after selecting the character. If I move the Make beyond the bracket, it crashes due to the Master not being defined. The function's bracket must not go past the Master and I've run outta ideas on how to do so.

 

Screenshot (1).png

Screenshot (2).png

Link to comment
Share on other sites

try putting both these in master_postinit now

local function sanityfn(inst)
	local delta = 0
	
	if TheWorld.state.isday then
		delta = -0.25
		else
	if TheWorld.state.isdusk then
		delta = 0.5
		elseif TheWorld.state.isnight then 
		delta = 0.95
	end
	return delta
end)

inst.components.sanity.custom_rate_fn = sanityfn

do it work?

Link to comment
Share on other sites

try putting both these in master_postinit now

local function sanityfn(inst)
	local delta = 0
	
	if TheWorld.state.isday then
		delta = -0.25
		else
	elseif TheWorld.state.isdusk then
		delta = 0.5
	elseif TheWorld.state.isnight then 
		delta = 0.95
	end
	return delta
end

inst.components.sanity.custom_rate_fn = sanityfn

do it work? lol

Link to comment
Share on other sites

ok I will try something else

@icantevennameok I think this will 100% work

above master_postinit

local function sanity_update_stuff(inst)
	if TheWorld.state.isday and not TheWorld:HasTag("cave") then -- Day
		inst.components.sanity.dapperness = -TUNING.DAPPERNESS_MED
	elseif TheWorld.state.isdusk and not TheWorld:HasTag("cave") then -- Dusk
		inst.components.sanity.dapperness = TUNING.DAPPERNESS_TINY
	elseif TheWorld.state.isnight and not TheWorld:HasTag("cave") then -- Night
		inst.components.sanity.dapperness = TUNING.DAPPERNESS_SMALL
	elseif TheWorld:HasTag("cave") then -- Cave
		inst.components.sanity.dapperness = TUNING.DAPPERNESS_SMALL
	end
end

inside master_postinit

inst:DoTaskInTime(1, sanity_update_stuff)
inst.components.sanity.night_drain_mult = 0
inst:WatchWorldState("startday", function() sanity_update_stuff(inst) end)
inst:WatchWorldState("startdusk", function() sanity_update_stuff(inst) end)
inst:WatchWorldState("startnight", function() sanity_update_stuff(inst) end)

 

Edited by SuperDavid
Link to comment
Share on other sites

19 hours ago, SuperDavid said:

try putting both these in master_postinit now


local function sanityfn(inst)
	local delta = 0
	
	if TheWorld.state.isday then
		delta = -0.25
		else
	elseif TheWorld.state.isdusk then
		delta = 0.5
	elseif TheWorld.state.isnight then 
		delta = 0.95
	end
	return delta
end

inst.components.sanity.custom_rate_fn = sanityfn

do it work? lol

it has extra else after before the elseif

local function sanityfn(inst)
	local delta = 0
	
	if TheWorld.state.isday then
		delta = -0.25
	elseif TheWorld.state.isdusk then
		delta = 0.5
	elseif TheWorld.state.isnight then 
		delta = 0.95
	end
	return delta
end

inst.components.sanity.custom_rate_fn = sanityfn

this should work. if it gives you error message read it and do not assume you are doing something wrong it is usually a typo somewhere. also use some IDE with lua support it will point out problems.

Edited by Ricoom
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...