Jump to content

Hunger gain on kill


Recommended Posts

The whole toolless-and-naturally-tree-chopping thing for my Chomper mod was a bust. But I still wanna give him something to have him stand out. Having him gain hunger from killing things should do the trick. Though, I don't know how to do that. Any help?

Link to comment
Share on other sites

Place this line in master_init:

inst:ListenForEvent("killed", onkilled)
inst.hunger_steal = 0.1 --Percent of the victim's max health that's converted into hunger

Then place this function before master_init:

local function onkilled(inst, data)
	local victim = data.victim
	if not inst.components.health:IsDead() and IsValidVictim(victim) 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

Modified from wathgrithr.lua, but I haven't tested this yet though.

Link to comment
Share on other sites

12 hours ago, JohnWatson said:

Place this line in master_init:


inst:ListenForEvent("killed", onkilled)
inst.hunger_steal = 0.1 --Percent of the victim's max health that's converted into hunger

Then place this function before master_init:


local function onkilled(inst, data)
	local victim = data.victim
	if not inst.components.health:IsDead() and IsValidVictim(victim) 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

Modified from wathgrithr.lua, but I haven't tested this yet though.

 

20170522141813_1.jpg

Link to comment
Share on other sites

I have it like this

local function onkilled(inst, data)
    local victim = data.victim
    if not inst.components.health:IsDead() and IsValidVictim(victim) 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

local function master_init(inst)
    inst:ListenForEvent("killed", onkilled)
    inst.hunger_steal = 0.1 --Percent of the victim's max health that's converted into hunger
end

If this isn't enough, I could attach the prefab file.

Link to comment
Share on other sites

11 minutes ago, JohnWatson said:

Ah, the problem was you created a new 'master_init' function when you only needed to put the two lines in the existing 'master_postinit' function.

Fixed it here:

chomper.lua

It still crashes...

Though, I looked through Wathgithr's prefab and found this

local function IsValidVictim(victim)
    return victim ~= nil
        and not ((victim:HasTag("prey") and not victim:HasTag("hostile")) or
                victim:HasTag("veggie") or
                victim:HasTag("structure") or
                victim:HasTag("wall") or
                victim:HasTag("companion"))
        and victim.components.health ~= nil
        and victim.components.combat ~= nil
end

I think that's missing from the code given to the Chomper.

20170522230309_1.jpg

Link to comment
Share on other sites

3 minutes ago, JohnWatson said:

@icantevenname Seems like that's the problem. You could choose whether to add the function or to change this line:


if not inst.components.health:IsDead() and IsValidVictim(victim) then

...into just this:


if not inst.components.health:IsDead() then

 

Works like a charm! Thanks for all the help!

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