icantevenname Posted May 22, 2017 Share Posted May 22, 2017 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 https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/ Share on other sites More sharing options...
JohnWatson Posted May 22, 2017 Share Posted May 22, 2017 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 https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-923648 Share on other sites More sharing options...
icantevenname Posted May 22, 2017 Author Share Posted May 22, 2017 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. Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-923913 Share on other sites More sharing options...
JohnWatson Posted May 23, 2017 Share Posted May 23, 2017 @icantevenname Where have you placed the code? Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924012 Share on other sites More sharing options...
icantevenname Posted May 23, 2017 Author Share Posted May 23, 2017 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 https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924014 Share on other sites More sharing options...
JohnWatson Posted May 23, 2017 Share Posted May 23, 2017 41 minutes ago, icantevenname said: If this isn't enough, I could attach the prefab file. That would help out. Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924022 Share on other sites More sharing options...
icantevenname Posted May 23, 2017 Author Share Posted May 23, 2017 1 hour ago, JohnWatson said: That would help out. Here ya go. chomper.lua Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924063 Share on other sites More sharing options...
JohnWatson Posted May 23, 2017 Share Posted May 23, 2017 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 Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924069 Share on other sites More sharing options...
icantevenname Posted May 23, 2017 Author Share Posted May 23, 2017 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. Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924077 Share on other sites More sharing options...
JohnWatson Posted May 23, 2017 Share Posted May 23, 2017 @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 Link to comment https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924078 Share on other sites More sharing options...
icantevenname Posted May 23, 2017 Author Share Posted May 23, 2017 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 https://forums.kleientertainment.com/forums/topic/78989-hunger-gain-on-kill/#findComment-924079 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