Possible to set viewID at runtime?
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Possible to set viewID at runtime?
strangegames
2018-06-04 16:50:14
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?
strangegames
2019-10-22 20:42:06
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.
Hi @strangegames,
Thank you for choosing Photon and sorry for not answering here earlier.
What PUN version do you use?
I know @xblade724 uses PUN Classic.
Did you read the important note here or the recommended way of switching scenes.
@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)
.
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.
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.
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.
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.
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.
- Remove any [RequireComponent(typeof(PhotonView))], and respectively its PhotonView in the editor
- In awake() within your singleton script, add this:
if ( PhotonView == null )
{
PhotonView = AddComponent
PhotonView.viewID = 999;
}
else
{
Debug.Log( " You already have a photonView " );
}