Jump to content

Recommended Posts

I made a custom food and gave it stats, but I want to change the things it does if a certain character eats it, so I wrote this lines...but of course the game ignores it completly..can someone explain me why? :/

 

Thanks a lot 

AddPrefabPostInit("custom_food", function(inst)    local _oneatenfn = inst.components.edible.oneatenfn     inst.components.edible.oneatenfn = function( inst, eater)        if eater and eater.components.health and eater.prefab == "custom_character" then            eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE)            _oneatenfn( inst, eater )        else            _oneatenfn( inst, eater )        end     endend) 

AddPrefabPostInit("custom_food", function(inst)    print("Okay, custom food exists.")    local _oneatenfn = inst.components.edible.oneatenfn    if not _oneatenfn then print("Btw old oneaten is nil!!! Your bad.") end      inst.components.edible.oneatenfn = function( inst, eater)        print("Custom OnEaten works! eater = "..tostring(eater)..", food = "..tostring(inst))        if eater and eater.components.health and eater.prefab == "custom_character" then            print("Everything should be okay!")            eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE)            print("Executing old oneat fn...")            _oneatenfn( inst, eater )        else            print("Something goes wrong... eater.components.health = "..tostring(eater.components.health)..", eater.prefab = "..tostring(eater.prefab))            _oneatenfn( inst, eater )        end      endend)

Then check your log.txt or even ingame log via Ctrl+L

You're right it stucks on the oneaten is nill part :/ not sure what can I do against it...I've tried some alternatives without any progress :(

AddPrefabPostInit("custom_food", function(inst)    print("Okay, custom food exists.")    local _oneatenfn = inst.components.edible.oneatenfn    if not _oneatenfn then print("Btw old oneaten is nil!!! Your bad.") end      inst.components.edible.oneatenfn = function( inst, eater)        print("Custom OnEaten works! eater = "..tostring(eater)..", food = "..tostring(inst))        if eater and eater.components.health and eater.prefab == "custom_character" then            print("Everything should be okay!")            eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE)            print("Executing old oneat fn...")            _oneatenfn( inst, eater )        else            print("Something goes wrong... eater.components.health = "..tostring(eater.components.health)..", eater.prefab = "..tostring(eater.prefab))            _oneatenfn( inst, eater )        end      endend)


Then check your log.txt or even ingame log via Ctrl+L

 

 

Edited by YordlePrincess

@YordlePrincess
It's a custom food, right? That means it doesn't has a oneatenfn yet, that's why it's nil.

Just don't do anything if the eater wasn't your character. Also, if you're making a custom edible, don't use AddPrefabPostInit, instead put the code inside your food's prefab, like so:

local function fn()...	inst.components.edible.oneatenfn = function( inst, eater)		if eater and eater.components.health and eater.prefab == "custom_character" then			eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE)		end	endend

If you are modifying an existing food, just add a check asking if the function isn't nil before calling it.

 

I tried your method but it still ignores it I've tried to check but it doesn't print anything where does it stuck :(

@YordlePrincess
It's a custom food, right? That means it doesn't has a oneatenfn yet, that's why it's nil.

Just don't do anything if the eater wasn't your character. Also, if you're making a custom edible, don't use AddPrefabPostInit, instead put the code inside your food's prefab, like so:

local function fn()...	inst.components.edible.oneatenfn = function( inst, eater)		if eater and eater.components.health and eater.prefab == "custom_character" then			eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE)		end	endend

If you are modifying an existing food, just add a check asking if the function isn't nil before calling it.

 

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