How do I create a system for setting and splitting tiles in my 2d game from top to bottom?

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

I would like the character to be able to come up and lay or break tiles with the mouse. I will be very grateful if they help me! I can’t even find the basis for this idea to implement it.

Your description is a little vague. Do you mean the player can walk on the tiles in 2D, like Terraria? Or would this be in 3D like Minecraft, but in 3rd person? Or do you want to fake 3D with a 2D isometric view?

TRAILtheGREAT | 2023-04-28 18:03

I apologize for my English. I would like that when I add a survival mode, the character can set and break tiles.

Goose | 2023-04-28 19:57

And all this with a 2d view from above

Goose | 2023-04-28 19:58

So like Rimworld? A flat plane with blocks on top that act as walls and can be dug out, but you can’t dig further down build up?

TRAILtheGREAT | 2023-04-28 20:42

Imagine that this is the installation of tiles in the editor, but only this can be done in the survival game itself by clicking the mouse buttons

Goose | 2023-04-28 21:18

As if it’s a block in minecraft that can be broken and put

Goose | 2023-04-28 21:19

It looks like you need to add user data to the tile (conditionally HP) and change the tile to change them.
Well, and the answer below looks good)

Animaliss | 2023-04-29 09:38

:bust_in_silhouette: Reply From: TRAILtheGREAT

Here’s a script that will allow you to click on a tile and change it to a different tile, defined by its atlas coordinates:

extends TileMap

@export var NEW_TILE_COORDS = Vector2i(3, 0)

func _input(event):
    if event is InputEventMouseButton:
	    if event.pressed:
		    var mousePos = get_global_mouse_position()
		    var tileLocalposition = mousePos - global_position
		    var tileCoords = local_to_map(tileLocalposition)
		    set_cell(0, tileCoords, 0, NEW_TILE_COORDS)

I’ll leave the rest up to you. If you want to choose what tile to place, then you’ll need to create some kind of UI for picking the tile and have it communicate with the script to place the correct tile. If you want items to drop when you break tiles, then you should add a function to this script which spawns an item when it replaces a tile.

Oh, thank you so much! Now I know the basis that I can grasp :slight_smile:

Goose | 2023-04-29 10:56