events
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | events [2020/05/22 12:23] (current) – created maxime | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Events ====== | ||
+ | |||
+ | Some core types use events. These events can be used to run a function when a certain trigger happens on a specific object. | ||
+ | |||
+ | ===== Triggering events ===== | ||
+ | |||
+ | To trigger an event on an object, call this event from the chosen object. Events are designed to be triggered by their owner, and listened to by other objects. | ||
+ | |||
+ | For example, if you create a new component type extending '' | ||
+ | |||
+ | <code lua> | ||
+ | myCustomWorkplace.registerVillager = function(villager) | ||
+ | ... | ||
+ | self.ON_ASSIGNED_WORKER_CHANGED(villager) | ||
+ | ... | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ===== Registering to events ===== | ||
+ | |||
+ | When you create a custom type, objects of this type can listen to event triggers, and run functions when those events are triggered. For this, you use three new event-specific functions in the '' | ||
+ | |||
+ | ==== register ==== | ||
+ | |||
+ | Register to an event triggered on a specific target object. | ||
+ | |||
+ | '' | ||
+ | |||
+ | ^ Name ^ Type ^ Description ^ | ||
+ | | //'' | ||
+ | | //'' | ||
+ | | //'' | ||
+ | |||
+ | <code lua> | ||
+ | local compMainGameLoop = getLevel(): | ||
+ | event.register(self, | ||
+ | myMod: | ||
+ | end) | ||
+ | |||
+ | local parentBuilding = getOwner(): | ||
+ | event.register(self, | ||
+ | myMod: | ||
+ | end) | ||
+ | </ | ||
+ | |||
+ | ==== unregister ==== | ||
+ | |||
+ | Stops listening to an event registered on a specific target object. | ||
+ | |||
+ | '' | ||
+ | |||
+ | ^ Name ^ Type ^ Description ^ | ||
+ | | //'' | ||
+ | | //'' | ||
+ | |||
+ | <code lua> | ||
+ | local compMainGameLoop = getLevel(): | ||
+ | event.unregister(self, | ||
+ | </ | ||
+ | |||
+ | ==== clear ==== | ||
+ | |||
+ | Stops listening to all events registered on the target object | ||
+ | |||
+ | '' | ||
+ | |||
+ | ^ Name ^ Type ^ Description ^ | ||
+ | | //'' | ||
events.txt · Last modified: 2020/05/22 12:23 by maxime