Topic was automatically imported from the old Question2Answer platform.
Asked By
Kompaktive
I’m planning to make a tower defense. The player can move freely but the building must be placed by grid.
However, the tilemap node is very limited for what I’m needed right now. I can’t make the script to work on tiles, and I can’t figure out how to convert placed tiles into a non-tiles entity without changing its position. Then I’m unable to create an object silhouette that follows player by grid.
So, is it actually possible to make a grid-based game without tilemap? Or if tilemap is the only way, is there any workaround to those problems? Thanks in advance.
do you want to place a node in editor?
simply you can try it with snap setting.
Let’s see if I understood your issue: you want to have a grid but you don’t want a Tilemap for whatever reason.
You can convert ANY set of coordinates (x,y) on the screen to a pair of integer coordinates (i,j) that correspond row and column (and viceversa) using some simple maths.
Basically, let’s say you have a grid where each square has size L. I want to build a building, and I click at the coordinates (x,y). We know that the x position of thei-th tile is x=i*L, so we can get its index as i=floor(x/L). In the same way we can get the index of the y-position, j=floor(y/L). Then, recompute the coordinates the tower has in the map. Try to draw in a paper and do the computations yourself, it is very easy!
I repeat the procedure to be clear:
Get the coordinates (x,y) you want to transform.
Compute the grid indices i=floor(x/L) and j=floor(y/L).
Your tower will be located at (i*L, j*L).
In this way you can get a grid from your script, without using any Tilemap. Ask if you have any additional question!
The answer is clear, thanks. However, after reading yours, I think implementing grid without tilemap is a bad idea. 2 days ago, I kept searching around while waiting for the answer and found map_to_world, says it gets the X, Y coordinates of the cell I stand on. Can I place a node using map_to_world?
Kompaktive | 2019-02-03 23:43
Sorry for the late response! Yes, you can use map_to_world. Actually I am almost sure that this function does the process I sketched above.
Be aware that you also have a world_to_map that does inverse stuff. I didn’t know that the TileMap class included these, I’m but pretty sure they do what I told you.