Jump to content

Recommended Posts

Hi, I'm trying to make a character loose sanity when eating any form of meat.

 

This is what I have in the character's lua : 

-- Decrease sanity when eating meatlocal function oneat(inst, food)      if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then          inst.components.sanity:DoDelta(-15)     endend

So why is this not working? :-)

 

I also tried adding a ListenForEvent in the master_postinit but that didn't fix it ^^'

Edited by Thibooms

So why is this not working?

uhm, i guess because u dont use your function anywhere?

if this is all u added, then u really only created a function, without using it anywhere.

the eater-component pushes this event:

self.inst:PushEvent("oneat", {food = food})

if u use a correct listener, u can use this to call your function, when u eat something ingame.

Edited by Seiai

@Seiai,Well I now added : 

inst:ListenForEvent("oneat", oneat) 

In the master_postinit but I get this crash when eating (meat and veggies) : 

69: attempt to index field 'components' (a nil value)

The line that crashes is this one : 

 

if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then 

 

Or maybe the one after it of course

Edited by Thibooms

Why would you list the meat?

 

C&P this to you modmain and try it yourself:

 

local function oneat(inst, food)
     if food and food.components.edible and food.components.edible.foodtype == GLOBAL.FOODTYPE.MEAT then
         inst.components.sanity:DoDelta(-15)
     end
end

AddPlayerPostInit(function(inst)
    if inst.components.eater then
        inst.components.eater:SetOnEatFn( oneat )
    end
end)

 

In the actual mod, make a copy of the old oneatfn and append that to you new oneatfn.

Edited by Isosurface

@Isosurface, Oh, I didn't get what you meant at first. But this will make the penalty for all characters, I want only mine to be affected.

 

I just don't get why I get that crash when eating. Why does it have a problem with the components? It seems like a simple function so I'd like to just use that, or at least know what's not working. I'm trying to learn from this sort of stuff... ^^

But this will make the penalty for all characters, I want only mine to be affected.

well, then add a check for if inst is your character.

inst.prefab=="yourcharactersprefabname"
also, the eventlistener calls the function u provide with the the parameters inst and the parameter from the pushevent-function(as u can see from all the other eventlistener and pushevents in the gamecode). the parameter in the pushevent-function here is {food = food}, which is a table, containing a variable food, which references the fooditem u eat.

which means that u have to select the item out of that table, either by changing the Listener:

inst:ListenForEvent("oneat", function(inst,data) oneat(inst,data.food) end) 
OR by changing the oneatfunction

local function oneat(inst, data)      local food=data.food     if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then          inst.components.sanity:DoDelta(-15)     endend
Edited by Seiai

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