I'm developing a top-down RPG, and I have a function within my Character class to allow me to retrieve the character's map coordinates based on their world coordinates. In the editor I am able to set this value and the character's location will automatically update. I would like to be able to drag the character and have it update as well. Is there a way to do this? I'm unable to add a setget to the character's position variable as I cannot overwrite variables defined in the parent class.

I think you can use the _notification function (docs) and check for the transform changed notification (CanvasItem, Spatial). That is what I did for the IK plugin I wrote and I think it worked okay, though it has been awhile so I don't remember exactly.

Hopefully this helps!

@TwistedTwigleg said: I think you can use the _notification function (docs) and check for the transform changed notification (CanvasItem, Spatial). That is what I did for the IK plugin I wrote and I think it worked okay, though it has been awhile so I don't remember exactly.

Hopefully this helps!

Thank you so much for the response TwistedTwigleg! I took your advice and implemented a basic _notification function in my characters so I could check to make sure it fired correctly. Unfortunately it doesn't seem to have worked, as the 'what' variable never seems to equal NOTIFICATION_TRANSFORM_CHANGED. I read the documents you linked and noticed the set_notify_transform() and set_notify_local_transform() functions, however setting either of them seems to have no effect. I did notice in their documentation that it says that those functions enable the notifications for the Node's children. Does that mean that I need to create a child node parented to the character's sprite to handle the notifications?

This following code works for me in a test project:

tool
extends Sprite

var not_snapped_pos;

func _ready():
	not_snapped_pos = global_position;
	set_notify_transform(true);

func _process(delta):
	not_snapped_pos += Vector2(32, 0) * delta;

func _notification(what):
	# Snap the global position to a 32x32 grid
	if (what == NOTIFICATION_TRANSFORM_CHANGED):
		global_position.x = floor(not_snapped_pos.x / 32) * 32;
		global_position.y = floor(not_snapped_pos.y / 32) * 32;

However to get it working in the editor I had to close and reopen the scene a couple times so the code in _ready was called. I don't think you need to create a child node to receive the notifications, though it might be something to look at if the notifications are still not sent to the _notification function in the sprite.

I'm so sorry for the late reply, I've been bogged down the last few days. _notification worked perfectly fine when I recreated all of the character nodes in the scene. Thank you so much for your help! It's really cool to see it working.

3 years later