Even if your game runs fine static typing helps so much when debugging issues and can even prevent you from creating bugs. There's really no reason not to use it even in small projects.
I didn't even know there were performance benefits to type hinting in GDScript... I use it because it's so much easier to write code with intellisense that knows the types of variables and outputs and it highlights the irritating type mismatch bugs before compilation even.
I'm not 100% sure if typing functions gives the same performance increase as for variables. My understanding is that statically typing all the things lets the interpreter take shortcuts when running the code, increasing the performance. Thanks for watching!
One thing I've wondered is if typing void or Variant improves performance. Both are implicit. Such as no return = void. And Explicit and implicit Variant has the same results. So I've always wondered if typing either leads to better performance or just unnecessary and more verbose code (bad).
I did statically type all the functions in my project where I could see an obvious return type - mostly voids, Arrays, bools and ints, and I have already noticed reduced loading times, and was able to slightly increase the frame rate on my most laggy level... There are still some hiccups here and there I need to work on - most likely due to textures... But it does seem to run more smoothly overall. I'll have to look into Variant because one of my most used functions returns different objects or null. Thanks.
@@ShiloBuff it's not necessarily bad even if it's not functionally necessary for the interpreter. Typing a method as void will indicate to you or another programmer that the method shouldn't return anything -- and also if you make the mistake of returning something it will allow the environment to complain at you. To answer your implicit question though, typically void typing won't see any performance gains. Why typing gives you performance gains in the first place is because the more refined your type the more specific the handling can be. If I have an untyped value it could be literally anything at all so the receiving side has to account for "What if it's a list or a string or a hashtable or a ..." and has to have code to handle all of those circumstances to prevent crashing. If instead you type a value as a "Number" now it only has to think about "Is it an int? Is it a float? Is it signed or unsigned? How big is it?.." and the more refined you get your typing, say to "uint8" now the receiver is certain, the value I'm working with is going to be an unsigned 8 bit wide integer, all the code that would manage receiving arrays and strings and floats doesn't need to exist anymore. When the return type is void, not only will you be returning nothing... there will be no receiver to even make an optimization decision about it. -- That said it's still worth it because it adds human understanding and will be optimized out at runtime for free.
Search this and Godot docs says "Also, typed GDScrypt improves performance by using optimized opcodes when operand/argument types are known at compile time.", which means that this should indeed work, given that documentation is accurate. Also it continues in the same paragraph "More GDScripts optimizations are planned in the future, such as JIT/AOT compilation.".
Tbh its both. I like it and I've not considered the performance gains. Because I love the intellisense improvement, I love that I can't accidentally set my number to a string like a dumbass
You can also check the "Add Type Hints" box under editor settings to have autocompletion add the static types for you in some cases. Instantly subscribed after seeing the debug option. Really great video thanks.
I've been hammering my head against this wall trying to learn programming as a hobby for 5-6 years now. I've run across this concept of static vs dynamic typing so often and it always confused me so I just thought "I'll worry about it tomorrow I guess." Somehow this video just really simplified it and something clicked for me. Thank you!
Thank you for making this video, I've heard the same answer from others when it comes to the performance gain of static typing, this help's reinforce their statements. I forgot that you could set the editor to error at non-static types, so thank you for that reminder.
You actually can type arrays in GDScript now. var my_array: Array[int] = [1,2,3,4] Although, it doesn't have the best support and can be a little finicky. Hopefully that will be addressed in the near future, along with nested array and Dictionary types like you see in TS
thanks for the tip. i knew of static typing i just didn't know why i should use it. i have a small game underway but luckily i'm not very deep yet so i can change the code to static typing :)
There's one more important benefit, namely you will get autofill depending of type of variable/packed_scenes/texture/resource/nodes/etc. It really helps when you are using resources and classes and passing them from editor - i think its on by default but If you dont see them You access them through "Editor" -> "Text Editor" -> "Completion" -> "Add Type Hints".
Still don't understand the point of dynamic type. Just to save seconds of typing time and waste hours or days to hunt down bugs relating to mismatch type? That sounds like an unworthy tradeoff. The fact that most dynamic typing languages had to introduce static typing at some point already proved my point.
It's sad to say that most dynamic programming is done because of the inability to teach typing, or learning typing. So our answer was to automate it. In other words, it's lazy way to solve a trivial problem.
@deforrest-studios that’s something you made up. typing is fundamental and easy to learn and the origins of dynamic typing is to support some far edge cases and lazy convenience
Hey that's super cool, I thought these are just type hints like in python. I guess I didn't bother reading the full manual and didn't realize these are proper static type declarations. Thanks!
worth the watch for the knowledge that I can set untyped declarations to throw errors alone, knowing it improves performance is the icing on the cake 😂 great video, thank you for the info.
EDIT: Looks like you struck gold with this topic. If you ever feel like talking about the debugger in more detail (for dummies like me who are 99% reliant on prints like a caveperson) I'll be there to watch it. 👍 Funnily enough, I already started using static types for my exported variables to get the proper inputs in the inspector, but hearing they also help with performance is good to know and I'll really have to get used to using return types again. Thanks! Oh and the := operator is great for primitive types. Yes, I am *that* lazy of a typer. :D
tl;dr Use staticly typed variables bc gdscript can decide what it doesn't need to do, making everything faster. Excellent Untyped Declaration tip too. Great video, thanks. Subbed.
I've always used static typing, and GDScript was no exception when I started learning it. The difference honestly doesn't surprise me. Any time variants are used, you're adding extra processing time as the engine figures out what type of variable it's meant to be before engaging with it. C# variants get around this problem because they're not *actually* variants, they're strongly typed variables that inherit their type from their declaration (eg. var myVar = Object ). It's why you can only use them as locals.
I was static typing variables already just because it felt like it made things more readable, but I was unaware of the performance boost, I'll keep this in mind.
C# and C++ have a massive community so there is there, that means you just need to know what you are looking for they are not originally designed for game programming but had business and scientific backings
3:09 If you press CTRL+R, you will enable "replace". You can simply replace all "=" with ":=", and anything after a paranthesis "()" with "() -> void" to fix all errors.
That's a part of why Godot was so slow in the past, and in certain cases still is. The Core Api relied a lot in Magic Strings, and the Variety Type. The performance would be amazing if other languages were first - class citizen I guess GDscript will eventually get there.
You can avoid explicit types at assignment, and still get static typing, by using the shortcut: var total := 100 Instead of var total : int = 100 A little tip that reduces boiler plate when the types are already easily readable in code. ( This pattern is also in other popular languages: Kotlin, C++ (auto), etc)
The issue is when you use arrays. And i dont mean simple ones like an array of ints. I mean stuff like an array of array when each element is an array where the first element is an int, the second a node2d, the third a string.
As a self taught absolute newb of a coder, could you please explain how when I would/wouldn’t set a function to void? That part has always tripped me up. Declaring the types seems super straightforward though, so thanks for the great video!
void is the function return type when you dont return any value(s) in the function's code. The _process() and _ready() node functions are some examples of this. If you need a value from that function (let's say a string) somewhere else you can set the return type as String. This is only needed to satisfy static typing with the setting.
@@DeepDiveDevelop So… if you don’t return anything at all, use Void. That makes sense… but does that only mean when you literally have “return X” in your code? Or are there other examples when you need to return something? Thanks for the reply!
@@BigBossRazz You can call a function to work with parameters and return the result. Here is a super basic exemple, func add(a:int, b:int) -> int: var result: int = a + b return result what's cool is that GDScript will check if the return type is correct and if you actually return a result.
I'd say a good rule of thumb is to almost always use static types unless the function will work with generic types or you're writing an addon (types are erased (duplicated but not equal) when passed to those it seems).
personally I don't like the untyped declaration as an error since it doesn't work nicely with some things like Array.map/filter/reduce, or dictionaries. you can also just look at the line number, if it's green that means that line is completely typesafe, otherwise it's not
I wouldn't have thought of this - python has a very similar syntax called 'type annotation' but it doesn't seem to do much except for add additional checking. interesting!
Good to know, i thought is was like in python where it does not give you more performance. Also it can prevent you from generating bugs. So its probably worth all the time - even in non-performance-critical parts of the projects.
Thanks for this, but you forgot something useful! Editor Settings -> Text Editor -> Completion -> Add Type Hints. With this setting, selecting functions via auto complete that can be overwritten (like _ready() etc.), they automatically get their type hint.
I prefer not to use := because if I don't understand the type I'll probably create bugs later. Coming from making typescript servers, love static types for reducing coding errors.
Update: I will use := where I'm creating an object with .new() because the type is obvious and repeating it makes the code less readable. E.g. var img := Image.new()
Coming from Python Godot's type hints felt very natural. The colon equals syntax is interesting, but I'm not convinced of its usefulness when assigning to a $ operation or a function call. For assignments to literal objects it's more worthwhile as it's as obvious there to the reader as the engine.
C# isn't hard at all, though one problem I have with Godot is its poor support for it (especially on Linux). Its still interesting that you can get such a boost by just by properly defining your variable types, static typing ftw (I never liked "var" type declarations in any language).
Yeah I think C++ is the more difficult one for sure. When I first got into godot I did C# for the extra performance, though I was scared away by the lack of support (I'm on linux too).
Huh, I thought my code was running faster because coding skills were improving. Might just have been because I swapped fully to static typing as the other languages I use also use it (TypeScript, Swift).
What do I do in something like my case... I've custom built Godot 3.1 with export templates for windows XP, there isnt untyped warning in the settings, plus they're not a radio button, but a boolean, so if I have to take the logic from the newer version yall are using, it'd take some time.. Is it really worth it? I just want to make the best out of my retro machine games...
I believe the performance increases are only available after godot 4.0. I can't speak too much about the prior settings before Godot 4, but still plenty of reasons to static type in 3.1! godotengine.org/article/gdscript-progress-report-typed-instructions/
I enabled all static-type-related features as compiler errors. GDScript is cool, but I HATE Type inferencing. That being said, I picked up C# and am learning Rust to add to the arsenal. I'm pretty sure there's also a setting to autocomplete the static types for variants, static return types for methods, and when droppping nodes from the inspector.
@@alexstone691 In the options shown in this video, you have the option to disable them, treat them as warnings, or treat them as an error. It's the drop down menu to the right of each of the settings.
My guess is it's trying to figure out the data type first everytime they are used Which what makes it slow. So if the data type is already known, they don't need to figure it out anymore.
Nice video! But how are you calculating your speedup at 2:33? 1 - static_time / dynamic_time is not the speedup, idk know what that is lol. I guess the phrase A is x% faster than y is somewhat ambiguous. I guess a reasonable way to interpret that is to look at the amount of time it takes less than the base case (as a percentage of the base case). So then 40% faster than 100 would be 100 - 40%*100 = 60. Defined like that you would calculate it like this: (static_time - dynamic_time )/static_time = 1 - dynamic_time /static_time. So I guess your version is like how much more time does dynamic take than static as a percentage of static? I don't even know at this point lol, my head hurts. Also, generally speed tests in debug mode can behave pretty different than in release mode, so testing is release mode would be better. Idk how much that applies to godot, but its probably better to test for the case of a released game than a debug build. But as I said, nice video :)
Oh shoot! Thanks for letting me know I got the formula wrong. I must've rushed through that part of the code. The new formula I came up with for percent faster is: (dynamic_time / static_time - 1) * 100 I reran the performance tests and here are the new results, which have numbers that make more sense I think: dynamic add nums: 0.66875791549683 static add nums: 0.44424700737 static is 51% faster dynamic mult nums: 0.63372111320496 static mult nums: 0.44116997718811 static is 44% faster dynamic vector2 dist: 1.77509212493896 static vector2 dist: 1.18353796005249 static is 50% faster dynamic vector2 add: 0.85133600234985 static vector2 add: 0.35749793052673 static is 138% faster And yeah you're right about the debug vs release build performance. I just focused on the debug build for this vid, but I found some articles that cover the numbers for that if you are interested.
I came from Java(my first language), it's the only one I know before trying and started to learn Godot and GDScript a week ago. Coming from a C based language, I'm too used in declaring the data types, so I started doing that right away after finding out I can do it 😅 I'm learning Python alongside GDScript right now… I hate Py.
I have quite a lot nodes(objects) per scene on a 2d game, this chance would benefit mainly the process loop(s) most? What is actually under the hood when comparind difference on dynamic / non dynamic vars?
The interpreter has to infer types for dynamic variable. For example: You can't do math on strings. What should GoDot do in this situation? var num1 = 3 var num2 = "abc" var result = num1 + num2 Throw an error? Or just infer that num1 is also a string, because num2 isnt an integer, then simply combine them? Those checks and safeguards cost performance. If the vars are static then the interpreter can skip trying to figure out the types in your data.
C# performance isn't actually that great in Godot (Not the language's fault), each Godot api call is a whole glued marshalling conversion that relies in Magic Strings, barely typed since is designed for Gdscript. The more api calls, the less performance gains You'd need to ditch the whole editor node system to get a huge performance difference, and just leverage Dotnet apis or wait for the Gdextension C# update. For Roguelikes and generative content would be beneficial tho. Anyway, for performance nothing beats recompililing the engine and writing C++ stuff modules right in the library, Rust would be amazing if it were officially supported.
oh, well, I already prefered static typing everything so *this video was in fact clickbait technically for me* (this is just because I chose to follow a tutorial that showed me how to static type before and if I can static type, I will)
I would suggest you should have also gone over the differences from the explicit and inferred type declarations instead of just randomly interspersing their use in the video without explanation. Also making the _process delta typed could have used a bit of a highlight. Using const will statically type a variable too. Nice overview though.
it is not massive Performance boost. it is better use gd script on prototype or demo projects for real thing it is better to use C++ or C. my general reason using gd script is fast compile time. but when projects ready. I convert my code to C++ or C
Even if your game runs fine static typing helps so much when debugging issues and can even prevent you from creating bugs. There's really no reason not to use it even in small projects.
I didn't even know there were performance benefits to type hinting in GDScript... I use it because it's so much easier to write code with intellisense that knows the types of variables and outputs and it highlights the irritating type mismatch bugs before compilation even.
didn't realize functions could be statically typed for a performance boost too... Very useful.
I'm not 100% sure if typing functions gives the same performance increase as for variables. My understanding is that statically typing all the things lets the interpreter take shortcuts when running the code, increasing the performance. Thanks for watching!
One thing I've wondered is if typing void or Variant improves performance. Both are implicit. Such as no return = void. And Explicit and implicit Variant has the same results. So I've always wondered if typing either leads to better performance or just unnecessary and more verbose code (bad).
I did statically type all the functions in my project where I could see an obvious return type - mostly voids, Arrays, bools and ints, and I have already noticed reduced loading times, and was able to slightly increase the frame rate on my most laggy level... There are still some hiccups here and there I need to work on - most likely due to textures... But it does seem to run more smoothly overall. I'll have to look into Variant because one of my most used functions returns different objects or null. Thanks.
@@ShiloBuff it's not necessarily bad even if it's not functionally necessary for the interpreter. Typing a method as void will indicate to you or another programmer that the method shouldn't return anything -- and also if you make the mistake of returning something it will allow the environment to complain at you. To answer your implicit question though, typically void typing won't see any performance gains.
Why typing gives you performance gains in the first place is because the more refined your type the more specific the handling can be. If I have an untyped value it could be literally anything at all so the receiving side has to account for "What if it's a list or a string or a hashtable or a ..." and has to have code to handle all of those circumstances to prevent crashing. If instead you type a value as a "Number" now it only has to think about "Is it an int? Is it a float? Is it signed or unsigned? How big is it?.." and the more refined you get your typing, say to "uint8" now the receiver is certain, the value I'm working with is going to be an unsigned 8 bit wide integer, all the code that would manage receiving arrays and strings and floats doesn't need to exist anymore. When the return type is void, not only will you be returning nothing... there will be no receiver to even make an optimization decision about it. -- That said it's still worth it because it adds human understanding and will be optimized out at runtime for free.
Search this and Godot docs says "Also, typed GDScrypt improves performance by using optimized opcodes when operand/argument types are known at compile time.", which means that this should indeed work, given that documentation is accurate.
Also it continues in the same paragraph "More GDScripts optimizations are planned in the future, such as JIT/AOT compilation.".
Also apparently there are "Type Hints".
You access them through "Editor" -> "Text Editor" -> "Completion" -> "Add Type Hints".
Bro WHAT I thought programmers were just being fancy and organized by doing that, nobody told me it boosted performance by 60% lmao
Thank you!
It's a new thing in Godot 4. In Godot 3 it only helps autocomplete ;-)
Tbh its both. I like it and I've not considered the performance gains. Because I love the intellisense improvement, I love that I can't accidentally set my number to a string like a dumbass
Awesome vid! I'm a C dev so I just prefer this way lol, but I can confirm the speed and bug improvements after doing this
You can also check the "Add Type Hints" box under editor settings to have autocompletion add the static types for you in some cases. Instantly subscribed after seeing the debug option. Really great video thanks.
I've been hammering my head against this wall trying to learn programming as a hobby for 5-6 years now. I've run across this concept of static vs dynamic typing so often and it always confused me so I just thought "I'll worry about it tomorrow I guess." Somehow this video just really simplified it and something clicked for me. Thank you!
Omg my prediction was spot on. Static type EVERYTHING!
Thanks! I didn't realize you could make it give a warning in the project settings. 👍👍
Thank you for making this video, I've heard the same answer from others when it comes to the performance gain of static typing, this help's reinforce their statements. I forgot that you could set the editor to error at non-static types, so thank you for that reminder.
Thank you for showing us the real performance gain in numbers. Other videos on the subject are missing that
This is great. Static typing is always my preference, I had no idea it was also a speed boost!
This is something I had been doing a bit, but since the last couple of months I've been fully static typing variables and functions.
I watched a couple of GDQuest videos, and he does that, but I never heard explanation. Thank you!
Wow that’s crazy ! Now I finally understand why some coders like to declare variable types, I’ll be doing it from now on !
I mean it's not only faster, it's also less bug prone that way
Let's hope GDScript gets array typing soon.
You actually can type arrays in GDScript now.
var my_array: Array[int] = [1,2,3,4]
Although, it doesn't have the best support and can be a little finicky. Hopefully that will be addressed in the near future, along with nested array and Dictionary types like you see in TS
Awesome tips! Thanks! God bless you!🎉
thanks for the tip. i knew of static typing i just didn't know why i should use it. i have a small game underway but luckily i'm not very deep yet so i can change the code to static typing :)
Hey thanks this was actually helpful even if not with performance it was with cleaner code and the video was right to the point.
Holy shit! Thanks man this has drastically improved my performance
There's one more important benefit, namely you will get autofill depending of type of variable/packed_scenes/texture/resource/nodes/etc. It really helps when you are using resources and classes and passing them from editor
- i think its on by default but If you dont see them
You access them through "Editor" -> "Text Editor" -> "Completion" -> "Add Type Hints".
I like clean code, so I already did that but I didn't know it would make a difference, thanks
Still don't understand the point of dynamic type. Just to save seconds of typing time and waste hours or days to hunt down bugs relating to mismatch type? That sounds like an unworthy tradeoff. The fact that most dynamic typing languages had to introduce static typing at some point already proved my point.
It's sad to say that most dynamic programming is done because of the inability to teach typing, or learning typing. So our answer was to automate it.
In other words, it's lazy way to solve a trivial problem.
@deforrest-studios that’s something you made up. typing is fundamental and easy to learn and the origins of dynamic typing is to support some far edge cases and lazy convenience
Hey that's super cool, I thought these are just type hints like in python. I guess I didn't bother reading the full manual and didn't realize these are proper static type declarations. Thanks!
worth the watch for the knowledge that I can set untyped declarations to throw errors alone, knowing it improves performance is the icing on the cake 😂 great video, thank you for the info.
EDIT: Looks like you struck gold with this topic. If you ever feel like talking about the debugger in more detail (for dummies like me who are 99% reliant on prints like a caveperson) I'll be there to watch it. 👍
Funnily enough, I already started using static types for my exported variables to get the proper inputs in the inspector, but hearing they also help with performance is good to know and I'll really have to get used to using return types again.
Thanks!
Oh and the := operator is great for primitive types. Yes, I am *that* lazy of a typer. :D
More info about debugging: ua-cam.com/video/P7AQLUU3xKk/v-deo.html
tl;dr Use staticly typed variables bc gdscript can decide what it doesn't need to do, making everything faster. Excellent Untyped Declaration tip too. Great video, thanks. Subbed.
I've always used static typing, and GDScript was no exception when I started learning it. The difference honestly doesn't surprise me. Any time variants are used, you're adding extra processing time as the engine figures out what type of variable it's meant to be before engaging with it. C# variants get around this problem because they're not *actually* variants, they're strongly typed variables that inherit their type from their declaration (eg. var myVar = Object ). It's why you can only use them as locals.
Thank you for this tip!! I heard about it before, but didn't know it made such a difference :D
Coming from Unity I already did this as standard practice, good to know this actually helps performance lol!
I was static typing variables already just because it felt like it made things more readable, but I was unaware of the performance boost, I'll keep this in mind.
This simple example should be in the godot documentation with an explanation - you can use the Variant type, but with a 30% performance loss.
any hints for statically typing a dictionary? in c# you would Dictionary but I can't figure this out for gdscript
there is no static typing for dictionaries in Godot currently
According to the official website, it's coming with 4.4. I'm so glad !
Thanks for the video 🤙🤙 I will start with this even for a minor performance boost
C# and C++ have a massive community so there is there, that means you just need to know what you are looking for they are not originally designed for game programming but had business and scientific backings
3:09
If you press CTRL+R, you will enable "replace".
You can simply replace all "=" with ":=", and anything after a paranthesis "()" with "() -> void" to fix all errors.
Not a great idea - some "=" are just to assign a new value ;-)
That's a part of why Godot was so slow in the past, and in certain cases still is. The Core Api relied a lot in Magic Strings, and the Variety Type. The performance would be amazing if other languages were first - class citizen
I guess GDscript will eventually get there.
You can avoid explicit types at assignment, and still get static typing, by using the shortcut:
var total := 100
Instead of
var total : int = 100
A little tip that reduces boiler plate when the types are already easily readable in code. ( This pattern is also in other popular languages: Kotlin, C++ (auto), etc)
True!
But that was shown in the video tho ;-)
I wondered what are the implications of using the ':'. Now I know. Thank You!
Great Video!
Thanks man, interesting. Coming from embedded C++, not declaring the variable types is confusing to me. I did not know it actually boost performance!
Very good tip, I never thought about it, oops a lot of work is waiting for me, thank you :)
1:20 the first thing I noticed was that the movement was done in _process and not _physics_process
The issue is when you use arrays. And i dont mean simple ones like an array of ints. I mean stuff like an array of array when each element is an array where the first element is an int, the second a node2d, the third a string.
As a self taught absolute newb of a coder, could you please explain how when I would/wouldn’t set a function to void? That part has always tripped me up. Declaring the types seems super straightforward though, so thanks for the great video!
void is the function return type when you dont return any value(s) in the function's code. The _process() and _ready() node functions are some examples of this. If you need a value from that function (let's say a string) somewhere else you can set the return type as String. This is only needed to satisfy static typing with the setting.
@@DeepDiveDevelop So… if you don’t return anything at all, use Void. That makes sense… but does that only mean when you literally have “return X” in your code? Or are there other examples when you need to return something? Thanks for the reply!
@@BigBossRazz You can call a function to work with parameters and return the result. Here is a super basic exemple,
func add(a:int, b:int) -> int:
var result: int = a + b
return result
what's cool is that GDScript will check if the return type is correct and if you actually return a result.
@@CmdZOD sweet, thanks for the help!
It's like remembering to put % after everything in QBASIC that should be an int.
I'd say a good rule of thumb is to almost always use static types unless the function will work with generic types or you're writing an addon (types are erased (duplicated but not equal) when passed to those it seems).
Someone know if there is a way to set the default script of godot with ready and process func with static declaration for all the projects ?
Looks like this is possible, Check out docs.godotengine.org/en/stable/tutorials/scripting/creating_script_templates.html
personally I don't like the untyped declaration as an error since it doesn't work nicely with some things like Array.map/filter/reduce, or dictionaries. you can also just look at the line number, if it's green that means that line is completely typesafe, otherwise it's not
But dictionaries are still untyped 😥
It will be a glorious day when they add typed dictionaries and generics
Another advantage of static typing is intellisense support.
Thank you so much !
I always use static typing (Haskell refugee), but it never appeared to me how much of a performance increase it is for Godot.
I wouldn't have thought of this - python has a very similar syntax called 'type annotation' but it doesn't seem to do much except for add additional checking. interesting!
Good to know, i thought is was like in python where it does not give you more performance. Also it can prevent you from generating bugs. So its probably worth all the time - even in non-performance-critical parts of the projects.
Thanks for this, but you forgot something useful!
Editor Settings -> Text Editor -> Completion -> Add Type Hints.
With this setting, selecting functions via auto complete that can be overwritten (like _ready() etc.), they automatically get their type hint.
Nice work, Ill try it
I prefer not to use := because if I don't understand the type I'll probably create bugs later. Coming from making typescript servers, love static types for reducing coding errors.
yes, its hard to go back to JS and dynamic types after trying out typescript!
Update: I will use := where I'm creating an object with .new() because the type is obvious and repeating it makes the code less readable.
E.g. var img := Image.new()
Coming from Python Godot's type hints felt very natural. The colon equals syntax is interesting, but I'm not convinced of its usefulness when assigning to a $ operation or a function call. For assignments to literal objects it's more worthwhile as it's as obvious there to the reader as the engine.
I hate dynamic typing. Static typing just solves so many issues and, in my opinion, reads better. I think in types and values, not just values.
C# isn't hard at all, though one problem I have with Godot is its poor support for it (especially on Linux). Its still interesting that you can get such a boost by just by properly defining your variable types, static typing ftw (I never liked "var" type declarations in any language).
How else do you declare variables?
Worst case of declaring types is probably python imo
Yeah I think C++ is the more difficult one for sure. When I first got into godot I did C# for the extra performance, though I was scared away by the lack of support (I'm on linux too).
this helped, thanks
Huh, I thought my code was running faster because coding skills were improving. Might just have been because I swapped fully to static typing as the other languages I use also use it (TypeScript, Swift).
Woa, thanks!
I don’t like that it can’t auto-infer type of `var a = 5` it’s obviously capable when you use `:=`
var a = 5
a would have dynamic type
var a := 5
a would have static type
It's not about that it couldn't. It's a programmer's choice ;-)
I am a C++ dev who uses godot. Since I learned c++ before gdscript, I can't stop declaring datatypes😅
I'm still in college, but I can feel you being a Java programmer 😅
Which is faster
1 - var number: int = 10
2 - var number := 10
I heard 1 is faster than 2, but is it true?
Good thing I mostly learned Godot not knowing how to make variables any other way than Static types
What do I do in something like my case... I've custom built Godot 3.1 with export templates for windows XP, there isnt untyped warning in the settings, plus they're not a radio button, but a boolean, so if I have to take the logic from the newer version yall are using, it'd take some time.. Is it really worth it? I just want to make the best out of my retro machine games...
I believe the performance increases are only available after godot 4.0. I can't speak too much about the prior settings before Godot 4, but still plenty of reasons to static type in 3.1!
godotengine.org/article/gdscript-progress-report-typed-instructions/
@@DeepDiveDevelop okay, thank you, that's really appreciated! 🙏🙏🙏
Liked and subscribed! Can you do one about finite state machine setup in godot4?
That's a good one. I'll add that to my list. Thanks for watching!
I enabled all static-type-related features as compiler errors. GDScript is cool, but I HATE Type inferencing. That being said, I picked up C# and am learning Rust to add to the arsenal. I'm pretty sure there's also a setting to autocomplete the static types for variants, static return types for methods, and when droppping nodes from the inspector.
Editor Settings > Text Editor > Completion > Type Hints
Is there an option to make them be treated as compiler error
@@alexstone691 In the options shown in this video, you have the option to disable them, treat them as warnings, or treat them as an error. It's the drop down menu to the right of each of the settings.
@@rawvoxel thank you!
I always static type everything simply for auto complete purposes.
subbed and liked, thanks
Thanks for the sub!
How would that make things faster? (Not saying it doesn’t I just don't understand how that would effect it)
My guess is it's trying to figure out the data type first everytime they are used
Which what makes it slow.
So if the data type is already known, they don't need to figure it out anymore.
Nice video! But how are you calculating your speedup at 2:33? 1 - static_time / dynamic_time is not the speedup, idk know what that is lol. I guess the phrase A is x% faster than y is somewhat ambiguous. I guess a reasonable way to interpret that is to look at the amount of time it takes less than the base case (as a percentage of the base case). So then 40% faster than 100 would be 100 - 40%*100 = 60. Defined like that you would calculate it like this: (static_time - dynamic_time )/static_time = 1 - dynamic_time /static_time.
So I guess your version is like how much more time does dynamic take than static as a percentage of static? I don't even know at this point lol, my head hurts.
Also, generally speed tests in debug mode can behave pretty different than in release mode, so testing is release mode would be better. Idk how much that applies to godot, but its probably better to test for the case of a released game than a debug build.
But as I said, nice video :)
Oh shoot! Thanks for letting me know I got the formula wrong. I must've rushed through that part of the code.
The new formula I came up with for percent faster is:
(dynamic_time / static_time - 1) * 100
I reran the performance tests and here are the new results, which have numbers that make more sense I think:
dynamic add nums: 0.66875791549683
static add nums: 0.44424700737
static is 51% faster
dynamic mult nums: 0.63372111320496
static mult nums: 0.44116997718811
static is 44% faster
dynamic vector2 dist: 1.77509212493896
static vector2 dist: 1.18353796005249
static is 50% faster
dynamic vector2 add: 0.85133600234985
static vector2 add: 0.35749793052673
static is 138% faster
And yeah you're right about the debug vs release build performance. I just focused on the debug build for this vid, but I found some articles that cover the numbers for that if you are interested.
Thank a lot.. very informative..
Static types make compile time optimization a reality also..
I do miss the days when you had no "variable" types and had to follow strict typing.
does giving static return types to functions also help?
Yep!
song name at end?
The roost from animal crossing
I came from Java(my first language), it's the only one I know before trying and started to learn Godot and GDScript a week ago.
Coming from a C based language, I'm too used in declaring the data types, so I started doing that right away after finding out I can do it 😅
I'm learning Python alongside GDScript right now… I hate Py.
I have quite a lot nodes(objects) per scene on a 2d game, this chance would benefit mainly the process loop(s) most? What is actually under the hood when comparind difference on dynamic / non dynamic vars?
The interpreter has to infer types for dynamic variable. For example: You can't do math on strings. What should GoDot do in this situation?
var num1 = 3
var num2 = "abc"
var result = num1 + num2
Throw an error? Or just infer that num1 is also a string, because num2 isnt an integer, then simply combine them?
Those checks and safeguards cost performance. If the vars are static then the interpreter can skip trying to figure out the types in your data.
The biggest performance gain you can get is changing to C#
C# performance isn't actually that great in Godot (Not the language's fault), each Godot api call is a whole glued marshalling conversion that relies in Magic Strings, barely typed since is designed for Gdscript. The more api calls, the less performance gains
You'd need to ditch the whole editor node system to get a huge performance difference, and just leverage Dotnet apis or wait for the Gdextension C# update. For Roguelikes and generative content would be beneficial tho.
Anyway, for performance nothing beats recompililing the engine and writing C++ stuff modules right in the library, Rust would be amazing if it were officially supported.
oh, well, I already prefered static typing everything so *this video was in fact clickbait technically for me*
(this is just because I chose to follow a tutorial that showed me how to static type before and if I can static type, I will)
Could you share the link or title of the tutorial
I would suggest you should have also gone over the differences from the explicit and inferred type declarations instead of just randomly interspersing their use in the video without explanation.
Also making the _process delta typed could have used a bit of a highlight. Using const will statically type a variable too.
Nice overview though.
Thanks for the feedback!
nice vid bud, audio quality needs a bit of work: if you toss a blanket over your head and mic, it can cut out the hovercraft in the background
Lol, yeah my PC fans are kinda loud. Thanks for the feedback!
it is not massive Performance boost. it is better use gd script on prototype or demo projects for real thing it is better to use C++ or C.
my general reason using gd script is fast compile time. but when projects ready. I convert my code to C++ or C
bro inherits from characterbody2d and uses _process function instead _physics_process where all physics objects should be, but shows amazing tip LMAO
Interesting
I don't use godot, i clicked for the cat
C# will plunder your performances. I went there and thrust me, that cake is a lie.
Garbage in garbage out. The performance gains have already been tested. You need to refactor your code structure
Wait, you Neanderthals seriously program without declaring variable types?
I always used static types since I was already used to it for using C++, honestly I never understood what real uses dynamic typing could have
I'm slightly disappointed cause I was already using these
Good video tho
next time just state what you're doing up-front, then elaborate on it, and i might actually believe you're a genuine person