HELP WITH EVENTS!

This is the issue, its quite bizarre.

I have an event in a Generic Class like so:

public abstract class Foo<G,T> : Singleton<G> where G : Singleton<G>{
private void Start(){
Debug.Log(typeof(T).ToString());
}
private void MyEvent(EventData){
Debug.Log(typeof(T).ToString());
}
}

If I instantiate an object from a class that inherits from Foo like so:
class Goo : Foo<Goo, float>;

Start prints: type of T is System.Single,
MyEvent prints: type of T is System.Single.
Both are right since its a float...

After I delete the instantiation of Goo (I move to another scene, trigger OnDestroy, etcetera), I instantiate another object from a different class that inherits from Foo like so:
class Hoo : Foo<Goo, int>;

Start prints: type of T is System.Int32,
MyEvent prints: type of T is System.Single.

THIS is the weird thing. Start and every other function within the class gets the type well, except the event.

At first I was afraid it was something regarding Singletons... But then I found out that only events get this wrong (haven't tried out RPCs, but since they are probably implemented as events on the background they might have the same issue).

My suspicion is that PUN instantiates something static of every event that fixates that type T to the first typing given, as every possibility of this maintains the type T on the first instantiation I made.

Beyond that I have NO clue of what to do. I would appreciate the help...

Note: The singleton is a plain ol' singleton, with nothing special going on with it.

Thanks in advance!

Comments

  • Typo: class Hoo : Foo<Goo, int>, should be: class Hoo : Foo<Hoo, int>;
  • Hi @Sleepy,

    Thank you for choosing Photon!

    I'm sorry to say your post is very confusing.
    The generics make it look complex and if you try to understand what's the issue you can't find anything concrete about Photon.

    So please try to answer the following questions:

    - What PUN version are you using?
    - What do you want to achieve?
    - What is the expected behaviour?
    - What is the current actual behaviour?
    - What are the minimal repro steps?

    I'm also not sure Unity and PUN work well with the generics.
  • Hi @JohnTube ,

    I'll try to be as clear as possible.

    - What PUN version are you using?
    Im using PUN 2 with the respective libraries (pun, realtime and chat).

    - What do you want to achieve?

    Given a generic abstract class like so:

    public abstract class Foo<T>{
    private void Start(){
    Debug.Log("Type of T on Start is: " + typeof(T).ToString());
    }
    public void OnDestroyed()
    {
    PhotonNetwork.NetworkingClient.EventReceived -= GetPlayerResult;
    }

    public void OnAwake()
    {
    PhotonNetwork.NetworkingClient.EventReceived += GetPlayerResult;
    }

    private void MyEvent(EventData data){
    if(data.code == 1){
    Debug.Log("Type of T on MyEvent is: " + typeof(T).ToString());
    }
    }
    }

    MyEvent is triggered with a RaiseEvent function from PUN and is registered to EventReceived.

    And the following classes:

    class Goo : Foo<int>{
    }
    class Hoo : Foo<float>{
    }

    If I run the following coroutine:

    private IEnumerator ExampleCo(){

    Goo g = new Goo(); // I instantiate an object of Goo, T in Foo is int here
    yield return new WaitForEndOfFrame(); // Just wait to run Start().
    RaiseMyEvent(1); // I raise an event with code 1 for everyone to hear.
    Destroy(g); // I deinstantiate g.

    Hoo h = new Hoo(); // I instantiate an object of Hoo, T in Foo is float here
    yield return new WaitForEndOfFrame(); // Just wait to run Start().
    RaiseMyEvent(1); // I raise another event with code 1 for everyone to hear.
    Destroy(h); // I deinstantiate h.

    }

    - What is the expected behaviour?
    For the log to print:
    From g: "Type of T in Start is: System.Int32"
    From g: "Type of T in MyEvent is: System.Int32"
    From h: "Type of T in Start is: System.Single"
    From h: "Type of T in MyEvent is: System.Single"

    I want the Type T to be consistent in both PUN events and Unity's callbacks.

    - What is the current actual behaviour?
    The log prints:
    From g: "Type of T in Start is: System.Int32"
    From g: "Type of T in MyEvent is: System.Int32"
    From h: "Type of T in Start is: System.Single"
    From h: "Type of T in MyEvent is: System.Int32" <--- This should print System.Single in my mind.

    h's T is of type float when instantiated. It should print System.Single in both the Start() function and MyEvent.

    - What are the minimal repro steps?
    Run the coroutine previously defined with the classes stated.

    I know that PUN's RPC's don't work with C# generics.
    I want to know if that is the case as well with events.

    I hope it's less confusing. If it still is, let me know please.

    Thanks again!
Sign In or Register to comment.