Liam Smyth(Liam)

Explicit Typing

Hey there! Just flagging some difficulties I had:

I'm using VSCode as an external editor for doing these steps, coming from c# GDScript feels very loose with typing, I was wondering if there was any opportunity around this area of the tutorial to add some optional extra info about how to cast / import the class_name's so that we get autocomplete from the custom classes. It explains how to do this in the GDScript documentation but for dumbos like me who are still getting used to how GD works with scope and importing it would be nice to have a section here on "how to do all this but with explicit imports and casting"

Also worth noting I hit a stumbling block where I named a script wrong and the .tscn lost its .gd reference, causing editor errors. I had to text-edit the .tscn to update the reference even though I renamed everything via GD editor. Had to figure that out by looking for the broken reference in the project. Might be worth adding to a troubleshooting page or something?

All the best, and thanks for making this series!!

  • Nathan Lovato replied

    Casting and typing would be way too advanced for the target audience of this course, especially at this early stage, but if you have any specific questions I'm happy to answer them.

    GDScript is a gradually typed language so by default your code will be dynamic and you won't have great auto-completion as a result: it's as if everything had the any or variant type by default. I think C# has no catch-all type named like this, so think of it as if you used the generic object type in your C# code.

    To register a new type in your project you can write class_name MyNewTypeName at the top of the script. For example at the top of the scoreboard script you could write:

    class_name Scoreboard

    You can then use that type when defining a variable. for example, in the score form script:

    onready var scoreboard: Scoreboard = $Scoreboard

    That should be enough to get the auto-completion type check you are looking for as long as you're working in the Godot editor.

    Now, in Godot 3, in the case of getting a node, when working in VSCode, the scenes don't automatically open in the editor and Godot doesn't have as much context about your code as you would when working from the Godot editor.

    VSCode has to rely on the language server protocol to get auto-complete information, and GDScript's language server is limited by the GDScript parser in Godot 3.

    It should get better with the new GDScript parser/compiler in Godot 4 but for now, you'll have to use a workaround: casting.

    You rarely need to cast values in GDScript, but casting the value returned by the get_node() allows you to get auto-completion in external editors.

    To cast a value, you write the as keyword followed by the type name. To get auto-completion in VSCode you would write the line of code above like so:

    onready var scoreboard := $Scoreboard as Scoreboard

    Notice how we can rely on type inference in this case (we write := ) because the compiler will look at the cast you are doing and use that as the variable's type.

    Let me know if you have other type-related questions!

    1 love
  • Liam Smyth(Liam) replied

    Ah this is a really great explanation of how it all works in GD, thank you :D 

    Being somewhat of a programming noob I cast everything all the time, even in Python where it really seems to encourage you not to, because it helps out a lot with code discoverability. "Gee, what can I do with this class? Let's try thing. (autocomplete)" and also to keep track of what data I'm manipulating. Might just be me, I'm sure lots of people are way more comfortable with "var foo = thing()" and reading api docs about what thing() spits out!

    Appreciate the reply! I am learning a lot!