Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party [Photon] Instantiating object and making it react to all players.

Discussion in 'Multiplayer' started by LootHunter, Aug 12, 2017.

  1. LootHunter

    LootHunter

    Joined:
    May 27, 2017
    Posts:
    66
    Hi.
    In my project, I want to have a few objects that a controllable by several players at once. Like it would be if the both players were on the same computer. But the one problem is that PhotonView transmits information about the object only from one "owner" to other players. So if a player, who is not "owner" does something to an object this doesn't go to others.
    There is also another problem. I prepare my scene before Photon Network is connected. So Photon's Instantiate doesn't work. I have to use regular Instantiate and after that I assign ViewID to instantiated objects. Problem is that if a player, who instantiated the objects, leaves, these objects are destroyed. And I don't want that.
     
  2. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    define controllable?

    to be shot at, pushed around like footballs, or directly controlled like players can take control with WASD.
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Why not prepare your scene after PhotonNetwork is connected, instantiate the objects as scene objects, and then use RequestOwnership to allow your players to control the objects.

    If the player currently controlling a scene object leaves, ownership is transferred to the master client, and the object remains in the scene.
     
    SiliconDroid likes this.
  4. LootHunter

    LootHunter

    Joined:
    May 27, 2017
    Posts:
    66
    The latter. Basically it's "Tanks", only you are supposed to be able to play on two computers as if you are on one.

    Because the game also can be played offline. And anyways, I don't understand, why there is no "don't destroy" option for Photon in the first place?
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    If you want to play it offline, just set Photon to offline mode, you can use exactly the same code as online, but it won't try to connect to the Photon cloud.
     
  6. LootHunter

    LootHunter

    Joined:
    May 27, 2017
    Posts:
    66
    Ok. Then noob question - how doI ensure that scene is created after Photon is connected? Or active in offline mode?
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Depending on your needs you can do whatever initialisation you need in the following callbacks, OnConnectedToMaster, OnJoinedLobby, OnJoinedRoom

    These callbacks will happen in both online and offline mode, so you can use the same logic flow in either case, the only difference being that in offline mode you don't need an internet connection and you will be the only player in the game.

    In offline mode, all other Photon functions such as RPCs etc. will function the same as in online mode, except they won't try and send any data to the cloud when they are invoked.
     
    tobiass likes this.
  8. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    I can offer some ideas, at least help brainstorm, I'm no PUN master, there may be a better way?

    With no real master/server architecture; any scheme you implement will have to be asymmetric. A scheme might be:

    First player to create room (P1) is given server like responsibility.

    P1 Opens empty scene.

    P1 uses PhotonNetwork.Instantiate to spawn all required moveable objects (Om) that all inherit PhotonView).

    Any further players (Pn) that join will open required empty scene and then connect.

    When Pn connects avatar moveables will spawn automatically.

    Pn controls moveable avatar by sending KeyUp/Dn commands via RPC to P1.

    This scheme would introduce control lag for Pn, this lag can be hidden from Pn though:

    When Pn pushes key an abstraction of PhotonView is moved locally, PhotonView is invisible.
    Send KeyUp/Dn RPCs but with the KeyUp RPC include global pos/rot of avatar abstraction.
    When P1 receives Pn KeyUp(vPos,vRot) it makes sure relevant PunView is correct (synched to Pn).
    Pn abstraction tracks its PunView directly when no control key down.
     
    Last edited: Aug 24, 2017