Statically typed variables = SPEED! (Godot)
Автор: Hyper Game Dev!
Загружено: 15 авг. 2024 г.
Просмотров: 62 007 просмотров
Someone in the Hyper Game Dev community, @precipire1025, posted a great article that convinced me to avoid leaving my variables untyped. Here's the article:
https://www.beep.blog/2024-02-14-gdsc...
Previously, I had enjoyed the convenience Godot affords devs by allowing us to type variables without clarifying what type of variable it is. That is to say, if it's a boolean (true/false), Godot doesn't require us to clarify that this is a "bool". There are lots of different types that variables can be assigned to, and it is an extra step that sometimes is nice to skip. Other common variable types are integer (if the variable is a whole number), or float (if it's a decimal), or any of the many Node types that Godot offers.
But this practice of avoiding the type clarification comes at a performance cost, especially when we consider just how many variables a single project can have.
Shaving off even those fractions of a second can really add up, so now I make a point to statically type my variables wherever I can.
The simple explanation for why this helps, is that Godot has to actually figure out what the variable type is if we don't explicitly tell it. This is called Static Typing, or Declaring the variable.
Thanks to some tests run by @Riazey, I know now that a colon and an equal sign, or ":=" is often as fast as static typing! (Though there are some edge cases where it's a tiny bit slower). At that point, it may come down to personal preference if you'd rather use := or explicitly statically type the variable.
To statically type your variables, add a colon, and the variable type, before your equal sign (eg, ": bool ="), and you'll give Godot a break from having to sort it out itself. := despite only rarely being slower works too, but I still prefer typing it all out.
I hope this information was helpful or useful to you!
By the way, I do livestream pretty often here on YouTube, so I hope you'll consider subscribing to see me continue learning Godot in public.
/ @hypergamedev
Also also, you can join this channel too, and get access to some pretty dope perks (like an exclusive livestream, and me singing you songs when you join chat)
/ @hypergamedev
Oh, and shout-outs are due to @DungeonMartian and @Dr4g0nBlade for the feedback while I recorded this!
Chapters & Tags:
#gdscript #godot #coding
00:00 - Static Variables Make Your Game Faster
00:06 - Untyped Variable VS Statically-Typed Variable
00:18 - Inferred Variable and Untyped Variables are Slow
00:25 - Make Godot Force Statically-Typed Variables

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: