Question by
halzate93 · Oct 28, 2016 at 04:43 PM ·
c#programmingeventslambda
Trigger events from different sources
Hey guys, I'm trying to make a crumbling platform, I have a parent CrumblingPlatform and children CrumblingPlatformPieces. The platform could respawn on different ways, so I decoupled them and use events OnTriggered, OnFinishedFalling and OnReady. I need to know which piece triggered the event, does anyone know a better way to do so than sending a reference on the event to 'this' everytime and for every event?.
public event Action<CrumblingPlatformChild> OnFinishedFalling;
public event Action<CrumblingPlatformChild, Collision2D> OnTriggered;
public event Action<CrumblingPlatformChild> OnReady;
private void TriggerReady ()
{
if (OnReady != null)
OnReady (this);
}
A possible solution would be to use lambdas in the calling side.
private void SubscribeToEvent (EventTrigger trigger)
{
trigger.OnFinished += () => ConsumeEvent (trigger);
}
private void ConsumeEvent (EventTrigger trigger)
{
// My code
}
Anyone can think of a better way?
Comment