adding coordinates to a vector array

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

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])
:bust_in_silhouette: Reply From: klaas

Hi,
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

or:

 var coords_around = [
    Vector2(-1,-1),
    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

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)

etc


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)
		grid.append(test)
		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.

Wurzelpilz | 2021-08-02 12:46

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.

klaas | 2021-08-02 13:01

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

Wurzelpilz | 2021-08-02 13:30