Help! Newbie level instantiate question.

Options
I am very new to photon, apologies if the questions are extremely basic.

1. I am making a game in which most of the elements are created in random positions. I assume the objects need to be instantiated on the server(host) side and use photonview so everyone sees the same objects. Is that correct? I don't want more information going over the server than needed.

2. I also can't seem to figure out how to get it to only happen once. Each player has the script to instantiate the objects. How do I make it only spawn the one time?

3. How do you differentiate from one player and another? I want one player to be chosen at random and be given a different prefab to start the game with. Everything I find is about all players starting with the same prefab.

Thank you for any help. I greatly appreciate it.

Comments

  • Hi @Goliath520,

    1. I am making a game in which most of the elements are created in random positions. I assume the objects need to be instantiated on the server(host) side and use photonview so everyone sees the same objects. Is that correct? I don't want more information going over the server than needed.


    This is basically correct, but actually no objects get instantiated on the server. The server just caches Instantiation calls and forward those messages to all clients. The PhotonView component is required for network identification, synchronization and RPC calls.

    2. I also can't seem to figure out how to get it to only happen once. Each player has the script to instantiate the objects. How do I make it only spawn the one time?


    When do you call those Instantiation calls? This sounds like a game logic problem to me.

    3. How do you differentiate from one player and another? I want one player to be chosen at random and be given a different prefab to start the game with. Everything I find is about all players starting with the same prefab.


    Players are identified by their ID. You can access the local player's ID by using PhotonNetwork.player.ID. To achieve what you want the MasterClient can select a random player before the game starts. Using this approach you have multiple options. You can either store the selected player's ID in the Custom RoomProperties, or add a Custom Property directly to the selected player or use the PhotonNetwork.RaiseEvent function to even send an event to the selected player. Based on the received information each client can instantiate a certain game object as his character.

    Custom (Room) Properties are described in the Synchronization and State guide, there you can find out more about how this is working. RaiseEvent is described in the RPCs and RaiseEvent guide.

    Hope that will help you. If you have further questions please feel free to ask.
  • Goliath520
    Options
    @Christian_Simon Thank you for your response. It is very informative. For the second question I asked. I am generating a maze. So I want the walls to be the same for each player but the maze will be different each game. when the game begins I want to instantiate the same maze for each player but not do it for each player. If there is a wall at 0,0 I don't want each player to instantiate a wall at 0,0.
  • Ok, let's see if I understood this scenario correctly. The MasterClient generates a maze. For himself he generates the maze physically (means that he instantiates the walls), other clients only get the information, where the walls are, but they don't instantiate those. Is this right?

    If this is correct, you can instantiate the walls locally on the MasterClient by using Unity's 'standard' Instantiate function. Then you have to share the walls position somehow with other clients. You can either store the data in the Custom Room Properties, if the amount of walls isn't that huge, or you can use for example the RaiseEvent function to send the coordinates to other clients, so that they can store them locally. The MasterClient has to store them, too.