Godot Version
4.5
Question
Hey so, I’m following along with Clear Code’s Udemy tutorial series and I’m mostly following it to learn GDScript. It’s been a great series but he doesn’t necessarily spend time explaining everything with script so I’ve been taking notes and researching when I don’t understand something involving syntax. Although, that doesn’t always get me the answers I’m hoping to find. This is a basic question about syntax.
I don’t really understand when to place data inside the method’s argument, or when to place the method after the data, or what. In the above for example,
“var dir = (two different positions from two different nodes).method_called()”
the method called (in this case, normalized) doesn’t even have an arugment inside the parentheses. When would I know to put the “player.position - position” inside the normalized parentheses, and when to call “normalized” afterwards? Why do I know just to call move_and_slide() as part of this physics_process, and not to put information inside the argument?
I’ve just realized lots of the time when I’m trying to write my own script and struggling, its because I do actually know what data points I’m trying to call, but I don’t know how to arrange it in a way GDScript likes.
Thanks in advance!
(vector2 - vector2) creates a new Vector2
after that the normalized() is called on this new Vector2
(Vector2).normalized()
and Vector2 type/class has that method normalized()
parentheses here just tell - after the operation in them, call the method on the result of the operation.
its the same as
var dir = player.position - position
var normalized = dir.normalized()
Additionally normalized() has no arguments because it doesnt need them, it is called on and instance of a class that already has that function. So method is invoked with that instance as its self context.
Because the function belongs to this class (Vector2), as in - it is a member of the class, a member function, i call it a method.
1 Like
I think reading the docs would help. They explain how all the methods work and what arguments they take.
For example, part of the description for move_and_slide() is:
Moves the body based on velocity. If the body collides with another, it will slide along the other body (by default only on floor) rather than stop immediately. If the other body is a CharacterBody2D or RigidBody2D, it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes.
The syntax of calling a method is always the same:
object.method(arguments)
Some methods do not take arguments. Others have default values for all or some arguments. Defaults will be used if you don’t specify them. It’s important to always look at documentation for objects/methods you’re using, so you understand which arguments are required and which will take default values if you omit them.
If a method is called from within the class it belongs to, then the object needs to be specified via special keyword self:
self.move_and_slide()
If you don’t specify the object at all, the compiler will always assume the object is self.
So:
move_and_slide()
is in fact the same as:
self.move_and_slide()
2 Likes