Jump to content

Syntax help - can I use an event parameter as variable?


Plospo

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 @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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
×
  • Create New...