I'm trying to implement a virtually infinite universe in which a player can roam around in space. I've separated it into sectors, 9 of which are always active around the player, and each sector is 3x the display area (which is 800x800). (Each sector contains nine 800x800 cells, but I'm yet to see whether they're going to be of any use.)

I successfully made it so new sectors are created as the player travels, while keeping only the 9 around him active, but I'm being unable to "pause" the stuff in the sectors that are outside that 3x3 area. I have 1 dummy asteroid (RigidBody2D) per cell for testing purposes, and as I move away and see sectors being "deactivated" the asteroids keep moving and rotating. I tried set_sleeping() and remove_child(), but didn't work.

I'm using a Polygon2D as a sector background and setting its alpha lower/higher to visualize this (I shrunk the cells to 50x50 for testing as well):

The problem really is that the more I travel away the lower the frame rates will drop. I don't yet know how to save them to a file, so I was wondering if there's a way to actually "pause" everything in them in the meanwhile.

Thanks in advance.

(Also open to suggestions on whether what I'm doing is sensible: currently using one array for all sectors (each sector contains an array for its cells). Could potentially further divide the universe into bigger chunks or get rid of the cells or something.)

Check this Q&A post for saving scenes at runtime.

set_sleeping() does work to stop the rigid bodies, right? The problem I see is that they don't resume simulation where they left off when you wake them up again. You would have to put a little script on your asteroid to save its linear and angular velocity before it goes to sleep, and set them back right after it wakes up. (Note, the signal "sleeping_state_changed" doesn't fire when you use set_sleeping, only when the engine decides the body should sleep.)

What you're doing seems sensible to me, I've tried the same thing on occasion. I'm not sure what the point of dividing each sector into cells is, but whatever. And you've probably found out that the size of your sectors needs to be set relative to the max speed of your ship—if you have small sectors and a fast ship, you'll fly through before they have a chance to load. I'm curious how saving and loading scenes will work for you. I've had some lag loading stuff at runtime on a PC with a disk drive.

I suppose set_sleeping() works (it was the first time I tried it), but it's not working in this case, probably because of what you said, that the engine is deciding the bodies shouldn't sleep (their movement and rotation is constant, I guess that's probably why).

Thanks for the link. Gonna try that out and see what I can make of it.

As for the cells, well, I had envisioned a point for their existence... I can't remember what it was though. :) On second thought they may be weighing on the game, since perhaps a sector with a list of 50 entities may run faster (one loop) than a a bunch of cells with a bunch of lists (many loops).

EDIT: also, about the size of the sectors, it seems to be working fine so far, but the sectors are also mostly empty now. I'm worried about how to get a starfield in the back that can have some variation, so I think I may have to be careful to not go too big that it weighs, nor too small that they can't load in time. The ship isn't that fast, but it would be nice to have some super fast ability with a cooldown or something.

Huh, weird. Sleeping bodies should stay still unless some force acts on them to wake them up.

They have linear and angular velocities set from the start, so I guess that's why.

Been trying to save the sectors but I'm having trouble getting them to save the stuff that's in them, and I now noticed they aren't getting saved with the script variables that they have...

6 years later