Abstract class array as RPC paramater

I have a player controller initialized at the start of the game. It spawns a few different ships and calls an Initialize() function on them. Problem is, I need to pass a Crew[] array through the RPC. Crew is an abstract class and the array contains mlutiple different child classes (different abilities, etc.).

I found out that custom types are invalid as paramaters of Rpc calls. I found a page explaining how to register custom types but visual studio doesn't recognize a PhotonPeer class nor is there an explanation of how to implement a byte code which is apparently required.(https://doc.photonengine.com/en-us/realtime/current/reference/serialization-in-photon)

Does anybody know how I could tell the ships which crew is assigned to them? Passing stats is out of the question because of the abstract array :/

Best Answer

  • TDP
    TDP
    Answer ✓

    Update: for anyone else having the same problem, I found a solution.

    I created a string[] array containing the names of the child classes I wanted to pass, which I then used as paramater in the RPC function. In my reciever, the Ship class, I created new instances of these exact child classes and stored them.

    This worked for me because in my case I don't need them to be Monobehaviours, nor do I need them to have Serialized variables (they only exist as a kind of loadout before).

    for(int i = 0; i < crewTypes.Length; i++)
               crew[i] = (Crew)System.Activator.CreateInstance(System.Type.GetType(crewTypes[i]));
    

    (crewTypes is the string[] array paramater)

    If you were to have classes inheriting from Monobehaviour, they would have to be added on GameObjects and you'd have to pass their names to find them again using FindObject(), at least if they are consistent across all instances of the game. If the classes have Serialized variables you require then I'd recommend check the page linked in my original post, although I didn't find a complete explanation on everything required.

Answers

  • TDP
    TDP
    Answer ✓

    Update: for anyone else having the same problem, I found a solution.

    I created a string[] array containing the names of the child classes I wanted to pass, which I then used as paramater in the RPC function. In my reciever, the Ship class, I created new instances of these exact child classes and stored them.

    This worked for me because in my case I don't need them to be Monobehaviours, nor do I need them to have Serialized variables (they only exist as a kind of loadout before).

    for(int i = 0; i < crewTypes.Length; i++)
               crew[i] = (Crew)System.Activator.CreateInstance(System.Type.GetType(crewTypes[i]));
    

    (crewTypes is the string[] array paramater)

    If you were to have classes inheriting from Monobehaviour, they would have to be added on GameObjects and you'd have to pass their names to find them again using FindObject(), at least if they are consistent across all instances of the game. If the classes have Serialized variables you require then I'd recommend check the page linked in my original post, although I didn't find a complete explanation on everything required.

  • I found a solution and posted it but for some reason the comment got deleted? anyways in short I passed the names of the classes as string array and used type.gettype and activator.createinstance to create new versions of those exact classes. Only works in my case tho, cos I dont have any serialized variables and the classes arent inheriting from monobehaviours