0 votes

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.

in Engine by (24 points)

do you want to place a node in editor?
simply you can try it with snap setting.

in the runtime too

1 Answer

0 votes

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:
1. Get the coordinates (x,y) you want to transform.
2. Compute the grid indices i=floor(x/L) and j=floor(y/L).
3. 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!

by (69 points)

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?

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.