tutorials for hexagon shaped tilemap in godot 4
-
Hi,
I'm trying to create a game where 2 players can move units on a hex grid map (like those old strategy games).
It seems like there are no tutorials on how to work with a hexagon tilemap. I have only found some tutorials on how to make units move on a square grid. I found out 3.5 didnt have hex tilesets. would it be better to go for godot 3.5 because there are more tutorials for that version or is there still hope for me?I'm trying make a unit "stick" to the grid and then limit its movement to the grid.
I managed to get a cell id with the following code.
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
var global_clicked = event.position
var pos_clicked = local_to_map(to_local(global_clicked))
print(pos_clicked)but the local_to_map command doesnt work in a characterbody2d.
I then tried to make my unit a tile on another layer in the tilemap but I got stuck there too.I realise I might be in over my head but any help is much appreciated.
-
The reasom local_to_map doesnt work om the characterbody is because they are not methods that are part of it, if you want to use them, you have to run them using the tilemap for example $hex_tilemap.local_to_map()
-
thanks for your reply!
I now send the click location to the tilemap with the following code:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
var global_clicked = event.position
gethex.emit(global_clicked)To test this my code in the tilemap is the following:
func converthex(target):
print("cord clicked is " + str(target))
var hex = local_to_map(target)
print("hex clicked is " + str(hex))
var convert = map_to_local(hex)
print("unit is moved to " + str(convert))
hexconvert.emit(convert)this gives results like for example:
cord clicked is (355, 132)
hex clicked is (3, 11)
unit is moved to (352, 144)My problem now is that the unit is moved to a coordinate that is not within the hex i clicked.
hope this is clear -
@mikael-Lundgren said in tutorials for hexagon shaped tilemap in godot 4:
thanks for your reply!
I now send the click location to the tilemap with the following code:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
var global_clicked = event.position
gethex.emit(global_clicked)To test this my code in the tilemap is the following:
func converthex(target):
print("cord clicked is " + str(target))
var hex = local_to_map(target)
print("hex clicked is " + str(hex))
var convert = map_to_local(hex)
print("unit is moved to " + str(convert))
hexconvert.emit(convert)this gives results like for example:
cord clicked is (355, 132)
hex clicked is (3, 11)
unit is moved to (352, 144)My problem now is that the unit is moved to a coordinate that is not within the hex i clicked.
hope this is clearupdate: I figured out moving a camera2d influences the coordinates. Do you know how I can get the coordinates relative to the whole map instead of my current view of the map? thanks in advance!
-
Yes, you can use get_global_mouse_pos() to get the global position of the mouse