Enable GDScript Type Hints for Better Code | Godot 4 Quick Tip

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
out of the box the Godot script editor doesn't include types so if I start typing input can you see that there great you've got a method input a callback that we know exists and we've got a vent and you can say if event dot is action press you know UI accept and then you can say pass you know do the thing and we're used to that that's what's in the box it looks clean it it's a lot like Ruby python other scripting languages but something I've been using and wanted to make more a parent is available is that you can turn on type hinting if you're not familiar with type hinting it's when your code lets you know what it is that's being passed in and it raises the visibility of what's available and what's being returned and I think it helps write better code coming from 15 years of using Ruby where we don't have static typing to in the last five years doing a lot more typescript at my day job I've come to prefer having the type signatures available so if you go to editor settings and then I just type in type but let's go ahead and walk there ourselves you go down to text editor completion there's this option to add type hints if true static typing hints such as the return value when using Code Auto completion or in creating them are available so if we click if we check that to true and click close now if I type funk and we do that input callback we see right away that this callback takes an input event like I said earlier when I added it manually and it returns void so we know we don't have to return anything special this is really helpful because now if I want to go and know what the heck is an input event I can easily search that so it just raises it increases the visibility of the classes and that are being used and I think has helped me learn more about the data structures and class structure that exists so also you know you can write your own methods that take let's say you know we just have a a damaged callback and we have um strength and we know that that's an in and it returns void and we'll just say you know print debug strength this becomes really helpful knowing that that's an INT because if I come down if I go to another part in the code and call damage it knows that it's an in and that type hinting is really helpful so then if I were to pass in you know Foo has a string when it's expecting an INT we get this error cannot pass value of string as M now if we got rid of the type signature it takes anything and that doesn't error but then if we were to do math like one plus strength you know that type of thing we would we would get an error which you know would be uh we can actually run it and see what happens so I'll just press a key well it actually errored there so invalid operands in in string in operator plus now if we go ahead and add back our type hinting we we catch the error sooner so then we know oh this has to be an integer we've run it let me press input it goes and adds them that's doing it a whole bunch but um because it's on the input callback but to me this has been really helpful because it helps catch bugs earlier it helps me write code better because when I call damage I know that it does that and maybe we could make it return the new health like let's say we had VAR health and we set it to five by default and then we say here Health minus equals strength and then maybe we have it return health right now if we go here and we type damage and we do one we can see um new health is equal to damage and we can even write docs we could say Returns the the new health after applying the damage I found it really helpful to have the type signatures available and to be more explicit about the data that's expected the nice thing is too you can still go ahead and write functions you know the thing and use any and then you can check like if Foo has method you know damage then call damage you know whatever that kind of thing so little little tip for Godot and GD script hope that helps it's really helped me all right thanks see ya bye
Info
Channel: Brett Makes Games
Views: 5,524
Rating: undefined out of 5
Keywords:
Id: 86cQq5HIfew
Channel Id: undefined
Length: 5min 16sec (316 seconds)
Published: Fri Mar 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.