Jump to content

Events


Recommended Posts

An event is just a table that contains a  label plus a set of data.   The label is the event name.  The 'event'  is always targetted at a specific object in memory like the world, a player, a monster, or some global component like a piece of the interface or the weather manager or clock.   Klei has done a good job setting it up so basically anything can accept events you push on them and those events can have ANY label you choose. There is no master list that defines if a given event name (the label) is valid or not. Anything is valid.

 

When an event is pushed onto an object the event system forwards that event to everything that has been registered as a listener. There can be any number of listeners and every one of them will get called and the data that was passed in with the pushevent call will get passed to the event handler.

 

So that explains how they work.  How you find them is just do a file search for "PushEvent"

Link to comment
Share on other sites

Woah, you made it sound totally complicated. Simplified, an event is code that sets up a certain function to run (using listenforevent) whenever an event is pushed (using pushevent). So for example you can push an event whenever the player character speaks, and you can listen for that event to run some function that reduces sanity.

Link to comment
Share on other sites

I'll try to put it even simpler:

 

Things can use "pushevent" to fire an event. If other things listen for that event (by that thing) using "listenforevent", they will always do what they got told to do then, and only when that event fires.

 

EDIT: To name an example:

An event is like the sun saying "wakey wakey" when rising.

An event listener is like blue mushrooms going "is that the sun?! hide!"

 

That's literally how it works in the game.

Link to comment
Share on other sites

And some code examples.

local event_name = "unique_name"local data = {	value1 = 1,	value2 = "okay",}-- Push the event to the instanceinst:PushEvent(event_name, data)-- ...-- Function to receive eventlocal function event_received_fn(inst, data)	-- inst = the instance which received the event	-- data = data object received from the event		local val1 = data.value1	local val2 = data.value2		print(val1, val2)end-- Detect pushed events and send the data to a functioninst:ListenForEvent(event_name, event_received_fn)

-

 

Shortened version:

-- Push eventinst:PushEvent("unique_name", {value1 = 1, value2 = "okay"})-- ...-- Receive event and use datainst:ListenForEvent("unique_name", function(inst, data)	print(data.value1, data.value2)end)
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...