Find the best set of conditions to unload a chunk that is offscreen.
(Chunk being an arbitrarily sized grid of blocks that all have a variable, named “ChunkID,” that is set to the same value.)
What is the expected result
Chunks load in when they enter the screen with a 40 pixel padding offscreen, and chunks get deleted when they exit the screen with a 40 pixel padding offscreen…
What is the actual result
I’ve tried 2 solutions, both look very similar ‘code’-wise; either nothing looks like is gets loaded at all, or chunks blip in and out of the scene near the edges of the screen. Sometimes, “unloading” doesn’t work at all, and my game lags to death
This is the solution where everything gets loaded then unloaded at the same time, so it looks like nothing happens at all.
Second solution
(fsr i can’t put more than one screenshot, this one looks almost the same, but the third event doesn’t have the “Repeat for each instance of ChunkObject:”)
This is the solution where things blip in and out of existence near the edges of the screen. This solution also sometimes doesn’t ‘unload’ anything at all.
I could be wrong but I think the off screen (inverted) padding should be larger than the on screen. IDK. Maybe the padding just needs to be larger.
Edit: Maybe, the padding can be the same for on and off if the padding is larger. IDK, I’m tired.
Say the red is the screen. You want some padding so an object can be off screen but still alive. Otherwise, an object might just pop into or out of view instead of doing it from outside of view.
But then to delete the object, you want to wait until it’s outside of the padding area. It needs a buffer. Some wiggle room.
That’s a nice simply solution zero. I’d be interested in knowing the optimum solution here - I’ve tried quite a few different methods. My latest game ‘Spindly’ has quite a complicated method! - @ZeroX4 - I think you’d find my method quite tiresome and so i’m not going to describe it! - it’s quite elaborate and involves a deliciously half-baked knowledge of gdevelops shortcuts
The events you see aren’t an extension, they’re just scene events. Although for this to work, you need the “IsOnScreen” extension, the “ObjectPickingTools” extension (I modified the extension from what you might see if you were to download it from the extensions tab), and my own custom extension which is used to generate and load the map data (among various other functions).
I only need a better set of conditions to check for to delete the not-onscreen blocks.
I have tried a new solution, which handles the individual blocks. It’s problem is that when you go away from a chunk them come back, half of the chunk will be deleted since all blocks in that chunk didn’t get deleted.
I’m doing this …it’s a bit hard to describe and the events are so hard to digest that I attempted to put on a second pair of glasses while reading them. See pick. At the beginning of the scene everything on the editor is loaded into a structure in an array that is cellular and currently each cell is 200 pix high and 200pix wide. The cellular world has a width and height and to check the cell above (from A to B) you subtract world width and the check cell D to the right you add 1
The player cell is called pcell. The players X and y are recorded when a world data check has been run.
In the events when the player is in a new cell and has travelled further than 180 pix from the last world data check. I scan the array according to which direction the player has travelled. So if the player has travelled right I check the hashed squares to see if the Boolean variable for those cells ‘created’ is false …if it is I read the items in that cell of the array and create them. Then I scan the cells on the other side to see if those cells have created ‘true’ …if so I pick all objects with an id equal to the cell no and delete them.
This all happens over 5 frames …unless you’re falling …when it happens over 1 frame
I need a fast and reliable set of conditions to check for, to see if and when I should delete a set of blocks (preferably, the set’s size is one chunk).
I almost have something currently but it makes the game super laggy, I’m not sure if it’s the number of blocks in the scene, or the actual dynamic chunk generation (as I call it).
I took some inspiration from @petlimpet 's idea, which involves only loading and unloading chunks when the player crosses a chunk border.
This solution’s issue is that it experiences lag spikes when crossing chunk borders, but otherwise, it is highly performant and reliable, with no bugs recorded.
I’ll try to make the system work without the ChunkObjects so it’s even less laggy.
sorry that It crashed you computer! - was it a ‘repeat while’ that did it?
I started with the data being associated with objects - but then changed it to the cellular system above.
here’s the formula that i use to find the objects cell
here - scenery is the group containing all objects that are being created/deleted
newworldwidth is the number of 200x200 (newworldcellwidth x newworldcellheight) cells horizontally
What i didn’t mention above, as i forgot! is that i’m actually using
array.array.structure.
world[cell][objectcount[cell]].
after object added to cell objectcount[cell] add 1
If you want to see this working …
press 1/2 to zoon out/in
If you experience lag - it uses lots of repeat events to generate animations using shape painter - some browsers/systems don’t like this at all. Runs fine on my phone - doesn’t run at all on my ipad!
I’ve played around with this system for quite a while and i’m still working on it! I’ve lost plenty of sleep over it …waking up thinking -‘what if i try this out?’
i’m very interested to see if anyone can come up with something better!
if you’re doing ‘has entered new chunk’ its better to also include a check that distance travelled from the last check is greater than at least width of chunk/2. Otherwise when you’re near a corner of a chunk its checking when it doesn’t need to
I think the showed solutions are too hard and complicated.
I made in the past a simplified remake game of Canabalt, with another games engine.
I remember having drawn on paper the manner to manage the scrolling platforms going to the left.
But i don’t know if that can be useful for the need of Necromancer.
I did not keep this drawing which was adapted to horizontal scrolling of platforms.
The way I check if the player crosses a chunk border is that I have two values, one is always the current chunk ID (We’ll call this value “ChunkID” and the other we’ll call “CurrentChunk”). If CurrentChunk isn’t the same as ChunkID, then we know that we have crossed a chunk border, then we update CurrentChunk to ChunkID’s value.