All Questions

Community

WhistaphX
posted to: Adding gravity

Godot and GDScript syntax

Hello! I'm new in programming so maybe these are dumb questions but I don't know where to search:

1- What does mean or how do you read the "." Ex: "Vector2.ZERO" or "velocity.y"

2- I see functions and methods with blank parentheses "()" and others that have something: "delta", numbers, variables. How do you know when it has something and when not?

3- I don't understand what is a "void" type I read that doesn't return a value but I don't understand how it works and why is used here?

4- Is it there a book which I can read this kind of things?

Thank you for your time and excuse my questions.

  • Nathan Lovato replied

    Hi Juan. Here are the answers to your questions. They are not dumb, it just seems you are lacking programming foundations at the moment.

    If you haven't already, I recommend you to watch our free https://www.gdquest.com/tutorial/godot/gdscript/intro/" target="_blank">introduction to gdscript tutorial. It should help you better understand how all of that works.

    I don't have a specific book to recommend to go further, I would instead recommend to study another programming language in parallel with Godot and GDScript. I wrote another free guide named https://www.gdquest.com/tutorial/getting-started/learn-to/code/" target="_blank">Learn to code to help beginners get started.

    Now for some specific answers.

    1. What does the dot mean between identifiers, for example when writing Vector2.ZERO?

    The dot after an identifier means "member of". On the left side, you can have a class name (like Vector2) or a reference to an instance of a class. For example:

    var player := get_node("Player)
    

    player is a variable that stores a reference to the node Player, an instance of the Player class (or Player.tscn scene).

    In the case of Vector2.ZERO, Vector2 is a class, a blueprint to create 2D vectors, and ZERO is a constant defined on that class inside the Godot engine's source code. Is it a 2D vector with the coordinates (0, 0). In Godot, you can ctrl-click on the identifier ZERO to jump to its documentation.

    velocity is a variable we defined ourselves, and it stores an instance of the Vector2 class. Any 2D vector has two member variables we can access: x, and y. They are decimal numbers, and together, they correspond to the coordinates of our 2D vector. We can read or change these values individually at any time:

    velocity.x = 20.0
    velocity.y = 75.0
    

    The code above would turn our velocity vector into a 2D vector equal to Vector2(20.0, 75.0).

    2. How do you know when functions have arguments or not?

    In short, you can ctrl-click on most functions to jump to their definition (where another programmer wrote them) or to their documentation. You can also press Shift F1 in Godot to bring up the help menu and type the name of a function or class you want to find. Doing so searches the built-in code reference. Here is a tutorial I made about that topic: https://www.youtube.com/watch?v=JT6jXW63w_o" target="_blank">https://www.youtube.com/watch?v=JT6jXW63w_o

    3. What is the void return type for a function?

    Using the "-> void" return type hint for a function tells the GDScript compiler that this function does not return any value. Such a function does not require the "return" keyword. We use it for functions that apply some behavior, like changing a variable of our class directly, when we don't need it to calculate and return a result.

    This type hint will help Godot tell you if you make an error in your code. For example, if you write the following code, Godot will highlight the line with "return" in red and tell you it's incorrect, because you are not supposed to return a value from this function.

    var health := 10
    
    func take_damage(amount: int) -> void:
    	health -= amount
    	if health <= 0:
    		queue_free()
    	return health
    

    I hope it helps. Please let me know if you have more questions.