Can't modify GlobalPosition on a Node2D inheriter (in C#)

Godot Version

Godot version 4.5.1

Question

Hello everyone !
I am working on a Tactical game

Some code I use, that came with a tutorial to complete it according to our needs, is in GDScript, but because it uses signals for the use of a tactical, and for multiple reasons, I prefer to use C#, I decided to rewrite the file in C# in order to use the built in C#

Now here’s the catch. The file is a child of Sprite2D, and here’s the GDScript _process function ;

func _process(_delta: float) -> void:
	global_position = get_global_mouse_position()
	global_position.x -= int(global_position.x) % int(cell_size.x)
	global_position.y -= int(global_position.y) % int(cell_size.y)
	global_position = global_position.snapped(cell_size)

Now, the C# equivalent

	public override void _Process(double delta)
	{
		GlobalPosition = GetGlobalMousePosition(); // Follows the mouse
		GlobalPosition.X -= (float)GlobalPosition.X % CellSize.X; //1
		GlobalPosition.Y -= (float)GlobalPosition.Y % CellSize.Y; // 2
		GlobalPosition = GlobalPosition.Snapped(CellSize);
    }

The lines I have commented as 1 & 2 are the heart of the problem ; Visual Studio Code gives me this error :
Cannot modify the return value of 'Node2D.GlobalPosition' because it is not a variable

How can I modify GlobalPosition then ?

Thank you in advance :slight_smile:

Thanks

1 Like