Jump to content

Recommended Posts

Hi, long time since last post.

 

For starters, the eater component pushes an oneatsomething event, like this:

 

[codesyntax]self.inst:PushEvent("oneatsomething", {food = food})[/codesyntax]

 

Which makes me wonder, is there a way to access the food at the other end? A script that listens for the oneatsomething event, and then checks what kind of food has been eaten, for instance?

 

My guess would be something like this:

 

[codesyntax]

self.inst:ListenForEvent("oneatsomething", {food = food})

        if event.secondparameter:HasTag("delicious") then

                 (print "yummy" in some way)

        end

end

[/codesyntax]

 

I'm very rusty so please have patience, any help is appreciated.

Edited by Plospo

You must listen for that event.

Then you can use it:

self.inst:ListenForEvent("oneat", function(inst,data) self:FoodPoison(data.food) end)
function Food:FoodPoison(food)	print(food.name)	print(food.components.perishable:GetPercent())	self.inst:StartUpdatingComponent(self)	self.poisoned = true    self.inst:PushEvent("foodpoisonstart")end

Other example of this is in kramped.lua file

 @Plospo, There's a function called ListenForEvent.

 

You must listen for that event.
Then you can use it:

self.inst:ListenForEvent("oneat", function(inst,data) self:FoodPoison(data.food) end)

function Food:FoodPoison(food)
print(food.name)
print(food.components.perishable:GetPercent())
self.inst:StartUpdatingComponent(self)
self.poisoned = true
self.inst:PushEvent("foodpoisonstart")
end

Other example of this is in kramped.lua file


To clarify what Q means: You can listen for an event as often as you like, and even from instances that aren't the source (you have to give a reference to the source then, though). [article about events]

Using an directly constructed/made/declared/whatever function, you can add a call to any other function or variable. It's a bit hack-ey, but who cares?

 

Edited by Mobbstar

Oh, "PushEvent" was a typo, I meant ListenForEvent. But the method to push "food" into a new function, to be able to use it, was kinda tricky so thanks for the help! I got it all working now.

 

And how the truck do you forum people put up with all these posting options... makes me feel like an old man trying to write an email.

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