Hello Sanctuary seekers,
I thought I'd kick off the devlogs I'll be posting here by telling you a little bit about my world streaming.
This is aimed more at fellow developers who may want to do something similar.
There will be code snippets but I'm not going to share the whole thing.
I don't believe it would be any good to anyone else as full code as it's integrated with my game for my specific use case.
The world is 8km by 8km (currently).
It's made up of a grid of terrain tiles.
I have a development scene where I can edit the terrain, place gameobjects and basically create the world.
When I want to test, I fire off my editor script which collects all the terrain tiles, gameobjects and anything else and packages them all into seperate scenes.

It also creates a JSON file which holds information about each scene.

This is what the WorldStreamer class uses to load and unload these scenes around the player as he/she moves through the world.
Inside the WorldStreamer, we create a dictionary to hold the data in the JSON file in memory, otherwise We would be grabbing the scenes from disk each time they are required (not slow but higher cost).

We load in the tile meta data

and populate the dictionary.

We start a coroutine to load the initial scenes, passing in the spawn position of the player from the player.

At the moment, this loads the player to a random POI. Sounds stupid but I only have 1 POI in the list atm.
I'll probably never use the random POI spawn but it's there if I need to.
Once the initial tiles have loaded around the POI, the scene streamer fires off an event action to let other classes know the game world has loaded.
We also start the scene streaming loop.

The player spawner listens for this event and spawns the player.
It also fires off it's own event to notify other classes which depend on the player being active in the scene before they can work.

We also register the player with the scene streamer properly so it knows where we are in the world and how far away from origin.
If we get too far away, everything is shifted closer to the origin to avoid floating point calculation errors.
Anything > 3k unity meters from 0,0,0 is ropey at best and glitching happens because the floating point calculations become inaccurate.
The streamer looks for movement and also runs on a timer. but only if the player is moving.

It then happily loads and unloads the world around the player.
This is just the basics really, it does other stuff like loading game objects and AI in over time instead of trying to do it all in one frame.
The real cost with activating game objects is the physics so this has to be done carefully otherwise the player will get stutters so I limit the max activations per frame.

This keeps everything running smooth.
The streaming class also assists with loading into indoor scenes and back out of them into the open world.
I'll probably post more about that in the future.
If you have any questions please feel free to post them in the comments.
Don't forget to join us on discord https://discord.gg/S8cCdyzB
Until next time,
Stay safe.
0 comments