How can I get a tilemap to 'wrap around'?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By picnic

I have a ‘world’ represented by a 256x256 tilemap. When the player is near the edges of the tilemap I’d like to wrap around the view - for example if the viewport was 20x20 tiles wide/deep and player was at tilemap position (0,0) the top left tile to be drawn would be (-10, -10) or rather (246, 246).

It’s trivial to do if drawing tiles as individual sprites but performance isn’t great and handling zoom etc is less convenient - is there a way to do this with a tilemap/camera set up?

I’ve probably found a way to achieve this but answers are still welcome!

picnic | 2018-07-08 15:31

Glad you found your answer. I’d really like to hear how you do you viewport/camera setup with tilemap :slight_smile: I can ask it as a question if it’s easier.

duke_meister | 2018-07-08 22:26

I decided to do it manually rather than using a camera as the player is moving one tile at a time rather than some number of pixels.

I’m updating the tilemap every time I move the player like so:

	for y in range(-3, 4):
		for x in range(-3, 4):
			var ty = y + player_pos.y
			var tx = x + player_pos.x
			
			if ty < 0:
				ty += Globals.WORLD_Y_SIZE
			if ty >= Globals.WORLD_Y_SIZE:
				ty -= Globals.WORLD_Y_SIZE
			if tx < 0:
				tx += Globals.WORLD_X_SIZE
			if tx >= Globals.WORLD_X_SIZE:
				tx -= Globals.WORLD_X_SIZE			
						
			match world[ty][tx]:
				SEA:
					$TileMap.set_cellv(Vector2(x + 3, y + 3), $TileMap.get_tileset().find_tile_by_name("Sea"))

… etc

That just draws a 7x7 tile view centred on the player but wraps around near the edges of the world[comment2-] array.

I’m actually using the same tilemap to show a zoomed out version of the entire world map when the player presses the ‘map’ key - the tilemap is cleared, filled with the relevant tiles from world[comment2-] then the zoom set to (0.1, 0.1) to display the whole thing on screen.

picnic | 2018-07-09 20:17

Interesting, thank you

duke_meister | 2018-07-11 05:01

I Wish you had posted a photo to show that

Sousio | 2021-05-06 23:32