0 votes

Hello, I have an entity that isn't bound by input from the player, instead, it spawns from a tile on the tilemap and changes direction on collision. When the entity is instanced, I would like to have some sort of way to access some data from the tilemap. I have tried making a tilemap singleton but it was causing unwanted behavior.

enter image description here

I want the entity to know about its neighbor tiles specifically, up, down, left, and right.

Godot version godot 3.4.2
in Engine by (26 points)

1 Answer

+1 vote
Best answer

You just want to get built-in tilemap data like tile index ?
All You need to do is to translate entity position to worldtomap ( which You already did since entity is aligned ti tilemap ) and do something like this :

var pos = #translated position here
var neighbours = []
for x in range(-1,1):
     for y in range(-1,1):
          var tile = pos + Vector2(x,y)
          if not neighbours.has(tile) and tile != pos :
return neighbours

number in range() can be replaced by any number and it will indicate size of a square within You want to get neighbouring tiles. This actually checks for neighbours in 8 directions. IF You don't want diagonals You can replace whole double for loop with :

for x in [Vector2(1,0), Vector2(-1,0),Vector2(0,1),Vector2(0,-1)]

neighbours will contain array of all tiles surrounding entity. You can iterate thrrough this array to ask tilemap for any data regarding these tiles, like :

for tile in neighbours:
      if get_cell(tile) == -1:
by (7,447 points)
selected by

Hey Inces! Thank you so much for this response, but the problem im having is when the entity is instanced I have no way of updating its position during runtime.

enter image description here

So my structure is like this:

enter image description here

its cropped here but I clicked on a block and it spawned in an entity(its called an orcho)
and added it as a child of tilemap. Am I structuring it badly?

But You did code collision bouncing behavior somewhere ? I thought entity has a script. I would add a script to entity and make setget variable indicating current tile like this :

var tilepos = Vector2(0,0) setget settilepos

func physics process():
       self.tilepos = $Tilemap.world_to_map(global_position)

      if tilepos != value:
            tilepos = value
            #your methods happening on entity approaching new tile here

If You don't want to script entity You can do it from higher scope, but You need to keep reference to entity when adding it as a child, and get/translate its global position in the same way

Thank you so much!!! Turns out I needed to use the _init() method since I didnt want the parent(tilemap) to wait for the instanced objects during runtime. Thank you so much for your help!

Hey, do you have a discord? If you are not busy could you help me out with another problem?

You are welcome :) I don't have discord, I only have a time to answer forum questions from time to time, so post it in Questions ;)

Got it! I was working on my game and it seems that my solution was only for the short term since it caused behavior I didn't want. the setter function that you have above, I cant use that since my entity doesn't know the location of tilemap during runtime. I have a feeling that I am doing this the wrong way. enter image description here

In the remote tree the entity's (orcho's) are not a child of tilemap. So the problem that I have is during runtime, the orcho's dont know anything about the tilemap except for the built-in collision that I added to the tilemap.

But You can use it, all You need is proper reference leading to TileMap, introduced as onready variable. If I see correctly, TileMap is a sibling of entity.
In entity script :

onready var tilemap = get_parent().get_node("TileMap")

Wherever TileMap is in the scenetree You can always input its proper path into reference like this and then in my setter replace $Tilemap with this tilemap variable
By the way, what is the problem with TileMap being parent of entities ? It will propably make future sollutions easier.

OMG... I finally understand! Thank you so much I got it working!!! When I had it as a child of tilemap there was no real problem I just didn't know what to do before the step that you just gave me. Ok, its official it works for real this time.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.