Problems With GDScript and Singletons

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Superkrypet

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

:bust_in_silhouette: Reply From: yrtv

From TileMap.world_to_map()
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.

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

Superkrypet | 2021-10-01 13:54

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))

yrtv | 2021-10-01 23:12

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

Superkrypet | 2021-10-02 11:16

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

yrtv | 2021-10-02 21:09

getting closer:

Invalid type in function world_to_map in base TileMap

Yeah, that one was annoying

Superkrypet | 2021-10-03 06:54

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

Superkrypet | 2021-10-03 20:52

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

yrtv | 2021-10-03 21:54