Trying to make an area 2D alter the tilemap

Godot Version

4.1.3

Question

So I'm making a game where I need the player to detect which tiles in the tilemap they are in contact with; as well as from the projectiles they fire. I'm doing this in the form or area2D nodes to detect and get the location od the tiles that they come in contact with. Two problems arose: 1. the area2Ds are only showing the closest tile, 2. I don't know how to use this information to alter the tilemap.

Unfortunately, this will only return the TileMap itself, not the tiles themselves. So this approach can only be used to query if there’s any collision at all, but not where!

You could write that yourself, but it’s somewhat involved and prone to errors, so I usually recommend looping over the relevant tiles beforehand and spawning a separate body for each of them. Then your approach works fine.

1 Like

could you explain me a bit more about your game…because I have a solution but I need more details

So it’s not fully clear but it sounds like you want to do two things:

  1. Determine what tile on a TileMap exists at a specific point (that is the same as some other Node’s location)
  2. Know how to change that (or other) tiles at that position

The first question can be done by defining a point you want to check and then figuring out which cell that point is at on the TileMap. There’s a lot of things that can affect this (viewport, camera, tilemap positions) so there’s not an easy answer without knowing more. That said, one fast way would be to get a specific global_position and then use TileMap.map_to_local() (see the notes about converting from global_position) to figure out the cell location. That cell location can have a tile (or be blank) on the TileMap.
Once you have that info, you will either need to use the tile’s atlas location or TileData to know “what tile” it is.

The second question is more straightforward once you know the information for the first, of course. Changing a tile on a map is as easy as TileMap.set_cell(). Look into that more to see how it works.

Good luck!

What kind of information about the game do you need? I’ll just start with this.

Wherever the player goes, it should leave a trail of oil on the ground. The player will also have projectiles that will also leave oil on the ground. The player will be able to sink into and move around within the oil, so long as there is oil on that patch of ground.

This was just the first idea that I had for this, to change the TileMap, changing the ID of the tiles that will be covered in oil. But if there is a different, easier approach, please let me know.

Ok. If you want to use the mouse to know the location of your character this code can help you but you can use the character’s position to know the vector when he arrives the end of the path “move_and_slide”.

var clickCell :Vector2i = tileMap.local_to_map(tileMap.get_local_mouse_position())
var cellCharacterPosition :Vector2i = tileMap.local_to_map(character.position)