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.
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!
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?
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.
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.
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
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
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.
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 :)
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.
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
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.
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?
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.
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.
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_ 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).
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.
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.
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.
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.
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
@@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.
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.
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!
Thanks man!
@@dev-worm just i1iot specturm mommyless guy try to teach someone lel
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?
This is so ridiculously helpful. That's what what I was trying to figure out
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.
as a newbie you are correct. i had to stop watching.
Yup you're 100% right about that one
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.
yes you are right! I shouldve made it more clear! sorry! Ill try and catch this type of error in future tutorials
@@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.
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
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
I didn't know inner classes existed. My world is completely shattered rn
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.
very good explanation! thanks!
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 :)
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.
cool , Don't Give Up
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!
Btw can you make a video about labels like score and how to connect them work on them etc that would be awesome
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!
Thank you so much, this is just what's been on my mind today!
so happy to hear that the timing was right! I hope the video is able to help! thanks!
You are a life saver.
so happy to hear! thanks!
man, this is the best Godot channel I've ever seen
so happy to hear that brother! If you ever need anything then please let me know!
He really is❤
Nice explanation mate, i use classes a lot since i found out about it ❤
so happy to hear that!! glad they proved to be useful!!
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
really?? how do i say it lol
@@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
Amazing video before even I watched it ❤❤❤
haha thank you so much Jeremiah!
Thanks mate! Liked! Always helpful ;)
thank you so much!!
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
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.
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?
neat! thank you :)
thank you!! I hope it helped!
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.
thanks a lot man! keep it up!
thanks! hoping it was helpful!
can you make a series about music/sounds like how to put sound effects and music in your game please
Thanks DevWorm!
of course anytime! thank you ElementalTJ
i cant understand where i could use classes, i heard they are based but just doesnt make sense to me
This video is like a class class
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.
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.
@@aquadros so the only difference is that one has global visibility and the other local to the script?
@@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).
@@aquadros thank you, i got it
thx bro
thank you! glad it helped!
Would be best to use resources and not sub-classes for something like this.
Cool
damn i get it cuz i know how to code but you have to slow down a bit, nice content
oops. thanks for pointing that out. Ill for sure work on that lol
Still waiting for 2d combat system
i've uploaded some 2d combat systems on the channel im working on a platformer one now though!
@@dev-worm nice you will make platform one 😍😳
Could this be considered the code version of Custom Resources?
May you make a video about java classes in godot ?
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.
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.
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.
It’s all personal if you are working by yourself. I use camel case for variables and nobody can stop me 😈
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.
@shlokbhakta2893 wait are people implying thats evil in python? i am NEVER abandoning my camelCase bruh that shit is invaluable
@@tam_69420 camelCase FTW!!
Please tutorial swim player godot 4
what do you mean by this? could you explain a little more?
@@dev-worm I have created a 2D platform project
@@dev-worm I would like to know if you know and want to create a swimming tutorial that is activated in a water tile
@@dev-worm Do you know how to create it but to activate it with the jump button?
@@truenincillo805 you mean place were player slower and it's jump smaller
all it does is crash my game
really? are you getting any errors?
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
is he spectrum? lol 😅 teach your mother with it
to be honest, this is not a very good tutorial.
sorry you see it that way! what do you think wasnt good about it? so i can improve?
@@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.