Expanding tiled background based on an object position

Hi guys,

I am new to GDevelop’s world and this is my first post. I tried to look for an answer to my question but could not find one.

I am trying to make an “expanding tiled background” in a 8-direction top-down enviroment based on the position of a moving object (player-controlled sprite).

To better explain what I am really trying to do I am posting the following image:

The circle is the moving object and the square tile is a tiled sprite acting as the floor.

I would like to expand the floor (background tiles) towards the direction the circle is moving in (in this case towards the top-left corner) while dropping the ones outside the camera (the no longer visible bottom right corner). Well I guess “deleting” the tile outside the camera view is secondary, just to avoid wasting resources.

I tried changing the floor size in relation to the camera’s width and height, and it seems to be working but only going down and right, so this doesn’t seem to be the right way to go.

For the time being the layout is very simple. I have 3 layers:
“BG” for the floor
“Base layer” for the moving circle
“Background color”

1 tiled sprite (Floor), and 1 sprite (Obj - the circle)

And the events (sorry I couldn’t post the second image this being my first post):
Always: Center camera position on Obj (the red circle);
Always: Change the X position of camera (layer: “BG”): set to CameraX();
Always: Change the Y position of camera (layer: “BG”): set to CameraY();
*Just messing around with size and cameras:
Always: Change the size of Floor set to CameraX(“BG”)xCameraY(“BG”)

Does anyone know how I could make this happen, or know perhaps some function or plug-in that I could use to achieve what I want the game to do?

Thank you in advance for your help.

Hi and welcome here :slight_smile:
I would make two nested repeat loops, to test all the points surrounding the player, and create a floor tile whenever there is none.

The two nested loops are to test all the coordinates, one after the other. One level is used for X, the other level is used for Y.
Regarding the tile deletion, simply destroy the objects when out of camera, there’s a behavior for that.

It might get laggy, though. :grimacing:

1 Like

That is one elegant solution and a very good starting point. As you said it gets a bit laggy and I might be missing something since I am experiecing some offset to the left, I will tinker some more to see what can I do.

Thank you very much for taking the time to answer!

Yeah, it could use some tweaking… Maybe only test the edges, like:
first line - all slots are tested.
last line - all slots are tested.
other lines in between - only the first and last slots are tested.
Because we don’t need to test all the slots that are close to the player, we know they’re already filled.

The offset you noticed is perhaps because we’re testing the origin point, which is the upper left corner of the tiles.