Proper way to handle procedural levels?

I'm working on a 2 player coop game that uses procedural generation for the levels and I don't think I'm using PUN properly-

As of right now I have the masterClient run a function that generates the level- I set PhotonNetwork.automaticallySyncScene = true; so when instantiating the second player they appear in the level-

I'm using PhotonNetwork.Instantiate to generate the pieces of the level(Rooms, props, enemies, etc) so this creates hundreds of PhotonView's- the level runs fine for the masterClient but the second player's fps is almost frozen it's so slow- I'm guessing its from too many views?

The only objects that I'm generating that need to be "live" on PUN are the enemies and the doors- all together they add up to about 30 PhotonView's-

I don't need the Room's, props etc to be live on PUN but I don't know how to make them visible to the other player after generating the level without using PhotonNetwork.Instantiate- which then add's all the PhotonView's-

Ideally I would like to pre-generate the scene before loading it with Pun using LoadSceneAsync and run the procedural generation in Awake() but I don't know if that is possible to do with PUN-

If anyone has any guidance/direction/tips I would appreciate them- thank you.

Comments

  • From searching through the "procedural" posts on this board it seems the easiest way would be to generate everything locally on both players using the same seed I would sync using an RPC-
  • Hi @mdotstrange,

    I set PhotonNetwork.automaticallySyncScene = true; so when instantiating the second player they appear in the level


    I guess there is a misunderstanding here: automaticallySyncScene only makes sure, that each clients load the same scene if the MasterClient uses PhotonNetwork.LoadLevel(...);. It has nothing to do with object synchronization.

    I'm using PhotonNetwork.Instantiate to generate the pieces of the level(Rooms, props, enemies, etc) so this creates hundreds of PhotonView's- the level runs fine for the masterClient but the second player's fps is almost frozen it's so slow- I'm guessing its from too many views?


    This is indeed a way to make this working but it's not a good way at all. Having a lot of PhotonViews at least extends the loading times for later joining clients, in your case the second client, and is overall not a good approach. If you however still want to use this approach, you should at least use PhotonNetwork.InstantiateSceneObject(...) instead of PhotonNetwork.Instantiate(...). This prevents all the objects from being removed if the MasterClient disconnects from the game, at least as long as a new MasterClient is available. Please note that this way is still not recommended.

    From searching through the "procedural" posts on this board it seems the easiest way would be to generate everything locally on both players using the same seed I would sync using an RPC


    Yes, this is the best way. You don't have to use a RPC because this requires a PhotonView, but you can. You also have at least two other options which both don't require a PhotonView. You can either use the RaiseEvent function with an OnEvent handler to do the generation or you can use the Custom Room Properties to store the seed in those properties and make it available to all clients this way.
  • Thank you for the detailed reply- I ended up creating a seed when the MasterClient connects using the System time- I then sent the seed to the other player using an RPC- then I generated new seeds using that seed and it all works perfectly- I used the code in this post to derive new seeds from the master seed https://answers.unity.com/questions/603000/generating-a-good-random-seed.html