|
|
|
 |
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.
… 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