Тёмный

Declutter Your Godot 4 Project: Singletons/Autoloads All Over The Place... 

GDQuest
Подписаться 267 тыс.
Просмотров 29 тыс.
50% 1

▶︎ GODOT 4 COURSES : school.gdquest...
🎓 FREE APP "Learn GDScript From Zero" : www.gdquest.com
🎮 FREE INTERACTIVE GODOT TOUR - The Godot Editor - www.gdquest.co...
🗨 DISCORD INVITE : / discord
------------------------------------------
🖋 SYNOPSIS
This video introduces the new GDScript static variables as a better way to store shared data compared to autoloads/singletons which have their use but can quickly pollute your code's global space.
🖈 GDQUEST DEMO FILES : github.com/gdq...
#gamedev #gamengine #gdscript
-------------------------------------------
CC-BY 4.0 LICENSE
Terms: creativecommon...
Attribute To: "CC-By 4.0 - GDQuest and contributors - www.gdquest.com/"

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 93   
@Gdquest
@Gdquest 7 месяцев назад
class_name Mob static var deleted_count := 0 static var hurt_count := 0 ... If you use a class name, you can avoid using preload(), and you can access Mob.deleted_count or Mob.hurt_count from anywhere in the project.
@kbkmn
@kbkmn 7 месяцев назад
does this mean that preload approach will reset all variables on scene change and class_name approach will persist throughout whole app run?
@icefoxzettai
@icefoxzettai 7 месяцев назад
What if you need to reset the counter at any instance, where would it be the best place to do so?
@Joel2Million
@Joel2Million 7 месяцев назад
thanks, the preload felt clunky for someone with a oop background, this is perfect
@TheNightShirt
@TheNightShirt 7 месяцев назад
I would love a tutorial about Composition.
@Gdquest
@Gdquest 7 месяцев назад
Thanks for the suggestion :) Will keep it in mind.
7 месяцев назад
Firebelley Games has a video that was key to me starting to understand composition. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-rCu8vQrdDDI.html Would recommend
@whatsupbro3
@whatsupbro3 7 месяцев назад
@@GdquestPlease! this is probably one of this biggest things i struggle with after switching from unity to godot
@vibingLieke
@vibingLieke 7 месяцев назад
Me too!
@Cigam_HFden
@Cigam_HFden 7 месяцев назад
The best one I had seen so far for composition was this one: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-74y6zWZfQKk.html How You Can Easily Make Your Code Simpler in Godot 4 by Bitlytic
@KyleLuce
@KyleLuce 7 месяцев назад
This is fine for prototypes, but i prefer a decoupled approach here. E.g - create an Autoload global Game event bus - enemies emit a signal when dead ln a Game event bus - create a Singleton that only manages the statistics on death (targeted per use case) - stats Singleton listens for appropriate Game events - Singleton manages statistics and only it is allowed to perform operations/math on these values This provides a decoupled design without any need to keep script resource references artificially held etc and avoids leaky abstractions with many objects directly altering low level details like individual variables.
@bahamajo
@bahamajo 7 месяцев назад
Not really sure I understand this. Doesn't this just mean creating two Autoload Singletons? The event bus and the "Singleton Death Handler"?
@KyleLuce
@KyleLuce 7 месяцев назад
@@bahamajo the global event bus should be an autoload (an established pattern) , the statistics manager can be wherever's appropriate. This stats management depends on your games needs. The idea here isn't "Singleton/autoloads need to be avoided" it's that "error prone design patterns and leaky abstractions need to be avoided". If you were directly changing autoload members from another class, this would also be an anti pattern. Improper uses like these led to autoloads being loathed. When you avoid accessing member variables directly and only mutating state through async events (or at least methods), your system is easier to maintain and refactor later. This is typically summed up as the "Law of Demeter". Static variable usage may make sense in some use cases, but in general if you have other "non owner" classes manipulating these, it can be more error prone and harder to keep track of what's going on (and refactoring challenges mentioned above). Please feel free to ask anything else or if this perhaps was unclear.
@bahamajo
@bahamajo 7 месяцев назад
Thanks for the feedback @@KyleLuce!
@Timikator
@Timikator 7 месяцев назад
Note that this applies everywhere in programming, not only in game programming. It's strange that, in every other programming field (e.g. Web dev), global variables are banned and mostly avoided, meanwhile here in the game dev industry people make tons of tutorials on how to use them without explaining the downsides.
@petethorne5094
@petethorne5094 4 месяца назад
@@KyleLuce Event bus is a really powerful pattern. I use it all the time, it's a pillar of maintainable code in Godot, along with StateMachines and Composition over Inheritance.
@testoftetris
@testoftetris 7 месяцев назад
if I'm not mistaken, you can also use the class_name property on your script with static variables to make them accessible through ClassName.static_var (so you don't have to load the script through its filepath)
@Ichthyoid
@Ichthyoid 7 месяцев назад
would the syntax for that be like this? var VariableName: ClassName.static_var = variable
@danielw7685
@danielw7685 7 месяцев назад
​@@Ichthyoid it would be: var VariableName: Type = ClassName.static_var
@dairyfreelemonstreams9493
@dairyfreelemonstreams9493 7 месяцев назад
Daniel's example is correct as for testoftestris: tested earlier today to verify and yep you can just use ClassName.StaticVar from any node in the scene, even if none of the nodes present in scene are said class. The remote view won't show static variables in playtest as far as I can tell so one of those debugging plugins or just classic print() will be how you peak at it in playtest. (Please feel free to prove me wrong, i can easily have missed it but I spent a solid few minutes looking and not seeing any mention of it in the remote views or in debugger)
@Gdquest
@Gdquest 7 месяцев назад
class_name Mob static var deleted_count := 0 static var hurt_count := 0 ... If you use a class name, you can avoid using preload(), and you can access Mob.deleted_count or Mob.hurt_count from anywhere in the project.
@zehijean8817
@zehijean8817 7 месяцев назад
I would like to and even pay for it...i d like to see a course on non game software development with godot as the ui framework to build a business app with dynamic layouts for mobile tablet and desktop resolutions...i am heavily pushing godot at my company as ui framework ...this way i can do both software dev and gamedev in one place
@Gdquest
@Gdquest 7 месяцев назад
I hear you. We've actually used it to make multiple apps and we love it too. The whole workflow is pleasant from fast prototyping with a visual editor, to UI design, and the OS abstraction is excellent. Definitely planning a course. I would really appreciate it if at some point you could send me an email at nathan@gdquest.com describing your needs. It might help focus the curriculum when we get to it (GDQuest is a very small team...). Thanks for the comment and the trust. :)
@zehijean8817
@zehijean8817 7 месяцев назад
@Gdquest alright ill send an email today thanks for replying fast.
@maze._
@maze._ 7 месяцев назад
If all instances of the script are gone/unloaded from memory, is it correct that the static variable will reset?
@Shadow-cs7oy
@Shadow-cs7oy 7 месяцев назад
Probably, if there are no other instances which could held the values, then static variables are gone
@Dharengo
@Dharengo 7 месяцев назад
Every tool has its use case. It's nice that static variables are here. That will put more value on autoloads for actual autoload things.
@Gdquest
@Gdquest 7 месяцев назад
Agreed
@PhoeniXCh1
@PhoeniXCh1 3 дня назад
Why would the counter data be lost if you delete the last mob? Is that what would happen if you didn't preload the "static_variables_mob" script? I'm sorry. I'm new and can't quite wrap my head around this
@AverageNeovimEnjoyer
@AverageNeovimEnjoyer Месяц назад
You literally saved my life with this. However, there is a small problem I am facing. I have an @onready var texture_rect: TextureRect in my script. I need it to me static so that texture_rect.texture property persists across scenes. But the thing is I cannot use static keyword for @onready variables. What can I do?
@r.e.4873
@r.e.4873 3 месяца назад
@gdquest it would have been nice to explore this with some more examples. A variety of cases to observe would give a deeper understanding. At the moment, I don't fully understand how this works or can be implemented generally.
@danielalvesldiniz
@danielalvesldiniz 7 месяцев назад
well, it is kinda cumbersome to have to keep a permanent reference to the script so that the data doesn't go away... but interesting idea!
@nadadenadak
@nadadenadak 4 месяца назад
why using autoload here is a problem? In my head I'd just use a simple dictionary in the autoload which would hold all the mob related statistics
@EricAbroad
@EricAbroad 7 месяцев назад
PERFECT timing! I just recently got ibtroduced to Autoload scripts, but quickly found that they overtook most of my code and made things easy to break/hard to update later. I will play around with static variables in my next coding session. Thank you!
@Lansamatv
@Lansamatv 7 месяцев назад
comp you make the animations of the bats and the slimer you can make a video about the creation of the sprites What tools do you use?
@MegaJohnny74
@MegaJohnny74 10 дней назад
this still looks like a singleton
@phileon2323
@phileon2323 7 месяцев назад
Use the mono version and declare your variable with public static
@Gatto_uwu
@Gatto_uwu 7 месяцев назад
I recently finished studying Python and wanted to learn how to program video games, but I'm not sure which engine to focus on. Can Godot work? If so, where can I find a tutorial that explains how to use this engine?
@Gdquest
@Gdquest 7 месяцев назад
On this channel. Watch our latest Short for a roadmap. ru-vid.comv7d8uYw59jk If you know Python, Godot will feel natural to you because GDScript is basically Python for Gamedev.
@llaneelyort5599
@llaneelyort5599 7 месяцев назад
Can you save this resource upon exiting game scene and load in back upon game startup?
@Gdquest
@Gdquest 7 месяцев назад
Yes, you will have to write the saving system though of course.
@Roddev776
@Roddev776 7 месяцев назад
How use open gl es 2 in godot 4
@atti1120
@atti1120 4 месяца назад
Is it persistent?
@crashito1752
@crashito1752 7 месяцев назад
I would like a tutorial to make a grid-based movement in Godot 4.2 please!, I am stucked with the programing of it (Sorry for the bad english)
@ভোমরা
@ভোমরা 7 месяцев назад
nice godot
@Floffffff
@Floffffff 7 месяцев назад
Hi, I would like a tutorial about a situation like this in godot 4: what if I have 2 virtual mobile joysticks and I want to be able to move both of them at the same time? (I mean something like brawl stars' controllers) A tutorial would be really appreciated
@dairyfreelemonstreams9493
@dairyfreelemonstreams9493 7 месяцев назад
Today I realized for all the utlities I make for myself like a utility to queue_free all children of a node(or all signal connections!), I could just put all of these in a static function in my utilities script... Bless godot finally having static.
@containedhurricane
@containedhurricane 7 месяцев назад
We could implement Unity's Scriptable Object event architecture and composition to make our code more modular in Godot, as shown in Unity Open Project 1
@Shanjaq
@Shanjaq 6 месяцев назад
Always wanted Statics in Godot, so glad I committed the hours to trimming/substituting dependencies for the upgrade from gd3 to gd4!
@MajikayoGames
@MajikayoGames 7 месяцев назад
wow i didn't know about this! the other day i was using some static utility functions and i thought, 'sigh, i wish i could use a static variable here in addition to my functions.' thanks gdquest!
@thenamesandounts9807
@thenamesandounts9807 7 месяцев назад
Oh. So static varibles have been ported over from C#? (I'm assuming that the concept is older than that but that's where I first encountered them.)
@littleerichsenstudios2292
@littleerichsenstudios2292 2 месяца назад
So valuable - thank you! And I love the bats!
@ritoon2223
@ritoon2223 7 месяцев назад
Hi, can you please tell me if you can port your app to android?
@Gdquest
@Gdquest 7 месяцев назад
Are you referring to your own app or our app to Learn GDScript?
@ritoon2223
@ritoon2223 7 месяцев назад
Your application for learning gdscript
@Gdquest
@Gdquest 7 месяцев назад
We have no plans to port it to android in the immediate future for reasons related to reposiveness and the editor.
@gablanwheel3895
@gablanwheel3895 7 месяцев назад
Good shout, I like the use case here too. We can reliably keep track of enemy deaths without having to store a reference to another script in the mob class. Nice and clean!
@ZoidbergForPresident
@ZoidbergForPresident 7 месяцев назад
On est d'accord que si one n'a pas de node avvec le script contenant les variables static, supprimer tous les objets fera perdre les données statiques?
@sirdogegd9567
@sirdogegd9567 7 месяцев назад
This is so poggers
@mitchellobrien1431
@mitchellobrien1431 7 месяцев назад
Unrelated but I made my first game in Godot and it’s on Steam! It’s called A Christmassy Christmas! 🎄🎄
@qingxian3870
@qingxian3870 7 месяцев назад
nice!
@mitchellobrien1431
@mitchellobrien1431 7 месяцев назад
Thank you!@@qingxian3870
@mitchellobrien1431
@mitchellobrien1431 6 месяцев назад
@@psilon6999 yes I did! I found it to be a great course, and I fully recommend it. Everything he taught has stuck with me and I feel much more confident in engine.
@scarfanzi
@scarfanzi 7 месяцев назад
Helpful! I'm a beginner Godot dev and generally new to game dev altogether. Learning about singletons was a huge step for me, and now learning about Static variables will make my code much cleaner.
@Gdquest
@Gdquest 7 месяцев назад
Welcome :)
@Frank_G_Finster
@Frank_G_Finster 7 месяцев назад
Very interesting video. Thank you for the upload! What about having different classes accessing identically named static variables? Will the data be shared between those classes variables or is this exclusive to instances of a specific class?
@Gdquest
@Gdquest 7 месяцев назад
They will be different even if identically named. Each has its own scope limited to its class. But you can still access static variables of some class from anywhere by preceding the variable by its class name.
@palmpixiplus
@palmpixiplus 7 месяцев назад
This is exactly what I needed!
@Weahl
@Weahl 7 месяцев назад
I want moreeeee, your videos are a drug ❤
@giovanemachado8339
@giovanemachado8339 7 месяцев назад
This is dope, I love this little tricks
@wrksless971
@wrksless971 7 месяцев назад
Will you ever do a packedscene save example? I have been told to search and I have , but some just say save the scene, others say don't, others are saying save the children of children to a owner.... I just don't know who or what to do at this point.
@Gdquest
@Gdquest 7 месяцев назад
Generally you can do whichever seems simplest to you and see if you have a problem. But you do always need to set an owner on nodes you want to save. We'll consider a video on the subject!
@wrksless971
@wrksless971 6 месяцев назад
​@@GdquestHI, Sorry I took so long to reply. Thank you for considering a video on the subject, I look forward to it. Again sorry for the way long timeframe.
@RezaAkbar
@RezaAkbar 7 месяцев назад
is there any example file for this videos?
@Gdquest
@Gdquest 7 месяцев назад
Sure. I will link it in description.
@eliederventura
@eliederventura 7 месяцев назад
What about changing scenes? Will those variables keep their values and be available?
@Gdquest
@Gdquest 7 месяцев назад
Yes
@ukrsolid
@ukrsolid 7 месяцев назад
nice trick
@FunBanan117
@FunBanan117 7 месяцев назад
I only wish for static functions...
@Gdquest
@Gdquest 7 месяцев назад
You can use static functions
@FunBanan117
@FunBanan117 7 месяцев назад
@@Gdquest oh, sorry for misunderstanding. I thought about some kind of function that would activate per each class instance when called from somewhere. For now the only option is to use singleton for that so it require a lot more extra code.
@mariegrasmeier9499
@mariegrasmeier9499 7 месяцев назад
Is this only valid for godot 4 or does it work in 3.5, too?
@joelgomes1994
@joelgomes1994 7 месяцев назад
Godot 4.1 and later.
@domi_dreams
@domi_dreams 7 месяцев назад
stop wasting people's time with that toy language, and focus on improving industry standards like C#. 6-year time Godot user speaking here...
@Cigam_HFden
@Cigam_HFden 7 месяцев назад
Shaming people for using something that you personally do not like is not a way to get them on your side. I prefer gdscript myself, but have used many languages before, even C# for years. It is just a matter of preference, but for me it is for the efficiency and speed at which I can write gdscript. But each language has its pros and cons. Cheers.
@Gdquest
@Gdquest 7 месяцев назад
Advocating that people consider DSLs toys would not improve industry standards. As a gamedev specific language, GDScript works well for most use cases and when it's needed, we use C++ or C#. But feel free to use C# across the board. Industry standard should evolve to recommend what is most applicable to your use case. Languages, like engines, are tools: means to an end. If your preference is to pick up and stick to only one tool, by all means.
@domi_dreams
@domi_dreams 7 месяцев назад
@@Gdquest you position yourself as one of the (if not THE ) main advocates of the engine, you have quite a big following and basically you seem to make a living mostly from the work around the engine. On the same hand you completely neglect such important topics as real-world usage of the tools and what industry actually needs. Do you really believe strong players, like companies or studios will switch their workflow to a semi-scripting language developed in recent yeards, with no serious tooling around it (debuggers, profilers, analyzers, refactoring) for all their codebase? From what we might observe is - you are targeting mostly solo-devs or ocassional users who just tinker with the engine from time to time, building toy games over the weekend. Have you ever considered focusing more on de-facto standards like .NET or maybe even C++ - and filling that missing gap for some serious usages of the engine? No offense, but right now, sugar-like graphics, bouncy vibrant music and python-simple-like scripting would mostly attract people entering game development - but if that's your ACTUAL target audience, not the A-AAA studios, and I am all wrong - I am taking my words back then...
Далее
Лучше одной, чем с такими
00:54
Просмотров 532 тыс.
#kikakim
00:10
Просмотров 11 млн
This Problem Changes Your Perspective On Game Dev
25:51
Best FREE Software for Game Development in (2024)
8:01
Godot Scripts I add to Every Game
12:34
Просмотров 20 тыс.
Do THIS Before You Publish Your Godot Game
3:33
Просмотров 174 тыс.
Draw fewer tiles - by using a Dual-Grid system!
6:22
Просмотров 440 тыс.
Upgrading 75,000+ lines to Godot's new version
19:46
Просмотров 22 тыс.
5 INVALUABLE Godot 4 Code Patterns (feat. @Gdquest )
11:39
Лучше одной, чем с такими
00:54
Просмотров 532 тыс.