I'm working on an hexagonal map in a 3D world, but I don't think that's important. I created a function that returns the distance between two tiles in someway similar to Manhattan distance, but in an hexagonal grid. I checked it for thousands of tiles and works. Now the error cames.
The position of the tile is Vector2(1,1)
in offset coordinates (https://www.redblobgames.com/grids/hexagons/). When I call a method inside the tile script, I get a weird result. To see what is happening, I made something like print(tilePosition, Vector2(1,1))
and I get (1,1)(1,1)
, that's ok. But the method still gives me a bad result, so I wrote if tilePosition == Vector2(1,1): print something
, this if statement is never passed. I make sure that the tilePosition
is a Vector2
using typeof
, and that the method is inside _ready
to make sure that the method is called after the position of the tile is set to the desired one. I don't know what is happening. Any help? Some pieces of code are bellow:
The ''game manager'' that adds the tile:
extends Spatial
var R : int = 2
var rng : RandomNumberGenerator = RandomNumberGenerator.new()
var tilesVpositions : PoolVector2Array
var tilesIds : PoolIntArray = PoolIntArray([])
func _ready():
rng.randomize()
tilesVpositions = PoolVector2Array([Vector2(1,1)])
var tile : Spatial
for i in range(tilesVpositions.size()):
tile = GLOBALS.Tile.instance()
tile.vdisplacement(tilesVpositions[i])
tile.name = tile.ttranslation
tile._init()
#tile.change_center(MATERIALS.WHITE)
add_child(tile, true)
#tile.id = i
tilesIds.append(tile.id)
GLOBALS
is a singleton used by every node, contains the information about the hexagonal grid and many methods (''vdisplacement'' is the function that makes a displacement in offset coordinates). The other script where the if statement is done:
extends Spatial
onready var mesh : MeshInstance = $Mesh
var vtranslation : Vector2 = Vector2.ZERO
var ttranslation : String = '0,0'
var id : int # identification in the world
func _init():
vtranslation = GLOBALS.b_to_v(translation)
ttranslation = GLOBALS.v_to_t(vtranslation)
func _ready():
id = _get_id()
func _get_id() -> int:
var H : int = GLOBALS.hex_dist(Vector2.ZERO, vtranslation)
print(vtranslation)
print(Vector2(1,1))
if vtranslation == Vector2(1,1):
print(vtranslation, GLOBALS.hex_dist(Vector2.ZERO, Vector2(1,1))) # this is never printed
GLOBALS.b_to_v
and similars takes, in this case, translation
(the default Godot Spatial.translation
) and transforms it into offset coordinates.
EDIT:
If I make print(vtranslation.x)
I get 1
, the same for print(vtranslation.y)
See comments bellow in @Thomas Karcher answer.