How to Use CLASSES in Godot 4 (everything to know)

  Рет қаралды 27,835

DevWorm

DevWorm

Күн бұрын

Пікірлер: 101
@synapse84
@synapse84 9 ай бұрын
It's worth noting the first example with class_name player isn't referencing the current player in the scene tree, but rather it's referencing a separate player object that's created strictly in memory with `var Player = player.new()`. This player object is never added to the scene, and wouldn't always work as one would expect. For example, if there's a spike trap in the game that does 25 damage to characterbodies that enter it then "print(Player.health)" would always output 100 since it's a separate object that will never collide (since it's not in the scene), meanwhile "Player" (the one in the scene tree) would have 75 health upon colliding with the trap. Instead, `var Player = player.new()` should be: `@onready var Player: player = $Player`. This ensures that "Player" is of type "player" and correctly references the player in the scene tree. Now when the Player collides with the spike trap, print(Player.health) would correctly print 75. The rest of the video looked great, and I really appreciate these easy to understand videos. I have a friend that's learning Godot and I like being able to share these videos with them.
@dev-worm
@dev-worm 9 ай бұрын
this is a very good point! thanks! I'll be sure to pin this for others wondering to see! I'm wishing your friend the best of luck in his learning journey!
@CarnevalCario
@CarnevalCario 9 ай бұрын
Thanks man!
@simmingi1
@simmingi1 5 ай бұрын
@@dev-worm just i1iot specturm mommyless guy try to teach someone lel
@justsomeone64
@justsomeone64 3 ай бұрын
When you use your variable for the player instead of DevWorms, an error occurs when using DevWorms "take_damage" function. What would I need to change, in order to make the player take damage as they do in the video?
@labreynth
@labreynth 2 ай бұрын
This is so ridiculously helpful. That's what what I was trying to figure out
@jokerCraz3d
@jokerCraz3d 9 ай бұрын
Gotta say, its a pretty confusing choice to tell us that classes would be great for a weapons system, but I'm going to show a fruit randomizer. And to then also present var player = player.new() as connected to your on screen character. It really feels like this would confuse newbies more than help them.
@BillybobsPainting
@BillybobsPainting 7 ай бұрын
as a newbie you are correct. i had to stop watching.
@labreynth
@labreynth 2 ай бұрын
Yup you're 100% right about that one
@KillerGameDev
@KillerGameDev 9 ай бұрын
Its worth mentioning that when you say "player. New()" that it DOES NOT reference the player on the screen, its a reference to the script. When you showed damage taking place, it was NOT the damage to the pkayer on the screen, even though you were moving the character. The player on screen ALSO had the same player script so when you do the Enter/return key, only the new instance took the damage. This is a hard concept to understand for new people. I think you should lead with this concept when introducing the someClassName.new() concept. Especially in this scenario where it LOOKED like it was affecting the player on the screen, where it wasnt. A good way to test if youre affecting the on-screen player node would be to queue_free() it on keypress. This would prove it is NOT affecting the the guy on screen.
@dev-worm
@dev-worm 9 ай бұрын
yes you are right! I shouldve made it more clear! sorry! Ill try and catch this type of error in future tutorials
@KillerGameDev
@KillerGameDev 9 ай бұрын
@@dev-worm you good. You do good stuff. Ive seen your other vids. You offer good stuff. Was just something I wanted to clarify for new people.
@sebaitor
@sebaitor Ай бұрын
Thank you so much for this comment. Even as an experienced programmer I was like "WTF how is he referencing that? " when he showed that part of the video
@tomgrimard8075
@tomgrimard8075 8 ай бұрын
Ah, I spent some hours trying to understand that (without a video) doing test and errors and trying to understand. Really cool video, sadly it's kinda an overview of everything that I already learnt by myself. But I wish we had more complex exemple like the weapon you were talking about. I want to know how to code properly, I know there are more than one way to code but I wish a good exemple was used rather than just showing how we can interact with classes
@Limited_Film_Works_
@Limited_Film_Works_ 9 ай бұрын
I didn't know inner classes existed. My world is completely shattered rn
@realElzie
@realElzie 6 ай бұрын
My professors taught classes using cars as an example. You can create a class that has the make, model, year, trim, etc. then you can use the class to easily make more cars. You’re essentially creating your own data type, which gives you a ton of power in your code.
@dev-worm
@dev-worm 6 ай бұрын
very good explanation! thanks!
@MrBrigadierArchived
@MrBrigadierArchived 9 ай бұрын
I am not sure on the structures and using classes that classical way as shown in the video, but I've found the best case for myself specifically by writing basic class_name class and then inheriting it by another script and the same for nodes. This way I can create different scenes and use something like "if contacted_area is Player" later and run scenarios depending on vars values in that Player class_name contacted instead of "if area is NodeType" and trying to define if this is an actual player or something else.
@sticraft41
@sticraft41 3 ай бұрын
I've been making a simple game, trying to learn a bit more coding formalism, classes etc, and your example about weapons hit right on the mark. I made it work like this: extends Node class_name Weapon # Name of main class var BaseDmg:int = 10 # value of base class func fire(): print(BaseDmg) class Kinetic: # inner class 1 var BaseDmg = 10 # value of inner class class Laser: # inner class 2 var BaseDmg = 6 -- And then on my main script-- extends Node class_name Ship var gun = Weapon.Laser.new() # instantiating the new object of class to use func _ready(): # gun.BaseDmg = 7 # can set a different value like this, but no need now pass func _physics_process(delta): print(gun.BaseDmg) -- And the console outputs '6' -- If the instantiated object was : var gun = Weapon.new() then gun.baseDmg would be 10 Hope it helps :)
@woof-t6v
@woof-t6v 9 ай бұрын
Thank you so much, this is just what's been on my mind today!
@dev-worm
@dev-worm 9 ай бұрын
so happy to hear that the timing was right! I hope the video is able to help! thanks!
@saffetoz2566
@saffetoz2566 9 ай бұрын
Thank you, i dont know how to say it but i love you man you and your tutorials are amazing keep up the great work!
@saffetoz2566
@saffetoz2566 9 ай бұрын
Btw can you make a video about labels like score and how to connect them work on them etc that would be awesome
@dev-worm
@dev-worm 9 ай бұрын
I'm working on a User Interface video now! What exactly do you mean by this? Maybe I can try and include it within that video!
@micahrobbins8353
@micahrobbins8353 3 ай бұрын
I'm fascinated by how you pronounce the letter L. I never clocked it until now but I've definitely heard this accent a lot
@dev-worm
@dev-worm 3 ай бұрын
really?? how do i say it lol
@micahrobbins8353
@micahrobbins8353 3 ай бұрын
@@dev-worm I think it's like farther back in the throat than I'm used to. Reminds me of the French R sort of
@pctipsandtricks12
@pctipsandtricks12 9 ай бұрын
cool , Don't Give Up
@samuraikina5908
@samuraikina5908 7 ай бұрын
Nice explanation mate, i use classes a lot since i found out about it ❤
@dev-worm
@dev-worm 7 ай бұрын
so happy to hear that!! glad they proved to be useful!!
@Zweletron_Dev
@Zweletron_Dev 7 ай бұрын
You are a life saver.
@dev-worm
@dev-worm 7 ай бұрын
so happy to hear! thanks!
@psyantologist
@psyantologist 3 ай бұрын
neat! thank you :)
@dev-worm
@dev-worm 3 ай бұрын
thank you!! I hope it helped!
@timon1816
@timon1816 9 ай бұрын
man, this is the best Godot channel I've ever seen
@dev-worm
@dev-worm 9 ай бұрын
so happy to hear that brother! If you ever need anything then please let me know!
@jeremiahaemile2008
@jeremiahaemile2008 9 ай бұрын
He really is❤
@brandonjacksoon
@brandonjacksoon 9 ай бұрын
Thanks mate! Liked! Always helpful ;)
@dev-worm
@dev-worm 9 ай бұрын
thank you so much!!
@jeremiahaemile2008
@jeremiahaemile2008 9 ай бұрын
Amazing video before even I watched it ❤❤❤
@dev-worm
@dev-worm 9 ай бұрын
haha thank you so much Jeremiah!
@ElementalTJ
@ElementalTJ 9 ай бұрын
Thanks DevWorm!
@dev-worm
@dev-worm 9 ай бұрын
of course anytime! thank you ElementalTJ
@mathislalonde353
@mathislalonde353 9 ай бұрын
I would like to make an inventory that would bring together all the weapons/armor/potion... And I could use it to change my main and secondary weapons, armore and more using the inventory. But I have no idea how to do it. I know you did a tutorial on inventory but looking quickly I don't have the impression that it does what I want. If you have a video to suggest to me (because I can't find any) or you plan to make one, that would be nice ! Besides, thank you for the explanation of the classes now I understand better.
@pixel1325
@pixel1325 9 ай бұрын
thanks a lot man! keep it up!
@dev-worm
@dev-worm 9 ай бұрын
thanks! hoping it was helpful!
@aquadros
@aquadros 9 ай бұрын
I just don't think your use case of weapon variety as inner classes is good. Actually that is a good use case for inheritance. Because your inner classes can't be instantiated outside their outer script.
@Shel-Dev
@Shel-Dev 7 ай бұрын
thx bro
@dev-worm
@dev-worm 7 ай бұрын
thank you! glad it helped!
@micahrobbins8353
@micahrobbins8353 3 ай бұрын
This video is like a class class
@stiergaming
@stiergaming 9 ай бұрын
can you make a series about music/sounds like how to put sound effects and music in your game please
@ibstudios7300
@ibstudios7300 9 ай бұрын
Cool
@sebastianczubacki1599
@sebastianczubacki1599 Ай бұрын
i cant understand where i could use classes, i heard they are based but just doesnt make sense to me
@quickpodcast5minutecasts
@quickpodcast5minutecasts 3 күн бұрын
damn i get it cuz i know how to code but you have to slow down a bit, nice content
@dev-worm
@dev-worm Күн бұрын
oops. thanks for pointing that out. Ill for sure work on that lol
@The5thR-YT
@The5thR-YT 4 ай бұрын
is there a way to edit a function in an instance of a class_name? for instance if i already defined a physics process and put code in it but wanted to add more to it instead of completely rewriting it
@jonas_hehe
@jonas_hehe 9 ай бұрын
big question. when you made the inventory system you made a class for the inventory(which is a gdscript not a resource) i autoloaded it but it prints as null and it is not supposed to be null how do i fix this?
@BlazeZpaze
@BlazeZpaze 9 ай бұрын
Still waiting for 2d combat system
@dev-worm
@dev-worm 9 ай бұрын
i've uploaded some 2d combat systems on the channel im working on a platformer one now though!
@jeremiahaemile2008
@jeremiahaemile2008 9 ай бұрын
@@dev-worm nice you will make platform one 😍😳
@icsp_
@icsp_ 9 ай бұрын
I still can't understand the difference between class_name and class. Instead of making class_name Weapon and then creating subclasses with class, can't I create another script that extends from Weapon and make a class_name rifle? So what is the difference, I can't see it.
@aquadros
@aquadros 9 ай бұрын
Every script in godot is an unnamed class. So when you name it it receives a class_name. (It was already a class, it just got a name) When you create a class inside your script you are creating a different data structure "separated" from the script itself that needs to be instantiated in order to be used.
@icsp_
@icsp_ 9 ай бұрын
@@aquadros so the only difference is that one has global visibility and the other local to the script?
@aquadros
@aquadros 9 ай бұрын
@@icsp_ add to that that the script file itself is always a class, when you give it a name you give it global visibility (it can be found in the node menu).
@icsp_
@icsp_ 9 ай бұрын
@@aquadros thank you, i got it
@JakubSK
@JakubSK 9 ай бұрын
Would be best to use resources and not sub-classes for something like this.
@CallousCoder
@CallousCoder 2 ай бұрын
Your terminology is all over the place 😂 A variable in a class is a property. A function in a class is a method. A class is indeed a blue print so an instance of a class is an object. So that player instance is just a pointer to a player object and it holds its own data. So it’s useful if you have two players for example. Bot with the same (shared) methods but the I own local properties. A node or a scene is literally an instance of a class.
@user-rx3xl7zn1u
@user-rx3xl7zn1u 9 ай бұрын
Could this be considered the code version of Custom Resources?
@DrW1ne
@DrW1ne 9 ай бұрын
May you make a video about java classes in godot ?
@PizzaGames-zb3my
@PizzaGames-zb3my 9 ай бұрын
Please look to the godot naming conventions. Class names should be like Player, FruitChoose. Variables should be like player, fruit_choose. This is first this you should pay attention when you start learning new programming language. That's why your code looks like very unprofessional.
@TheEnderCo
@TheEnderCo 9 ай бұрын
Code look has almost zero relevance when used in solo work. Yes its good to follow conventions so that its more readable, but if its readable is dependant on the people who view it. This comment seems rude especially since this user may have not a lot of experience with code writing conventions or prefers the way they write to said comventions.
@shlokbhakta2893
@shlokbhakta2893 9 ай бұрын
It’s all personal if you are working by yourself. I use camel case for variables and nobody can stop me 😈
@KillerGameDev
@KillerGameDev 9 ай бұрын
It is more important to use the same naming conventions throughout your project instead of following what Godot creators do for namimg conventions. If your classes are capital, continue that way. If your variables are snake case, continue that way. If you geta job or team with godot, follow what they do.
@tam_69420
@tam_69420 7 ай бұрын
​@shlokbhakta2893 wait are people implying thats evil in python? i am NEVER abandoning my camelCase bruh that shit is invaluable
@shlokbhakta2893
@shlokbhakta2893 7 ай бұрын
@@tam_69420 camelCase FTW!!
@truenincillo805
@truenincillo805 9 ай бұрын
Please tutorial swim player godot 4
@dev-worm
@dev-worm 9 ай бұрын
what do you mean by this? could you explain a little more?
@truenincillo805
@truenincillo805 9 ай бұрын
@@dev-worm I have created a 2D platform project
@truenincillo805
@truenincillo805 9 ай бұрын
@@dev-worm I would like to know if you know and want to create a swimming tutorial that is activated in a water tile
@truenincillo805
@truenincillo805 9 ай бұрын
@@dev-worm Do you know how to create it but to activate it with the jump button?
@jeremiahaemile2008
@jeremiahaemile2008 9 ай бұрын
@@truenincillo805 you mean place were player slower and it's jump smaller
@Seisry
@Seisry 5 ай бұрын
all it does is crash my game
@dev-worm
@dev-worm 5 ай бұрын
really? are you getting any errors?
@32Tumbles
@32Tumbles 2 ай бұрын
This video was not helpful at all, this really didn't help me understand how to make use of classes. What is a fruitchoose? What is the point? You could just delete the parts of your code that have to do with class and add nothing new and it would run the same
@simmingi1
@simmingi1 5 ай бұрын
is he spectrum? lol 😅 teach your mother with it
@ColorauGuiyino
@ColorauGuiyino 5 ай бұрын
to be honest, this is not a very good tutorial.
@dev-worm
@dev-worm 4 ай бұрын
sorry you see it that way! what do you think wasnt good about it? so i can improve?
@ColorauGuiyino
@ColorauGuiyino 4 ай бұрын
@@dev-worm To be honest, I don't know how to make videos, but I've noticed that good tutorials are scripted and don't have downtime while the announcer thinks or backtracks. The first part, as mentioned in other comments, has that error that you are not instantiating the referenced object. But I continued watching and even though the video was going on, I didn't feel that the core concept of what they are and what it's really for to declare a class was explained. I didn't finish watching the video; I switched to one made by user Queble that I found acceptable and viewed completely. I understand that his is a newer video and that he may have "feeded off" of your video. I hope it helps you.
How to use Classes in Godot! - Intermediate Tutorial
8:46
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 41 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 6 МЛН
Walking on LEGO Be Like... #shorts #mingweirocks
00:41
mingweirocks
Рет қаралды 7 МЛН
When to Use a Resource Over a Dictionary?
11:06
Coding Quests
Рет қаралды 9 М.
How to Use SIGNALS Correctly in Godot 4 (everything to know)
24:06
How to Add Interaction in Godot 4
16:48
Nagi
Рет қаралды 20 М.
The MUST Follow Roadmap For ALL Solo Developers
14:41
DevWorm
Рет қаралды 16 М.
How Games Make VFX (Demonstrated in Godot 4)
5:46
PlayWithFurcifer
Рет қаралды 358 М.
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 41 МЛН