0 votes

Hello !
So I have read that worldtomap isnt global and I have to use to_local for worldtomapand and to_global for maptoworld.

But it changes nothing...

Here is my set up :
my project is phone size project. My elements are 3x bigger for scaling purpose.
So my main node is scaled X and Y to 0.389.
Everything works great, path finding of the unit on the map depending on the type of tile etc..

But, When I'm creating my build tool for elements to add on the map (based on tile positioning) turns out the info coming back from worldtomap (to local or to global) are as if my nav tile was not scaled to 0.389 but a full on 1.
I tried scaling down nav : nothing. tried scaling down timemap_nav : nothing.
I'm running out of ideas.

Here is a little video of how it's looking like.
You can see that the tile aren't matching the tile size (green and orange background), as if the worldtomap info was never scaled down to the project size

I read somewhere that someone found a "haccky fix" where they divide the return or worldtomap by the scale desired. But worldtomap returns me tile cords like 2,6 not cords...

Any idea ?

Here is my code :

            var mouse_pos = get_global_mouse_position()         
        current_tile = map.world_to_map(to_local(mouse_pos))
        var id_tile = map.get_cellv(current_tile)

        $build_tool.global_position = map.map_to_world(to_global(current_tile))

(yes i have tried to invert the tolocal and toglobal, and also not using them.

here are my nodes

  • playing (scaled 0.389)
    nav >tilemap_nav

I'm sure it's something simple... But I have bene on it since yesterday and I'm getting blind.
Thank for your time.

Godot version 3+
in Engine by (184 points)

can i highlight that if you are using the scale property to scale everything to a "phone screen" size, you could simply set the camera zoom instead?

1 Answer

+1 vote
Best answer

Ok there are two issues:

First, the map_to_world() function expects a local input and will give you a local coordinate as output. However, you call the to_global() method on your current_tile and therefore changing it. Instead you would need to make the result of the map_to_world() function global:


Secondly, the returned position is local to the map, not to the script you are running the code in. So instead of using that scripts to_local() or to_global(), you should use the one from the map as map.to_local() or map.to_global().
Your full code becomes:

var mouse_pos = get_global_mouse_position()         
current_tile = map.world_to_map(map.to_local(mouse_pos))
var id_tile = map.get_cellv(current_tile)

$build_tool.global_position = map.to_global(map.map_to_world(current_tile))
by (54 points)
selected by

That make so much sense once you pointed it out. ; first time workign with tolocals and toglobal.

Thank you very much.
It is now working !

Question, if you have time for it:

making the map, I had to move my tilemap_nav a few pixels to the right to align the tilemap with the design of the background.

It looks like this : https://i.ibb.co/JHf2z1w/tile-position.png

And I have just realized that my enemies path finding and now this tile positioning is not reflecting the proper positioning of the itlemap.

Any input on what to do ? Even vaguely.
thanks :)

no Idea about that, it might be worth creating a serparate Question for that

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.