Possible to set viewID at runtime?

I have a scene object from the first scene that persists throughout the game and has a viewID of 1. When I place prefabs in other scenes, Photon always autoassigns conflicting viewIDs because it doesn't know about the view in the original scene. What I would like is to have the viewID set at runtime or have the option to allocate it manually via script.

Even if I attempt to allocate the viewID in Awake(), it still conflicts and generates an error. I'm finding that I have to manually set the viewIDs of all my scene objects to ensure no conflicts which is not ideal.

Is there any way to assign viewIDs at runtime for scene objects so you don't have to manually assign them?

Comments

  • Found this at the top of Google - what was the answer to this?
  • I don't think I ever got an answer on this so I just set my viewIds to really high numbers for scene objects and Photon seems to start with low ids so it works out. I think 999 is the highest viewID you can use so I start there and work my way down for static objects.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited October 2019
    Hi @strangegames,

    Thank you for choosing Photon and sorry for not answering here earlier.

    I don't think I ever got an answer on this
    You could always send an email if you do not get a reply within 2 business days.

    What PUN version do you use?
    I know @xblade724 uses PUN Classic.

    When I place prefabs in other scenes, Photon always autoassigns conflicting viewIDs because it doesn't know about the view in the original scene.
    This looks like a bug that we need to fix first if you could help us with minimal repro steps.

    Did you read the important note here or the recommended way of switching scenes.

    I think 999 is the highest viewID you can use so I start there and work my way down for static objects.
    This depends on MAX_VIEW_IDS. Read about it here.

    so I just set my viewIds to really high numbers for scene objects and Photon seems to start with low ids so it works out.
    I did not get the workaround, could you be more specific?

    @xblade724 I guess you want an answer to the question in the title "Possible to set viewID at runtime?", right? May I know why do you need this? Is it also for scene objects? Do you instantiate those objects at runtime or do you have them in the scene already?

    In any case, there is PhotonNetwork.AllocateViewID(PhotonView) and PhotonNetwork.AllocateSceneViewID(PhotonView).
  • xblade724
    xblade724
    edited November 2019
    I think a better wording for my goal would be: How to *automatically have Photon choose* the viewId? My issue is the same as this one:

    > When I place prefabs in other scenes, Photon always autoassigns conflicting viewIDs because it doesn't know about the view in the original scene.


    So in 1 scene, I'll start at 0. 2nd scene, I'll start at 100. Other scene, I'll start at 300. Or go backwards from 999 at the farthest-end scene. It's not necessarily ideal - I first ran into an issue with our gameover screen photonView Id's conflicting with our Id from our lobby scene. So both had the same view.

    I noticed that when PhotonViews are stored in a prefab, it'll automatically assign. However, if not in a prefab, I'd like the same feature where I can checkbox an "auto-assign" option to prevent conflicts for when I don't track a static ID. This way, I don't have to remember what IDs the other ones are in different scenes.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @xblade724,

    In PUN2, we have addressed this issue lately.
    We have added a PunSceneSettings ScriptableObject to configure the minimal PhotonViewID per scene.
    We have a § talking about this here.

    So I guess, it's another reason to switch to PUN2.
  • Gargosian
    Gargosian
    edited February 2021
    Hey @JohnTube ,
    to be honest I don't really see the point in the PunSceneSettings ScriptableObject you were talking about back then. Sure it helps assigning those IDs easier at this certain point, but it does not fix the problem of IDs beeing assigned in a conflicting way. The moment you increase your ID count in a Scene a lot you have to shift all your IDs. Is there no way to have a Scene object which gets an ID assigned realtime without instantiating it?

    Maybe it is totally trivial why this is not a feature, but I cannot see it.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Gargosian,

    Thank you for choosing Photon!

    The PunSceneSettings ScriptableObject helps you set the offset (lower bound, min. index/ViewID) of 'static' / compile time / scene ViewIDs per scene.
    The moment you increase your ID count in a Scene a lot you have to shift all your IDs
    You could simply choose a higher value per scene (more than what you actually have) to take this (further additions of scene networked objects later in the project) into consideration and avoid resetting PunSceneSettings.
  • not_good
    not_good
    edited May 2021
    Hi there, late but just in case.. I also got this error, and found a resolution :

    "PhotonView ID duplicate found: 1. New: View (0)1 on PlayerNetwork (scene) old: View (0)999 on
    PlayerNetwork (scene). Maybe one wasn't destroyed on scene load?! Check for 'DontDestroyOnLoad'.
    Destroying old entry, adding new."

    Basically PlayerNetwork is the object that persists with a PhotonView attached. -> Which causes the duplication that is happening right as the scene loads in.

    To avoid this duplication key error: Just initially remove your photonview, attach it to your Singleton object on awake if it has none yet.


    Therefore there will be no dupes on scene load. As the problem lies with the PhotonView being attached in editor, rather than JUST after scene load where the checks happen.



    It seems like it's a benign error message anyways, unless you're doing something specific to certain viewID values.

    1) Remove any [RequireComponent(typeof(PhotonView))], and respectively its PhotonView in the editor
    2) In awake() within your singleton script, add this:

    if ( PhotonView == null )
    {
    PhotonView = AddComponent<PhotonView>();
    PhotonView.viewID = 999;
    }
    else
    {
    Debug.Log( " You already have a photonView " );
    }