Cache objects over the network

Hi,

I have a game where players can hit explosives. I want everyone to see when another player hits a certain explosive and I think wending information as an RPC will be a good idea for this.

The explosives are all tagged "explosive" and are activated at start of the level.

The question I have is how to know what explosive someone else hit? I was thinking of caching each explosive object locally like this:


foreach (GameObject explosive in GameObject.FindGameObjectsWithTag("explosive"))
    {
      explosivesList.Add(explosive);
    }

Then when a player hits an explosive I find the index in the explosivesList and send it over RPC. Then each player can activate the explosive locally.


The main quesiton with this approach is: Does Unity find all gameobjects by tag in the same order all the time, or is there a risk that the list is differently ordered for different players? Or can objects even get in different positions in the heirarchy after build and get mixed up that way too?

Answers

  • Tobias
    Tobias admin
    edited December 2021

    I don't really know if Unity finds objects in the same order in each pass. I guess but can't guarantee.

    My approach would be to avoid this question and create the list when you save the scene. A few Editor scripts can easily generate the same list and you're sure it's fixed.

    Sending an RPC for the exploded barrel (via ID) sounds reasonable.

  • "My approach would be to avoid this question and create the list when you save the scene".


    What do you mean by this? You mean I should manually put the objects in a list before building the project? I guess this would work, a bit tedious but would work. I got 20 levels and 5 worlds, so that would be 100 lists to manually create..

  • Shouldn't this be the most standard thing? I can't find anything about this on Google.


    How do games send data about things happening on the level to other players? How do all players see things happening such as buildings falling, things exploding, powerups disspaearing when taken etc? ALL multiplayer games have these things, how is this achieved the standard way?

  • I meant that you could write Editor code that organizes the lists for you, while you edit the scenes.

    There is no standard way to synchronize game state. Much in the same way there is no standard for saving games. It is all done bit by bit by evaluating what part needs to be saved / synced and which doesn't.

  • I understand. Feels like the most basic you want to do in a multiplayer though so should be some preferred way of doing it. I ended up naming everything differently and then send the gameObject name as a string via RPC.

  • If you just wanted to refer to a networked object, using PUN, you'd put a PhotonView on it and then use the ViewID of it to refer to this object.

    However, due to how PUN works internally, I would not really recommend this and instead use a custom made ID. Don't use the name, it can change and it's not effective to send strings.

    In Fusion, the equivalent is the component NetworkedObject.

  • Yeh and I guess PhotonViews are limited to be 999 in a game which is not enough for all environmental objects.

    Custom ID? Basically put a myScript.cs on the component that holds a unique number? That would work too, didn't know that I shouldn't send strings. I am not sure if it matters for my game since these calls will happen at most every 10 seconds or so.