PUN+ accesing object over network in different scene?

edited November 2013 in Any Topic & Chat
Hello I have a question about creating a connection between 2 different scene's which are connected with photon.
This is kind of like the Marco Polo tutorial but different.
I have a cube prefab in a host scene and got a few buttons in a player scene I want to be able to move the cube in the host scene top, left, right, down depending on the button top etc they press?
How can I get this to work, can someone please support me with this?

Comments

  • In principle, PUN doesn't care about which scene each client loaded. You can position your cube at any position and send this to the other clients, no matter what their scene is or looks like.
    In this case, you should avoid GameObjects that have PhotonViews on them in the scene. If they are loaded with the scene, their IDs and scripts might not be the same in each loaded scene.
  • Well it's more like this:

    cube prefab with movement and photonview script at host scene.

    and

    gui buttons for top etc on player scene

    What I want to do is access the object created at the host scene through the player scene and let it move on the button press.
  • Like I said: Photon doesn't limit you in this regard.
    You might want to use the PhotonNetwork.isMasterClient bool, which is only true on one player's client. It can be considered the host.
    It's explained briefly in the Marco Polo Tutorial: http://doc.exitgames.com/photon-cloud/M ... -tutorials
  • Sorry that my explanation of the problem has been a bit short.

    1 script/client is the host of the room and on creating it's creating a cube prefab.
    The other clients just get the GUI options to move that cube but they do not see it on their screen as the host is more like a screen without controls.
    On a button press at the client I want to move the cube at the host?

    So the part that I still need help with is accessing the cube from the client.

    This is part of the code:
    private GameObject cube;
    private PhotonView playerView;

    void Start()
    {

    if(PhotonNetwork.isMasterClient == true)
    {
    Debug.Log("Hosting the game");
    hostGame();
    }

    if(PhotonNetwork.isMasterClient == false)
    {
    Debug.Log("Joining the game");
    //checkPlayer = 1;
    player();
    }
    }


    void hostGame()
    {
    cube = PhotonNetwork.Instantiate("Cube", Vector3.zero, Quaternion.identity, 0);
    }

    void player()
    {
    Debug.Log("ID" + photonView.viewID.ToString());
    }


    I want to be able to access the id in player().
  • Thanks for your explanation.
    There is no direct support to move an object owned by someone else yet.
    In this case, the master client would be the owner and the only one who should send updates. If you got more than one client who should move the cube, you will run into trouble, as multiple clients can enter their action at the same time.
    If the movement should be action-based (e.g. kicking the cube somewhere), then you could use RPCs. Send them to the owner of the object and this client can decide which action takes place and update the clients.
    If you move the cube around consistently, you would want to use the PhotonView's "observe" method. This, however, only works for the owner. So if the host creates the cube, only this host can send the updates.

    Depending on your (detailed) requirements, you should get creative about who creates the object or who owns it. No one will see who is actually sending the updates and positions. If they are sent by a player, who cares?!
    Another idea: Any client spawns a "pointer" object. An empty transform with a PhotonView. You can move it around and sync the position and such easily. The master client tells all clients which user's "pointer" the cube will follow. You move the cube to the pointer and ... done!
    To refine this: Clients can stop sending their pointer updates when they are not the "active" player.
  • So if I wanted to make it move with RPC commands how would I go about it?
    I have a script with movements attached to the cube prefab I'm using with [RPC] parts before a movement function.
    But then it's a question of how to call it as the host/owner?
  • You can't call it "as the owner". Just call it.
    As long as the method you call does what you want, you can do whatever you like. You can directly move the cube if you like but then you have to make sure the owner of the cube also accepts the "input" to move it. Else the owner might re-position it to whatever is right for that client.
  • I uploaded the 2 scripts used in this project to show you how I'm trying to do it (adding as quotes would make a very long post).

    The script controlGUIPlayer is attached to a seperate scripts gameobject and the hostScript and PhotonView is attached to the cube prefab.
  • I can't look at the scripts right now and as it's a "how do i?" issue instead of something broken, I'm not sure if I can help beyond what I suggested.
    You should take some time and play with the options and ideas I gave you. I'm sure you can find a solution and when you're done, you actually also understand why it's working and what isn't working.
  • Tobias wrote:
    I can't look at the scripts right now and as it's a "how do i?" issue instead of something broken, I'm not sure if I can help beyond what I suggested.
    You should take some time and play with the options and ideas I gave you. I'm sure you can find a solution and when you're done, you actually also understand why it's working and what isn't working.

    Thanks for this scenario 1 has been completed I was thinking to hard :D

    Now on to the next issue:

    In scenario 2 I want to create objects/instantiate on the host using client (so isMasterClient is false).
    To do this I use an RPC instantiating the cube object here is where the problem starts:
    It creates the cube on both the client and host!
    Which is weird since it returns the debug.log data from the if(isMasterClient == false) on the client but also adds the same cube as on the host (yeah the real same one as it moves on the buttons of scenario 1. (it moves on both hosts and client) but yeah the problem is that box being there in the client scene.
    void player()
    {
    photonView.RPC("createCube", PhotonTargets.MasterClient);
    }


    [RPC]
    void createCube()
    {
    if(PhotonNetwork.isMasterClient == true)
    {
    // create the cube object in the network
    Debug.Log("Creating Cube");
    cube = PhotonNetwork.Instantiate("Cube", Vector3.zero, Quaternion.identity, 0, null);
    host = cube.GetComponent<hostScript>();
    }
    if(PhotonNetwork.isMasterClient == false)
    {
    Debug.Log("Failed creating Cube");
    }
    }
  • I'm not sure if I understand exactly what you want to achieve.
    Maybe it's just that I am used to another naming of the player/client roles.

    You want any player to make the Master Client instantiate objects for them?
    Or do you want to Master Client to create objects which the other players don't create?

    PhotonNetwork.Instantiate syncs the created object. No matter who does it, all players will follow and do this automatically, too.
    If you want something only on some clients, then don't use networked Instantiate but GameObject.Instantiate.
  • Fixed before you replyd:

    cube = (GameObject)Instantiate((GameObject) Resources.Load("Cube", typeof(GameObject)), Vector3.zero, Quaternion.identity);

    What I wanted to do was create an object for every player that connected on the host.
    Thanks for your help :D
  • Tobias said:


    .....
    In this case, you should avoid GameObjects that have PhotonViews on them in the scene. If they are loaded with the scene, their IDs and scripts might not be the same in each loaded scene.

    I have a problem with the case you described. However, I do not understand your explanation completely so could you give me a simple solution?
Sign In or Register to comment.