I want to make a game in the style of construction games like “dune 2 the battle for arrakis”. But I don’t know how to make it so that when you press a button, an object is created on the tilemap. And at the same time, so that you can interact with him. Tell me please…
Having said that, the simplest example that I can offer is this:
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
curent_mouse_event = event
else:
curent_mouse_event = event
func _process(delta):
var hovering_tile:Vector2i = get_node(“tilemap”).local_to_map( get_node(“tilemap”).get_local_mouse_position());
if curent_mouse_event.pressed:
get_node(“tilemap”).set_cell(1,hovering_tile,1, Vector2i(0, 0),1)
You will have to look into set_cell and see what properties you need to change and how.
First you have to set up 2 layers on your tilemap and second identify the id of the tile you want to use.
thank you for your help and advice. As for parsing with tilemap, I know how to get cell coordinates and work with layers. I just had a question, what if I need to add xp of buildings, or other data. How will I do this then? Will I place a dummy over the building and read the values from it? In any case, thank you very much for your answer.
I see,
In that case, you may want to try to use scenes as tiles.
This is a little bit more complex.
Here is a summary of how this would go:
You create a scene of a building that has a collision (lets say area2d)
On that scene you will need to have a script with a signal for the collision.
You then add that scene to you tiles and place that tile into the “world”.
Now when a collision happens that code will run. Could be something like:
“When a player clicks on a building upgrade the building.”
There is a limitation to this tho:
You can not access a tile as a node directly, the tilemap does not store that information.
So for example you can not do something like: “if the player clicks on a tile go into that scene and increase the xp of that building with 20xp points”
What you can do is something like this: “when a node is clicked increase the xp of this node with 20xp points”.
There are some “ugly” workarounds to this problem
I am sure you will find a lot of these cases while working on this, so I will not go into more details.