Boost Godot Project Performance With This Simple Trick

Sdílet
Vložit
  • čas přidán 22. 08. 2024
  • Join the community discord! / discord
    Play my games on itch: breckheck.itch...
    Here's a quick tip in the Godot universe I think the world should know about. GDScript static typing performance gains. Enjoy!

Komentáře • 111

  • @eddex.
    @eddex. Před měsícem +122

    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.

    • @btCharlie_
      @btCharlie_ Před měsícem +6

      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.

  • @davidbarkhausen7739
    @davidbarkhausen7739 Před měsícem +70

    didn't realize functions could be statically typed for a performance boost too... Very useful.

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +14

      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!

    • @ShiloBuff
      @ShiloBuff Před měsícem

      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).

    • @davidbarkhausen7739
      @davidbarkhausen7739 Před měsícem +8

      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.

    • @JayLooney
      @JayLooney Před 28 dny

      @@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.

  • @mirasmussabekov4897
    @mirasmussabekov4897 Před měsícem +18

    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.".

    • @mirasmussabekov4897
      @mirasmussabekov4897 Před měsícem +2

      Also apparently there are "Type Hints".
      You access them through "Editor" -> "Text Editor" -> "Completion" -> "Add Type Hints".

  • @RetroCroww
    @RetroCroww Před měsícem +11

    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!

  • @evan_game_dev
    @evan_game_dev Před měsícem +20

    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

  • @aidanlaing4272
    @aidanlaing4272 Před měsícem +29

    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.

  • @guardllama8922
    @guardllama8922 Před měsícem +2

    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!

  • @andre-angelo-devlog
    @andre-angelo-devlog Před měsícem +9

    Thanks! I didn't realize you could make it give a warning in the project settings. 👍👍

  • @jkouss
    @jkouss Před měsícem +2

    Thank you for showing us the real performance gain in numbers. Other videos on the subject are missing that

  • @dmsys6516
    @dmsys6516 Před 19 dny

    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".

  • @aqua-bery
    @aqua-bery Před měsícem +1

    Omg my prediction was spot on. Static type EVERYTHING!

  • @copperbadge1
    @copperbadge1 Před měsícem +2

    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

  • @JustVoylin
    @JustVoylin Před měsícem +4

    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.

  • @user-qs6qg6jl9f
    @user-qs6qg6jl9f Před 2 dny

    This simple example should be in the godot documentation with an explanation - you can use the Variant type, but with a 30% performance loss.

  • @KyleLuce
    @KyleLuce Před měsícem

    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)

  • @NuttachaiTipprasert
    @NuttachaiTipprasert Před měsícem +4

    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.

    • @deforrest-studios
      @deforrest-studios Před 14 dny

      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.

  • @KucheKlizma
    @KucheKlizma Před 18 dny

    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!

  • @buzzbuzz20xx
    @buzzbuzz20xx Před měsícem +1

    Wow that’s crazy ! Now I finally understand why some coders like to declare variable types, I’ll be doing it from now on !

  • @soviut303
    @soviut303 Před měsícem +4

    Let's hope GDScript gets array typing soon.

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +8

      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

  • @mr1880
    @mr1880 Před měsícem

    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.

  • @AlyphRat
    @AlyphRat Před měsícem

    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.

  • @zhengistasbolatov8480
    @zhengistasbolatov8480 Před měsícem

    I watched a couple of GDQuest videos, and he does that, but I never heard explanation. Thank you!

  • @Seraph120
    @Seraph120 Před 19 dny

    I wondered what are the implications of using the ':'. Now I know. Thank You!

  • @HamishMonkey
    @HamishMonkey Před měsícem

    Holy shit! Thanks man this has drastically improved my performance

  • @setaindustries
    @setaindustries Před měsícem

    Coming from Unity I already did this as standard practice, good to know this actually helps performance lol!

  • @hylucien2980
    @hylucien2980 Před měsícem

    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 :)

  • @OnyxIdol
    @OnyxIdol Před 28 dny

    Another advantage of static typing is intellisense support.

  • @5ionnach205
    @5ionnach205 Před měsícem

    Thank you for this tip!! I heard about it before, but didn't know it made such a difference :D

  • @Lexiosity
    @Lexiosity Před měsícem

    1:20 the first thing I noticed was that the movement was done in _process and not _physics_process

  • @Aeduo
    @Aeduo Před 9 dny

    It's like remembering to put % after everything in QBASIC that should be an int.

  • @Caellyan
    @Caellyan Před měsícem

    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).

  • @kunai9809
    @kunai9809 Před měsícem

    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.

  • @fishnpotatoes
    @fishnpotatoes Před měsícem

    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

  • @VladimirZuev-Elbacha
    @VladimirZuev-Elbacha Před měsícem

    Great Video!

  • @borismarcone6991
    @borismarcone6991 Před měsícem

    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.

  • @Rai2M
    @Rai2M Před 20 hodinami

    I do miss the days when you had no "variable" types and had to follow strict typing.

  • @dom1310df
    @dom1310df Před měsícem

    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.

  • @DanielAttaway
    @DanielAttaway Před měsícem

    Thanks for the video 🤙🤙 I will start with this even for a minor performance boost

  • @content8420
    @content8420 Před 28 dny

    I am a C++ dev who uses godot. Since I learned c++ before gdscript, I can't stop declaring datatypes😅

    • @Reymax164
      @Reymax164 Před 26 dny

      I'm still in college, but I can feel you being a Java programmer 😅

  • @purplelord8531
    @purplelord8531 Před měsícem

    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!

  • @Reymax164
    @Reymax164 Před 26 dny

    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.

  • @Kadad65
    @Kadad65 Před měsícem

    Very good tip, I never thought about it, oops a lot of work is waiting for me, thank you :)

  • @Jinrty
    @Jinrty Před 28 dny

    Thank you so much !

  • @out1007
    @out1007 Před 23 dny

    I always use static typing (Haskell refugee), but it never appeared to me how much of a performance increase it is for Godot.

  • @decepi
    @decepi Před měsícem

    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).

  • @skell_jams
    @skell_jams Před měsícem +1

    any hints for statically typing a dictionary? in c# you would Dictionary but I can't figure this out for gdscript

    • @fatihvahip2765
      @fatihvahip2765 Před měsícem +5

      there is no static typing for dictionaries in Godot currently

  • @jurajojo2543
    @jurajojo2543 Před měsícem

    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)

    • @teistensean7227
      @teistensean7227 Před měsícem

      Could you share the link or title of the tutorial

  • @Julianiolo
    @Julianiolo Před 20 dny

    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 :)

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před 19 dny +1

      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.

  • @eturnerx
    @eturnerx Před měsícem

    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.

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +1

      yes, its hard to go back to JS and dynamic types after trying out typescript!

  • @BenjaminWheeler0510
    @BenjaminWheeler0510 Před měsícem

    I don’t like that it can’t auto-infer type of `var a = 5` it’s obviously capable when you use `:=`

  • @robertwaremusic
    @robertwaremusic Před měsícem

    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.

    • @robertwaremusic
      @robertwaremusic Před měsícem

      Editor Settings > Text Editor > Completion > Type Hints

    • @alexstone691
      @alexstone691 Před měsícem

      Is there an option to make them be treated as compiler error

    • @robertwaremusic
      @robertwaremusic Před měsícem +1

      @@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.

    • @alexstone691
      @alexstone691 Před měsícem +1

      @@robertwaremusic thank you!

  • @Ronscoe
    @Ronscoe Před měsícem +5

    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

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +3

      Lol, yeah my PC fans are kinda loud. Thanks for the feedback!

  • @SuperTommieNL
    @SuperTommieNL Před měsícem +1

    Liked and subscribed! Can you do one about finite state machine setup in godot4?

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +1

      That's a good one. I'll add that to my list. Thanks for watching!

  • @JamesResend
    @JamesResend Před měsícem

    Nice work, Ill try it

  • @BlackJar72
    @BlackJar72 Před měsícem

    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).

    • @RenderingUser
      @RenderingUser Před měsícem +2

      How else do you declare variables?
      Worst case of declaring types is probably python imo

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem

      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).

  • @evopeer
    @evopeer Před měsícem

    this helped, thanks

  • @Paruthi.618
    @Paruthi.618 Před měsícem

    Thank a lot.. very informative..

  • @BigBossRazz
    @BigBossRazz Před měsícem

    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!

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +2

      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.

    • @BigBossRazz
      @BigBossRazz Před měsícem

      @@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!

  • @nightfox_69
    @nightfox_69 Před měsícem

    Woa, thanks!

  • @Thund3rboltGaming
    @Thund3rboltGaming Před měsícem

    bro inherits from characterbody2d and uses _process function instead _physics_process where all physics objects should be, but shows amazing tip LMAO

  • @Burnrate
    @Burnrate Před měsícem

    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.

  • @morganp7238
    @morganp7238 Před měsícem

    subbed and liked, thanks

  • @tomppeli7727
    @tomppeli7727 Před měsícem

    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?

  • @indigouan
    @indigouan Před měsícem

    does giving static return types to functions also help?

  • @ddxyzz
    @ddxyzz Před měsícem

    Interesting

  • @ariseyhun2085
    @ariseyhun2085 Před měsícem +4

    I don't use godot, i clicked for the cat

  • @protogionlastname6003
    @protogionlastname6003 Před měsícem +1

    But dictionaries are still untyped 😥

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +1

      It will be a glorious day when they add typed dictionaries and generics

  • @oosukeren7948
    @oosukeren7948 Před měsícem

    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...

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem

      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/

    • @oosukeren7948
      @oosukeren7948 Před měsícem

      @@DeepDiveDevelop okay, thank you, that's really appreciated! 🙏🙏🙏

  • @veto_5762
    @veto_5762 Před měsícem

    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

  • @_.-.
    @_.-. Před 8 dny

    Wait, you Neanderthals seriously program without declaring variable types?

  • @billy91011
    @billy91011 Před 29 dny

    How would that make things faster? (Not saying it doesn’t I just don't understand how that would effect it)

    • @Reymax164
      @Reymax164 Před 26 dny

      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.

  • @Sergejack
    @Sergejack Před měsícem

    C# will plunder your performances. I went there and thrust me, that cake is a lie.

    • @froggin-zp4nr
      @froggin-zp4nr Před měsícem

      Garbage in garbage out. The performance gains have already been tested. You need to refactor your code structure

  • @fifolker
    @fifolker Před měsícem

    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 ?

    • @DeepDiveDevelop
      @DeepDiveDevelop  Před měsícem +2

      Looks like this is possible, Check out docs.godotengine.org/en/stable/tutorials/scripting/creating_script_templates.html

  • @Mithaldu
    @Mithaldu Před měsícem

    next time just state what you're doing up-front, then elaborate on it, and i might actually believe you're a genuine person