All Questions

Community

Light

_physics_process(delta: float) -> void VS _physics_process(delta):

When I had Godot auto-complete the func it only had delta as a parameter

From what I understand, the only difference between the two ways is that the way you do it in the video, forces a specific type, right?

What is the purpose of the return type of void then?

Sidenote, I see you are forcing a type for velocity. How critical is it to have variables strongly typed? I'm coming from Javascript, so the chaos of untyped variables is something I'm comfortable with. Is that a bad thing in Godot? 

  • Razoric replied

    For the functionality of your code (whether it works or not), the requirement does not exist. In the future (Godot 4.0+), we will have strongly typed instructions, so there will be a performance benefit, but there is no benefit for the game engine to have type hints on or using them.

    They are, however, in my opinion, an invaluable programming tool, because they help prevent mistakes. Godot will complain if you try to stick a Node inside of an integer. But with the normal, dynamic language without hints, that is perfectly allowed (they are both of type "Variant" which can hold anything), which can lead to hard to trace bugs if you're not careful.

    They are optional, but we use them all the time because they are great and useful. It also explicitly tells the type of the function and what it's supposed to return (or not return, as in void) at a glance without having to look through the function and check if anything is returned, why and when. Same with parameters. 

    If you want your auto-complete to add them for built-in functions like physics process or process, you can go into Editor -> Editor Settings -> Text Editor -> Completion and tick Add Type Hints.


  • Light replied

    That makes perfect sense! I think the issue is partly because I've been using Javascript for so long, I forgot how it is to use a strongly typed language. 

    I'll make those changes to my editor, and do my best to treat GDScript as a strongly typed language when I can. I will say, I still enjoy weakly typed languages <3

    I don't know who said this, but this is very true! If anyone else stumbles on to this thread, enjoy:

    Would you rather have "silly" errors during development, or insanity-inducing errors in production?