none
Why is this simple cast not working?

    Question

  • EventManager class:

    public class EventManager : MonoBehaviour
        {
            public event EventHandler DialogEvent;
    
            public static void ProcessEvent(IEvent eventObject)
            {            
                EventManager eventMgr = ManagerRepo.Instance.EventMgr;
                switch (eventObject.Type)
                {
                    case EventType.Unknown:
                        break;
                    case EventType.Dialog:
                        eventMgr.DialogEvent(eventObject, EventArgs.Empty);
                        break;
                    default:
                        break;
                }
            }
        }


    And the handling method in another class:

    private void HandleDialogEvent(object sender, EventArgs e)
            {
                NpcResponse response = sender as NpcResponse;
                Debug.Log("Event received for keyword: " + response.Keyword);
            }

    The cast within the HandleDialogEvent method is not working and the variable response ends up being null.

    Wednesday, April 19, 2017 1:50 AM

Answers

  • Not 100% sure about this one, but maybe this is relevant:

    sender appears to be of type EventObject<NpcResponse>

    I suspect EventObject<NpcResponse> is not an NpcResponse

    Perhaps EventObject<NpcResponse> response = sender as EventObject<NpcResponse> is the statement you should be using?

    FWIW: The backtick  syntax is how generic types get named.

    • Proposed as answer by CoolDadTxModerator Wednesday, April 19, 2017 2:31 PM
    • Marked as answer by darrenlc Wednesday, April 19, 2017 2:32 PM
    Wednesday, April 19, 2017 2:51 AM

All replies

  • Not 100% sure about this one, but maybe this is relevant:

    sender appears to be of type EventObject<NpcResponse>

    I suspect EventObject<NpcResponse> is not an NpcResponse

    Perhaps EventObject<NpcResponse> response = sender as EventObject<NpcResponse> is the statement you should be using?

    FWIW: The backtick  syntax is how generic types get named.

    • Proposed as answer by CoolDadTxModerator Wednesday, April 19, 2017 2:31 PM
    • Marked as answer by darrenlc Wednesday, April 19, 2017 2:32 PM
    Wednesday, April 19, 2017 2:51 AM
  • You are exactly right, so I will be marking this as the answer. Unfortunately, I have another question. Here is my code in the event handler method now:

    private void HandleDialogEvent(object sender, EventArgs e)
            {
                EventObject<NpcResponse> response = sender as EventObject<NpcResponse>;
            }
    I can see the values within the response object in the Locals window, but I am unsuccessful in accessing them via code. Would you also happen to know this?
    Wednesday, April 19, 2017 4:29 AM
  • You mean that you can set a breakpoint and see properties of the response object, but you can't write code to access properties of the response object?

    What happens if you access some property of the response object?

    private void HandleDialogEvent(object sender, EventArgs e)
    {
        EventObject<NpcResponse> response = sender as EventObject<NpcResponse>;
        System.Diagnosties.Debug.WriteLine( response.your_property_name_here );
    }

    Does it fail to compile?  Does it throw an exception?


    • Edited by Wyck Monday, April 24, 2017 3:12 PM
    Monday, April 24, 2017 3:11 PM