0
\$\begingroup\$

I had three basic questions regarding the creation of a 2D sandbox/openworld game much like Terreria.

Only looking for a general overview/opinion, nothing too detailed. Any links to literature that would help answer the questions below would also be helpful.

First off - Keeping track of the map using regions/chunks, lets say 512 x 512 chunks. Is it recommended to pre-load the surrounding chunks of the 'chunk in focus', so if the character enters either one there is no load time? ~Or do you simply load smaller slices of the chunk next door, in the direction of movement?

Secondly. Do I need to add a collision box to every tile that is preloaded so any NPCs that are off screen but 'next-door' can interact with the environment? Or is this going to be massively CPU intensive ? (512 x 512 x 9 tiles alone)

Third and last question. Off screen RANDOMDLY GENERATED NPCs/monsters I assume dont load and render as they enter the viewable area on the active chunk but are loaded and activated sometime before they are seen. What are the general strategies use to handle this?

For Reference: Using c# with Unity, no third party frameworks/plugins.

Many thanks John

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Preloading

Preloading is a touchy subject. It allows for simulation and fast loading but is subjected to the user's memory availability. If you use Minecraft for example, the preloaded area is the active chunks surrounding the player. The catch of preloading is that if the chunk is not loaded into memory, its not simulated.

Preloading can me summarized into a balance of the following items

  • The need to simulate
  • The availability of memory
  • The requirement of continuity (whether the player have to pause and load the next chunk)

Ill talk about these 3 points briefly.


Simulation

This depends on the accuracy of the world you are creating. Whether the world need to "continue" when you are not watching it. Most tile based games have a set area that confines the player to avoid this issue. The only area it needs to simulate is the only area the player can play in. But if you need to have a large/open world there is two solutions I have seen before.

  • Discard continuity and only simulate the area you are playing in
    • Examples of this is Minecraft. Where crops dont grow unless theres a player in the area.
  • Discard accuracy and simulate in a lower resolution
    • I cant give an example of this but in a project I previously worked in, open world AI is simulated in a lower resolution. They have no colliders, no physics and path finds in a limited fashion. The only issue with this is that you need to resolve any issues that happens during the limited simulation when they are loaded into real simulation. ie. whether they morphed into a wall when they have no collision.
    • Check out this question for a more detailed explanation for your second question
      Good way to handle offscreen AI?

For your third question, since your design of the world is chunk based, your reference would probably be the entire chunk. Load your AI together with the chunk so that the AI can interact with the chunk. Apply the lower resolution simulation if necessary for AI outside the current chunk.


Rendering

On the subject of rendering stuff outside the screen, A simple positional calculation would serve the purpose with the least computation requirement. Simply not render any object that is X distance away from the center of your screen (Assuming it is a top down orthogonal view). For 3D solutions id recommend frustum culling as a solution.

Generally speaking Unity does a great job of doing this automatically. You can keep sprites and objects active and they wont render unless they are within view of the camera.


Afterthought

Do note that you are dealing with an optimization issue. While this is good, as Donald Knuth would say, Premature optimization is the root of all evil

Unless your AI is highly complex, most games are able to simulate a significantly large area at 3-5 times speed with no issues. Consider other solutions like multithreading if your AI is too compute intensive. One interesting strategy I have seen in an old game is to slow down the game when its really busy.

Most games suffer from not properly using the available CPU cores than actually have issues with simulation.

\$\endgroup\$
2
  • \$\begingroup\$ Many thanks for your quite detailed answer, lots to think on. Regarding the AI, I will be trying to simulate minimal items for monsters. For Example I would just want to spawn them randomly in an area they are capable of spawning (Sky, top of ground and in underground voids) and simply have them mill around within a given area and then simply have them take interest in the character when he/she is within a given range. For this obviously I would need to know the layout of the environment ahead, hence I assumed preloading. \$\endgroup\$
    – John Cogan
    Commented May 1, 2018 at 14:16
  • \$\begingroup\$ If the active chunk is not large enough for the enemies to spawn in and you require them to spawn outside the active chunk then you do need to load the chunks for them to correctly interact with the environment. But referencing rimworld, you can simple spawn them at the border of the active chunk and the player would be none the wiser. A simple trick is to apply fog of war to the parts that the player isn't aware of and simply spawning the enemy at the edge of the fog. This will reduce the simulation required to have the enemy wander into the player's view. \$\endgroup\$
    – DarkDestry
    Commented May 3, 2018 at 2:03
-1
\$\begingroup\$

I am not sure about first and second question, but for third question, you can fix a position for event trigger on x so when your character enter the region, the NPCs/Monsters will be loaded and rendered.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .