Help with understanding code/ gdscript.

Godot Version

Godot 4.4.1

Question

Hi. This is my first time on here so if I do anything incorrectly please don’t hesitate to let me know, thanks!

So I’m interested in making games, but I’m not familiar with coding. So I have been doing my best to study python on W3schools for the past 2 months, doing their exercises etc. to try and rewire my brain to think like a programmer.
At the same time I am trying to follow along with GDscript tutorials, right now trying to follow a video by “Unain” explaining how to make 2d movements a bit more modular. I really like this idea, because that’s like making a template that works for other things in the future. (This is the exact video I’m following: https://youtu.be/yzbxoZFsU2Y?si=6FkKKbRr1Dsg3cNe)

The difficult bit is that I am very stubborn and will only be able to memorize/get things done if I understand the core fundamentals of how something work. So I find a lot of the youtube tutorials really hard to follow because most of them have to assume you know coding.

And that’s what brought me here: In Unain’s video, when it gets to the movement component, this is what he types (Unain’s example):

class_name MovementComponent
extend node

@export_subgroup("settings")
@export var speed: float = 100

func handle_horizontal_movement(body: CharacterBody2d, direction: float) -> void:
    body.velocity.x = direction * speed

Unain goes on to explaining that direction is will change depending on which way the player is going; if it’s 1.0, then it’s going right, if it’s -1.0 then it’s going left, and if it’s 0 then the character isn’t moving.

I am also aware that people usually do (general example):

func _physics_process(delta):
    var direction = Input.get_vector("go_left", "go_right", "go_up", "go_down")
    velocity = direction * speed
    move_and_slide()

Below are my questions:

  1. in Unain’s example, the direction is passed as an argument in the func and given the float type. But it was never explained why the direction can change from 0 to 1.0 or -1.0 when movement keys are pressed. In the general example, direction is a custom variable with the value:
Input.get_vector("go_left", "go_right"...etc)

so in my mind it makes a bit more sense how then direction * speed would be able to change.

  1. I have seen a lot about variable frame rates and how you MUST times things by delta, to keep things consistent across different PCs. But in Unain’s example, delta was not used. I was wondering why that is? (Now that i check, the general example doesn’t times direction and speed by delta either, but has delta as an argument within the _physics_process.

an extra question would be: sometimes i see delta as

delta

but some times i see it as:

_delta

I was just wondering why that is. I remember reading something about the underscore marking it as a built in godot name? I think i read that in the learn gdscript from Zero app.

Anyways, thank you so much for your time everyone! Really hoping I can get your professional insights from here and start making some games!

1 Like

Hi there, it’s nice to hear about your journey and I hope you can stick with it!

As for your problems:

  1. The handle_horizontal_movement() is just a function that changes velocity, nothing else. By itself it will not do anything, you need to call that function to make something happen. And you need to call it with the direction parameter, meaning at the time of calling the function, you should have already specifed the direction Vector, e.g. as you mentioned with the Input.get_vector(). Changing the velocity won’t do anything by itself either, you need to call the move_and_slide() function to actually move the body. In this simple example, going with the “general approach” seems better and easier to grasp for beginners. But I understand that Unain probably wanted to teach you the modular approach, which might come in handy with more complex games.
  2. The move_and_slide() already has the delta multiplication built in, that’s why you don’t need to multiply it here. If you don’t use that function and instead change the position of the body directly, then you’d need to multiply by delta.
  3. It doesn’t really matter if you say delta or _delta or monkey, you can call it whatever you want. The underscore in the parameter name just suppresses the warning about the parameter not being used in the function’s body, so if you don’t use it you might as well just slap an underscore there to suppress that warning.

I strongly recommend you start with the official Godot documentation tutorials, then branch out. They teach you how to use the editor, how and why you write the code you write, and you can use the knowledge to help you understand other people’s tutorials. This is going to help you understand a lot more than asking questions about some random person’s tutorial that may just be out of date. Because it’s going to give you enough information to ask more informed questions when you get stuck.

I’ve also got some other really great tutorials to recommend that are good for beginners and moving from beginner to intermediate. (The last two links.)

Welcome to the Godot and the Development communities! Good luck! It’s a lifelong journey!

Since your questions have already been answered, I won’t copy that. But I just wanted to shove some encouragement your way.

Wanting to understand everything (you called it being stubborn) will pay you dividends in the long run. Focussing on learning and understanding, instead of getting things working will help you tremendously down your programming path.

At some point you’ll learn what rabbit hole to dive in to and which to skip, but that comes with experience.

Keep digging, keep learning :raising_hands:t3:

1 Like