Adding two Vector2

Godot Version

4.2.2

Question

This is probably trivial, and I’m surely missing something as I’m a complete newbie to Godot, but I have a question about how and when Vector math is executed. I have this code in the _ready() func of a Node (actually a AnimatableBody2D):

print (position)
position = position + Vector2(1.0, 1.0)
print (position)

and I’m wondering why the output is this:

(-33, -9)
(-33, -9)

where (-33,9) is the position of the object in the scene.

When does Godot execute the Vector math?

Because if I do the same thing with an integer, I get the expected result in Output.

It’s working fine for me. And it’s weird it’s not working for you.
Now, if for some reason you’re against a wall, of course it won’t work because even if you add a position, you can’t push through the wall or a floor, and the position will be stuck there.
I tried it on a character in the middle of the screen with no floor, no gravity, no walls, and it works as intended (it would be super weird if Godot didn’t do this basic vector math)

However try this:

	print ("------------------------------")
	print ("position before: " + var_to_str(position))
	position = position + Vector2(1.0, 1.0)
	print ("position after: " + var_to_str(position))

Because if I try your code on the _physics_process method, and I start at (0,0) I get this result:

(0, 0)
(1, 1)
(1, 1)
(2, 2)
(2, 2)
(3, 3)
(3, 3)
(4, 4)
(4, 4)
(5, 5)
(5, 5)

which might be confusing and might lead you to think the array doesn’t change, but you’re actually doing two consecutive prints of different physics calls and it’s confusing in your console.
As I said, it works fine for me.

------------------------------
position before: Vector2(0, 0)
position after: Vector2(1, 1)
------------------------------
position before: Vector2(1, 1)
position after: Vector2(2, 2)
------------------------------
position before: Vector2(2, 2)
position after: Vector2(3, 3)
------------------------------
position before: Vector2(3, 3)
position after: Vector2(4, 4)
------------------------------
position before: Vector2(4, 4)
position after: Vector2(5, 5)
------------------------------
position before: Vector2(5, 5)
position after: Vector2(6, 6)
1 Like

By the way, on my ready function it works fine too.
image

Oooh, I see now why it prints the same result twice in the physics update.

In the _ready func I still get the unchanged result with position, but it works fine with a local Vector2 variable. Could it be that the node is an AnimatableBody2D and it has “Sync to Physics” flagged in inspector?

Just tried it… I think it was just that! Thank you a lot for the detail explanation! I still have to get used to how various funcs are called and in which order.

Oh, yes. Definitely. I have heard that “Sync to Physics” gives a lot of headaches. It’s better not to use it if you don’t know exactly what you’re doing.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.