My game divides levels up into tons and tons of discrete chunks, and generally speaking one chunk will correspond to one single room. To that end, I simulate NPC movement like Skyrim by calculating where they’re supposed to be based on schedule and state, moving them to the OccupantsList in my room class that retains reference to every NPC occupying a room, and when the chunk is loaded, a routine queries OccupantsList and spawns every NPC in it.
This works fine, but I’m running into a bit of a labor problem: I have one big GameSimulation class which is a primary workhorse for the game, among many other things it keeps a list of Room classes, each one corresponding to a single room in-game. Each Room has a single OccupantsList, but where I’m having trouble is figuring out how to tell the game which chunk corresponds to which Room.
At present I do this by placing a spawner object in each chunk, and giving the Spawner.cs a single variable of type Room, which holds the reference to which Room in GameSimulation this chunk corresponds to. This works great for prototyping, but my scope calls for hundreds or thousands of rooms, and it seems like a terrible idea to sit there using the inspector to manually assign each reference: there’s tons of room for mistakes, and it forces me to repeat labor every time I add a room or change something.
To that end, is there an obvious solution I’m missing to assign rooms programmatically, so that I never have to worry about hard-coding spawners?
I kind of have this already: I stream the entire level as an optimization, so whenever you get close to the boundary of your current chunk, it starts to load in the next one. Spawning works as part of that, as the spawner gets loaded and unloaded with its chunk, so I put the function that finds out which NPCs are supposed to be nearby and spawns them in OnEnable, and the one that despawns them in OnDisable, so they get pulled from the simulator whenever their chunk is loaded.
That part works, but I’m still doing a lot of drag & dropping by hand to tell it which chunk correlates to which room class in the simulation.
There isn’t a predictable layout to the rooms, unfortunately, which is why I’m suspicious that this might need to be hard coded- the map is a city, so it has extremely variable geometry. Chunks are basically retrieved based on portals: every chunk is a bounding box plus a number of portals signalling transitions to other chunks, so given a single chunk it’s possible to determine what chunks it’s connected to, but the streaming system alone doesn’t really give enough context to determine what the adjacent rooms actually are. I flirted with the idea of putting the room classes on each chunk’s bounding box instead of centralizing them in the simulator, but I don’t like the idea of spreading what is fairly critical data around like that.