A little bit confused


1) var velocity := Vector2(500, 0) - this code is type hint right? you teach this code on your Learning App so i'm a little bit confused should i use the one code with data type on it or just this one. 

2) extends - this code always auto generated when i choose or use a Godot nodes?

3)  position - how should i know if the code are built in or keywords because in Godot editors theres no color coding. what i mean is theres no specific color for the built in or keywords unlike in vscode or any other editor or IDE they tell you the code you using are built in or keywords

3) delta - should i really need to use this for movements? Im confused about this delta you teach it in Learning App but can't  understand it.

  • Razoric replied

    1) := is called an 'implicit type hint'. Meaning, you're asking Godot to look at the value and decide on the variables' type that way. so var velocity := Vector2(500, 0) is the same as var velocity: Vector2 = Vector2(500, 0), albeit with less repetition. It's not always possible for godot to guess (dealing with values out of arrays and dictionaries, for example) so sometimes you must use the explicit type name.

    2) "extends" is "be an extension of ..." Any script you create is an extension of Object at the very least, so you can say that any script "is" that object. It has all the same variables and functions as the class you're extending (and any variables and functions of the class it's extending)

    3) This is one of those things you must learn. There is no difference between a variable that godot introduced in the engine, and one that you created, which is why there is no special syntax highlighting for them. You use the official godot documentation at https://docs.godotengine.org/en/3.5/

    4) You decide to move the player in _process without delta. The game on your computer runs at 60 FPS, so you tune the amount of speed based on that. You give your game to your friend to try it out, and he has a beastly computer cause he's a tech enthusiast. The game runs at 150 FPS on his computer - so the everything moves over twice as fast and is basically unplayable. If you ever start playing old DOS games from the 80s and 90s, this was a recurring issue.

    delta is the amount of time in seconds from the previous frame to this frame, which is often something small like 0.016 (for 60 FPS). If you multiply something by delta, it takes it from "x per frame" to "x per second", which allows you to have framerate independent logic. So you can tune speeds and acceleration based on pixels per second, instead of pixels per frame, and it'll run the same on your computer as it would on your tech-enthusiast friend's computer.

    3 loves
  • a
    andrewjhaugen replied

    I'm going to try break these questions down one by one:

    1) The part of this line ":=" is a type hint yes. When you use ":" before your "=" you are telling Godot that I want you to infer the type that this variable should be based on what comes to the left of the assignment operator. So in this case since we are making a new Vector2(500,0) we don't need to type: "var velocity: Vector2 = Vector2(500,0)" That would be the explicit way of declaring the data type and there is nothing wrong with it but it's our practice to infer type whenever possible thus we use "var velocity := Vector2(500,0) here.

    2)The extends is auto generated yes. This is because every Godot Script you write, no matter how basic, at least extends from Object, which is the base class for all non-built in types. You can ultimately choose what you want your script to extends from, but usually you are trying to piggy back off of some already built in functionality. So for example, I like the Sprite Node's functionality but I just need it to do a few custom things for me. So I could have "extends Sprite" at the top of my script, and now I get everything Sprite has for free plus I can code my own functionality in addition to that.

    3) GDScript is pretty light on the keywords, there are not nearly as many as there are in other languages. This is nice for learning because there is a very finite list that you need. One page that I had bookmarked all the time when I was learning was https://docs.godotengine.org/en/3.5/tutorials/scripting/gdscript/gdscript_basics
    It has all the keywords clearly laid out.
    When talking about position specifically though, that is actually a property that belongs to the Node2D class. It can be a little tricky getting a handle on all these properties, because even if you look up the documentation for the node you are working on, you have to be able to go up the chain of inheritance to look at ALL the properties that the node inherited from it's ancestors. Luckily, in the Godot Script Editor, you can press "Ctrl-LeftClick" on any built in property and it will take you straight to that section in the documentation. I highly advise getting used to this skill, as it will vastly simplify getting help on the language.

    4) Delta can be confusing at first, and there is a more detailed explanation a few lessons down the road in this course. In the meantime I will answer "should I really use this for movement?" The answer to that is yes, it does matter. If you want your character to have consistent movement no matter if someone is playing it on their 10 year old laptop versus their freshly built gaming rig than you need to perform this additional calculation.

    Let me know if you have any more questions or need me to follow up on any of this.


    2 loves