0 votes

I don't know why but I can't come up with a solution to this one. I trying to store every tile around my moving player.
enter image description here

I'm basically trying to add to
array 0: coordinate -1,1, array 1: coordinate 0,1, array 2: coord 1,1 etc
I somehow can't get to the solution how to do this....

after for y in 3 it checks if the tiles around are free

    var tilePos = tileMap.world_to_map(position)

    var passable = [Vector2.ZERO]

    for x in 3:
        for y in 3:
            if tileMap.get_cell(tilePos.x + x-1, tilePos.y + y-1) == 0:
                var tiles = tileMap.map_to_world(Vector2(tilePos.x + x-1, tilePos.y + y-1)) 
                var list = Vector2(tiles.x, tiles.y)
#               passable.append(Vector2(x,y))
#               print(passable[2])
Godot version 3.3.2
in Engine by (160 points)

1 Answer

0 votes

like so:

var tilePos = tileMap.world_to_map(position)

for x in range(-1,0,1):
    for y in range(-1,0,1):
        if x != 0 or y != 0: # exclude center
            if x >= 0 and y >= 0 and x < map_width and y < map_height: # outside of the map
                var actual_x = tilePos.x+x
                var actual_y = tilePos.y+y


 var coords_around = [
    Vector2(-1, 0),
    Vector2(-1, 1),
    Vector2( 0,-1),
    Vector2( 0, 1),
    Vector2( 1,-1),
    Vector2( 1, 0),
    Vector2( 1, 1),

for c in coords_around:
     if c.x >= 0 and c.y >= 0 and c.x < map_width and c.y < map_height: # outside of the map
           var actual_coords =  tilePos + c
by (4,078 points)

thanks but don't think it is what I asked for or I may misunderstood your code, my explanation was not clear enough...
it was more or less this dilemma... I have for example a 5x5 grid and I need to store those variables.

So if I type: grid[0]
it should return Vector2(0,0)

  • grid[1]: Vector2(1,0)
  • grid[2]: Vector2(2,0)

  • grid[6]: Vector2(6,1)


sooo, that's the script I have so far:

var grid = [Vector2(1,5)]

for x in MAPW:
    for y in MAPH:
        tileMap.set_cell(x, y, 0)   

        var test = Vector2(x,y)
        print(grid[2]) #error

print(test) gives me every coordinate I want, that's good but how do I append them to my grid array?

grid.append(test) does not work:
grid[0] returns the test example: Vector2(1,5)
grid[1] returns 0,0
grid[2] error.

there is some logic puzzle here and I cannot wrap my head around it.

The printed result is whats to expected.

0: the initual vector
1: x and y are zero at first Iteration
2: error because there us no index 2 on the first itetation, therefore it causes an out of bounds error.

Just let he for loops run tgtough. Then print(grid) this will show you the whole contents of the array at once.

.... I spend hours on this issue and the only thing I had to do was setting my print on another line. *big facepalm

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.