PhotonNetwork.Instantiate

Options
How do I make instantiated objects persist after the requesting client disconnects? I thought, perhaps, making the instantiate an RPCto MasterClient would work, but any instantiated objects vanish when the MasterClient switches due to disconnect. Is this anything to do with the group argument? I'm just using 0. Any feedback would be much appreciated.

Comments

  • Leepo
    Options
    See: PhotonNetwork.InstantiateSceneObject. This allows you to spawn a network object which is owned by the scene (and thus not destroyed when someone leaves)
    (OR manually manage gameobjects and their photonviewassignment..but this is too cumbersome compared to instantiatesceneobject)
    Another option would be to disable autocleanup, as you could then reassign photonviews after the player who owned the object has left.

    But the cleaneast&easiest is using PhotonNetwork.InstantiateSceneObject
  • greenland
    Options
    That's amazing. Thank you very much...

    Wondering how it works with the isMine attribute... :?:
  • greenland
    Options
    I'm a tad confused now. Consider this small bit of code:
    using UnityEngine;
    using System.Collections;
    
    public class TestScript : MonoBehaviour
    {
    
        void OnClick()
        {
            Transform spawnPoint = GameObject.Find("SpawnPoint").transform;
            Object[] dataHolder = new Object[0];
            PhotonNetwork.InstantiateSceneObject("Mob", spawnPoint.position, spawnPoint.rotation, 0, dataHolder);
        }
    }
    
    The XML documentation asserts that the last argument is optional, but the compiler isn't happy unless it's provided. Giving it anything other than a generic Object[] array also makes the compiler unhappy. So I just stub it out for the sake of getting to runtime. I get this:
    NotSupportedException: cannot serialize array of type UnityEngine.Object
    ExitGames.Client.Photon.Protocol.SerializeArray (System.IO.MemoryStream dout, System.Array serObject, Boolean setType)
    ...
    
    It seems like I can't keep the Photon API and the compiler happy at the same time. What am I doing wrong?
  • Leepo
    Leepo
    edited August 2012
    Options
    try null:
    PhotonNetwork.InstantiateSceneObject("Mob", spawnPoint.position, spawnPoint.rotation, 0, null);

    Still, I'd have to check why it doesnt accept the empty Object

    Edit:
    You don't have to send the object argument as it's optional. Either leave it away, or pass null.
    Furthermore, it didn't work as you were using Object (uppercase is Unity.Object). Passing a new object[0] does work.
  • greenland
    Options
    It works! ...and I've learned a bit more about nulls in C#. Thanks for your help.
  • Thanks for this earlier thread, was able to use the 'null' argument for the last parameter in PhotonNetwork.InstantiateSceneObject to get past the same compiler error, but now my AI/NPC doesn't seem to have a 'isMine' property associated with it(get Unity error) so previous calls to that in the RPC calls like 'ApplyDamage' where 'isMine' is true are failing.

    Should InstantiateSceneObject invoked object have a isMine property? or should I re-code my methods to look at some other property,etc?

    Thanks
  • Tobias
    Options
    IsMine is only true for objects that are instantiated for a player. With scene objects you set the "scene" as owner and isMine will not work like for regular player-owned objects.
    The MasterClient can modify scene objects despite this. He is more or less the owner of anything room-related.

    If that doesn't help, then you will need to come up with another ownership property.
  • So looking at the Marco Polo tutorial for example http://doc.exitgames.com/photon-cloud/M ... _Tutorial/ the code for syncing other client movement(Smooth Moves section) as AI/NPC instantiated with InstantiateSceneObject by the masterclient would subtitute the condition
    if (!PhotonNetwork.isMasterClient) {
    
    instead of
    if (!photonView.isMine) {
    

    Correct?

    As a general rule of thumb, seems like I'd always use the new and less documented method 'PhotonNetwork.InstantiateSceneObject' for every scene instantiation other than players themselves - guessing documentation/tutorial examples might add/highlight this relatively new method and above differences for others at some point going through the same mistakes/learning curve.

    Thanks
  • Tobias
    Options
    You can't simply substitute one for the other. They have different usecases.
    IsMine should be true for sceneViews for the MasterClient (one per game), too. But I'm not 100% sure if we check correctly for runtime-instantiated scene objects.

    The InstantiateSceneObject feature is relatively new and when we can, we will add examples and include it in a demo. It's so much easier to add a feature than to come up with a good usecase and tutorial ;)
  • Leepo
    Options
    I updated my response above about the Object problem
  • Hello, noticed this hasnt been updates in 3 years but figured i wouldnt make a new thread for it, i cant seem to have multiple players InstantiateSceneObject because he is not the master client, the master client can just fine but all other players are unable to, i can instantiate using photon normally but then the objects dissapear if the client leaves the room

    here is the full log..

    Failed to InstantiateSceneObject prefab: Brick Resources No1. Client is not the MasterClient in this room.
    UnityEngine.Debug:LogError(Object)
    PhotonNetwork:InstantiateSceneObject(String, Vector3, Quaternion, Int32, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2310)
    Player:BecomeBrick() (at Assets/Scripts/Level Scripts/Player/Player.cs:922)
    Player:Update() (at Assets/Scripts/Level Scripts/Player/Player.cs:796)

    This wasnt always happening, before i was just playing from a single scene for testing but now that i have built a menu system from the Demo Worker scenes as a base i can no longer use InstantiateSceneObject, any ideas how to rectify this? all i really want to do is not have the objects dissapear when the player leaves
  • vadim
    Options
    Hi,

    The error means that PhotonNetwork.InstantiateSceneObject is called on non-master other. Use PhotonNetwork.isMasterClient property to check if client is master.