The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I have a script and it looks like this

extends TileMap

var BreakPalmPosition
var BreakPalm

func _process(delta):
    if BreakPalmPosition != null:
        BreakPalm = world_to_map(BreakPalmPosition)
        set_cellv(BreakPalm, 0)
        BreakPalmPosition = null

And i saved the tilemap as a scene and activated singleton. That way i can change the BreakPalmPosition variable to the position of the palm im breaking. That looks like this

Saplings.BreakPalmPosition = position

And Then its changed back to null again after its used for some reason when i do set_cellv and then the position as the new variable linked to the tilemap via world_to_map it dosent set the cell. Probalby its a very obvious problem and im just stoopid but if anyone knows why it dosent please let me know

Godot version 3.3.3
in Engine by (49 points)

1 Answer

0 votes

From TileMap.worldtomap()
Returns the tilemap (grid-based) coordinates corresponding to the given local position.

To use this with a global position, first determine the local position with Node2D.to_local:

var local_position = my_tilemap.to_local(global_position)
var map_position = my_tilemap.world_to_map(local_position)

If BreakPalmPosition global position, then convert it first.

by (887 points)

Just a quick question/more details. The Saplings is what my tilemap is called and that line of code is from the palm script itself. im not using Global_position because then i would get the position of every palm i want just the one that im breaking ive figured out that part. And what is the

var local_position = my_tilemap.to_local(global_position)
var map_position = my_tilemap.world_to_map(local_position)

script attached to, the tilemap or the palm or something completely different?

sorry again for being an idiotic noob

I just quoted relevant part of documentation. Script is example from documentation, it assumes script is attached to TileMap.

If I understand your code correctly, then:

In Singleton:

Saplings.BreakPalmPosition = global_position

In TileMap

BreakPalm = world_to_map(to_local(BreakPalmPosition))

Yes Exactly.
only problem is that it dosent work, instead it returns an error:

Invalid Type of function to local in base tilemap. cannot convert from nil to vector2

Try converting position before passing to TileMap

Saplings.BreakPalmPosition = Saplings.to_local(position)

And use Vector2.INF instead of null in TileMap

extends TileMap

var BreakPalmPosition = Vector2.INF
var BreakPalm

func _process(delta):
    if BreakPalmPosition != Vector2.INF:
        BreakPalm = world_to_map(BreakPalmPosition)
        set_cellv(BreakPalm, 0)
        BreakPalmPosition = Vector2.INF

getting closer:

Invalid type in function worldtomap in base TileMap

Yeah, that one was annoying

Actually No. I forgot the INF detail.

With that code where back at the start. No Errors, No action and nothing working

somehow i feel like im just wastin your time

Debugger is your friend. Check BreakPalmPosition on every step of it;s way to Tilemap. Make sure it's global 2d position all the way until Tilemap.to_local() call

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 Frequently asked questions and 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 [email protected] with your username.