Tile map moving with screensize

Godot Version

4.4

Question

Hello,
I’m trying to add a dictionary keeping tile information for a tile map using:

for tile in %TileMapLayer.get_used_cells():
		tileData[tile] = 0

This however returns the tiles location relative to the screen-size and not representative of the visual location of the tile.

This is the TileMapLayer in its own scene:

And this is what it looks like in the Main scene:


The green area is where the TileMapLayer visually appears and the red area is the positions which the dictionary i created above contains.

Is there any way to circumvent this?
I tried manually changing the position of the tilemap however the tile (0,0) will always be at the top left of the screen and i also couldn’t find a function that returns the right tiles for my dictionary when the screen size changes.

Thanks for any help in Advance.

This is not enough code to know what you are trying to do. I will say that whatever you are doing, you are storing that data inefficiently. I cannot tell what you actually want to happen though.

Here’s some questions that would help:

  1. What do you plan on doing with that dictionary of data?
  2. Why are you storing your tiles as the keys?
  3. How come you’ve decided to use a dictionary to store this information?
  4. Why do you care about the screen coordinates at all?
  5. Do you want to store the screen coordinates?
  6. What’s the little red square with rounded corners represent?
1 Like

Thanks for telling me i need to be more precise I will do my best to awnser your questions.

  1. The cells of the map will have “buildings” inside, so the dictionary will store if the cell is empty or which “building” is on it.
  2. The key is the tile location so when placing buildings i can check if this tile already has a building.
  3. I used a dictionary because i thought this would be the most efficient way to store the above mentioned data. (the key for location and value for building), if you have a better way i would be glad to implement that aswell.
  4. I think this is answered from my above statements.
  5. No i dont think this is needed
  6. This is a building which is being placed but is of no importance for the threat.

I need the red area from the picture to overlap with the green one from above no matter the screen size / aspect ratio.

When placing buildings this is how i currently check if building can be placed:

func _process(_delta: float) -> void:
	if build_mode:
		var tile = $CursorIndicator.tilePos
		if tileData.keys().has(tile) and tileData[tile] == 0:
			can_build = true
			$CursorIndicator.turn_green()
		else:
			can_build = false
			$CursorIndicator.turn_red()

A solution ive come up with since posting this is this:

func _ready() -> void: 
	var tileCentered = %TileMapLayer.local_to_map(global_position)
	var x_start = tileCentered.x + 1
	var y_start = tileCentered.y + 2
	for x_range in range(x_start,x_start+ 8):
		tileCentered.x = x_range
		for y_range in range(y_start, y_start + 11):
			tileCentered.y = y_range
			tileData[tileCentered] = 0

This will get the tile at the top left of the playing field and manually calculate the green area from there.
It works , however I thought there must be a easier solution.

Hope this clarified some things and thanks for answering even when i was unclear in m question.

I just found out about custom data layers for tile sets and think this is probably the best way to go about this.

I actually looked for something like this before writing the other code however a thread i found said this is not possible in Godot must have been outdated.

Edit: Nevermind i think you still need the tile location to acess this

1 Like

Could you clarify why this is a Inefficient way to store data?
Im not too experienced with programming.

cheers.

Ok so a Dictionary in Godot is also what’s called a Map or Hash in other languages if you want to read up on them. They are really useful when you have a bunch of keys and values. For example, I want to track my produce:

var produce: Dictionary = {
"apples": 10,
"oranges": 20,
"plums": 20
}

You are using the Dictionary backwards. You are storing the tile as the key, and nothing in the value. You would be better off served using an Array. Here’s a quick video on the differences:

Having said all that, you are doing WAY too much work. First of all, you want to use a TileMapLayer. TileMap is deprecated, that means it is no longer the way you should make anything new, because it is going to be removed in the future.

Secondly, you should just save the TileMapLayer itself. while it will take up a little more disk space theoretically, it is going to be optimized for speed, memory usage, and disk space. Your current solution likely takes up more space than just using a TileMapLayer. And it complicates your life.

1 Like

I’m using a TileMapLayer what do you mean by “just save the TileMapLayer itself” as in save it in its own scene? I’m doing this too.

As for the dictionary how would i use a list instead when i want to check if a certain tile has a building associated with it or not. (the value of the dict is only set to 0 at the start when there is nothing in it later it will have the building…)

I understand that this is over complicating things however just telling me im doing to much work does not help me at all.

Thanks anyway