/// <summary> /// Call this to raise event on entity, that will be processed by registered components /// </summary> /// <param name="component">component upon which container this is going to be invoke</param> /// <param name="eventType">type of event</param> /// <param name="eventParams">event params or derived type</param> public static void RaiseEntityEvent(this MyEntityComponentBase component, MyStringHash eventType, EntityEventParams eventParams) { if (component.Entity == null) // this component is raising event, but it's entity don't exists.. { return; } long entityId = component.Entity.EntityId; InvokeEventOnListeners(entityId, eventType, eventParams); }
/// <summary> /// This just iterates through registered listeners and informs them.. /// </summary> /// <param name="entityId"></param> /// <param name="eventType"></param> /// <param name="eventParams"></param> private static void InvokeEventOnListeners(long entityId, MyStringHash eventType, EntityEventParams eventParams) { var previousValue = ProcessingEvents; if (previousValue) { m_debugCounter++; System.Diagnostics.Debug.Assert(m_debugCounter < 4, "Invokation of entity events are generating more than 3 other entity events and this call get's too long. We should rework the mechanism to some queue and have postponed invokation instead.."); } if (m_debugCounter > 5) { System.Diagnostics.Debug.Fail("Loop on entity events detected, ignoring event " + eventType.String + " and returning.. "); return; } ProcessingEvents = true; if (RegisteredListeners.ContainsKey(entityId)) { if (RegisteredListeners[entityId].ContainsKey(eventType)) { foreach (var registered in RegisteredListeners[entityId][eventType]) { // TODO: This is now in safe block, this should be propably removed later, when this is tested and safe try { registered.Handler(eventParams); } catch (Exception e) { System.Diagnostics.Debug.Fail(String.Format("Invoking registered method for entity event {0} failed. {1} ", eventType.ToString(), e.Message)); } } } } ProcessingEvents = previousValue; if (!ProcessingEvents) { m_debugCounter = 0; } if (HasPostponedOperations && !ProcessingEvents) { ProcessPostponedRegistrations(); } }
/// <summary> /// Call this to raise event on entity, that will be processed by registered components /// </summary> /// <param name="entity">this is entity on which is this being invoked</param> /// <param name="eventType">type of event</param> /// <param name="eventParams">event params or derived type</param> public static void RaiseEntityEventOn(MyEntity entity, MyStringHash eventType, EntityEventParams eventParams) { if (entity.Components == null) // no components that could listen.. { return; } long entityId = entity.EntityId; InvokeEventOnListeners(entityId, eventType, eventParams); }