Infinite tilemaplayer scrolling

I’m trying to make an infinite scrolling background for my 2d top-down game, but for some reason, when it goes back to its original position, it causes the tilemap to kinda stutter/disapear for a second. Here is the code I’m using. I’m also moving it diagonally; it looks better.

extends TileMapLayer

@export var speed := 16.0
@export var tile_size := 128.0

func _process(delta):
	position -= Vector2(1, 1).normalized() * speed * delta

	if position.x <= -tile_size:
		position.x += tile_size

	if position.y <= -tile_size:
		position.y += tile_size

Here is some footage

Is the tile’s center at position or is it the edge of the tile? Subtracting a full tile_size would only be right, if the tiling starts at the edge of the tile.

You probably also want to use some sort of modulus operation instead of infinitely incrementing position. For this you can use % or fposmod().

Is this the full code or is there more to it?

Actually I take that back. Subtracting a full tile_size should work no matter what offset you have.

Does the stutter happen repeatedly or just once at the start?

Yes, this is all the code I have, and the stuttering happens repeatedly.

Also sorry for the late reply

That is odd, because I tested the code and it works fine for me.

Did you verify that your tile dimensions are actually 128 by 128 pixels in size?

Yep, they are exactly 128x128. I just double checked

I think then I need to see more of the project or get more clues in order to reproduce the problem. For me it seemed to work so far. That’s odd.

what do you need?

Hard to say, what do you have to offer? :wink: A public repo on any Git server would help, like Github or Codeberg for example.

I havn’t set up my github repo for this game just yet, but I could send the zip file of the project, is that fine?

here is a zip file of the project https://drive.google.com/file/d/1I0FejuNBVd-UO4BBpWGGbYN6W68cGfZ5/view?usp=sharing

btw it takes between 10-15 seconds I think, for it to stutter

The best tool for this job is a Parallax2D that has build-in scrolling.

In the properties set the Repeat size to 128x128 and the Repeat Times to 3.

You can comment out the TileMapLayer’s code.

Instead attach a script to the Parallax2D:

extends Parallax2D

@export var speed := 16.0

func _ready():
	autoscroll = Vector2(-speed, -speed)

That fixed it for me.

What was the problem? I don’t really know. But since this is the right tool for the job, better go with that. See if that solves it for you.

thank you so much!!!