/// <summary> /// Subscribe to the given event on the given target object /// </summary> /// <param name="targetEvent"></param> /// <param name="target"></param> public void Subscribe(FieldInfo targetKlaxEvent, object target) { // Unsubscribe in case we are subscribed to another event currently Unsubscribe(); if (TargetComponentGuid != Guid.Empty && target is CEntity baseEntity) { CEntityComponent targetComponent = baseEntity.GetComponentByGuid(TargetComponentGuid); if (targetComponent == null) { throw new Exception("Event graphs tried to bind to a component event of a component that does not exist on the parent entity"); } target = targetComponent; } //Validate that the event handler has the correct form CKlaxScriptEventBase klaxEvent = (CKlaxScriptEventBase)TargetEvent.klaxEventInfo.GetValue(target); if (klaxEvent == null) { throw new NullReferenceException("The given event has an invalid handler"); } klaxEvent.Subscribe(Execute); m_eventSource = target; m_bIsSubscribed = true; }
/// <summary> /// Unsubscribes this graph if it is currently subscribed to an event /// </summary> public void Unsubscribe() { if (m_bIsSubscribed) { CKlaxScriptEventBase klaxEvent = (CKlaxScriptEventBase)TargetEvent.klaxEventInfo.GetValue(m_eventSource); klaxEvent.Unsubscribe(Execute); m_bIsSubscribed = false; } }