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

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 พ.ย. 2024

ความคิดเห็น • 101

  • @synapse84
    @synapse84 10 หลายเดือนก่อน +114

    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  10 หลายเดือนก่อน +24

      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 หลายเดือนก่อน +4

      Thanks man!

    • @simmingi1
      @simmingi1 5 หลายเดือนก่อน

      @@dev-worm just i1iot specturm mommyless guy try to teach someone lel

    • @justsomeone64
      @justsomeone64 4 หลายเดือนก่อน

      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 3 หลายเดือนก่อน

      This is so ridiculously helpful. That's what what I was trying to figure out

  • @jokerCraz3d
    @jokerCraz3d 9 หลายเดือนก่อน +62

    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 8 หลายเดือนก่อน +14

      as a newbie you are correct. i had to stop watching.

    • @labreynth
      @labreynth 3 หลายเดือนก่อน +2

      Yup you're 100% right about that one

  • @KillerGameDev
    @KillerGameDev 9 หลายเดือนก่อน +16

    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 หลายเดือนก่อน +2

      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 หลายเดือนก่อน +1

      @@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 2 หลายเดือนก่อน +1

      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 หลายเดือนก่อน +6

    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 หลายเดือนก่อน +3

    I didn't know inner classes existed. My world is completely shattered rn

  • @realElzie
    @realElzie 7 หลายเดือนก่อน +4

    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  7 หลายเดือนก่อน

      very good explanation! thanks!

  • @sticraft41
    @sticraft41 4 หลายเดือนก่อน

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

  • @MrBrigadierArchived
    @MrBrigadierArchived 9 หลายเดือนก่อน +2

    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.

  • @pctipsandtricks12
    @pctipsandtricks12 10 หลายเดือนก่อน +2

    cool , Don't Give Up

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

  • @woof-t6v
    @woof-t6v 10 หลายเดือนก่อน +1

    Thank you so much, this is just what's been on my mind today!

    • @dev-worm
      @dev-worm  10 หลายเดือนก่อน +2

      so happy to hear that the timing was right! I hope the video is able to help! thanks!

  • @Zweletron_Dev
    @Zweletron_Dev 7 หลายเดือนก่อน

    You are a life saver.

    • @dev-worm
      @dev-worm  7 หลายเดือนก่อน

      so happy to hear! thanks!

  • @timon1816
    @timon1816 10 หลายเดือนก่อน +3

    man, this is the best Godot channel I've ever seen

    • @dev-worm
      @dev-worm  10 หลายเดือนก่อน +1

      so happy to hear that brother! If you ever need anything then please let me know!

    • @jeremiahaemile2008
      @jeremiahaemile2008 9 หลายเดือนก่อน

      He really is❤

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

  • @micahrobbins8353
    @micahrobbins8353 4 หลายเดือนก่อน

    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  4 หลายเดือนก่อน

      really?? how do i say it lol

    • @micahrobbins8353
      @micahrobbins8353 4 หลายเดือนก่อน

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

  • @jeremiahaemile2008
    @jeremiahaemile2008 10 หลายเดือนก่อน

    Amazing video before even I watched it ❤❤❤

    • @dev-worm
      @dev-worm  10 หลายเดือนก่อน +1

      haha thank you so much Jeremiah!

  • @brandonjacksoon
    @brandonjacksoon 9 หลายเดือนก่อน

    Thanks mate! Liked! Always helpful ;)

    • @dev-worm
      @dev-worm  9 หลายเดือนก่อน

      thank you so much!!

  • @The5thR-YT
    @The5thR-YT 5 หลายเดือนก่อน

    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

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

  • @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?

  • @psyantologist
    @psyantologist 3 หลายเดือนก่อน

    neat! thank you :)

    • @dev-worm
      @dev-worm  3 หลายเดือนก่อน

      thank you!! I hope it helped!

  • @aquadros
    @aquadros 9 หลายเดือนก่อน +3

    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.

  • @pixel1325
    @pixel1325 10 หลายเดือนก่อน

    thanks a lot man! keep it up!

    • @dev-worm
      @dev-worm  10 หลายเดือนก่อน

      thanks! hoping it was helpful!

  • @stiergaming
    @stiergaming 10 หลายเดือนก่อน

    can you make a series about music/sounds like how to put sound effects and music in your game please

  • @ElementalTJ
    @ElementalTJ 9 หลายเดือนก่อน

    Thanks DevWorm!

    • @dev-worm
      @dev-worm  9 หลายเดือนก่อน

      of course anytime! thank you ElementalTJ

  • @sebastianczubacki1599
    @sebastianczubacki1599 2 หลายเดือนก่อน

    i cant understand where i could use classes, i heard they are based but just doesnt make sense to me

  • @micahrobbins8353
    @micahrobbins8353 4 หลายเดือนก่อน

    This video is like a class class

  • @icsp_
    @icsp_ 10 หลายเดือนก่อน

    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

  • @Shel-Dev
    @Shel-Dev 8 หลายเดือนก่อน

    thx bro

    • @dev-worm
      @dev-worm  8 หลายเดือนก่อน

      thank you! glad it helped!

  • @JakubSK
    @JakubSK 10 หลายเดือนก่อน

    Would be best to use resources and not sub-classes for something like this.

  • @ibstudios7300
    @ibstudios7300 10 หลายเดือนก่อน +1

    Cool

  • @quickpodcast5minutecasts
    @quickpodcast5minutecasts 13 วันที่ผ่านมา

    damn i get it cuz i know how to code but you have to slow down a bit, nice content

    • @dev-worm
      @dev-worm  12 วันที่ผ่านมา

      oops. thanks for pointing that out. Ill for sure work on that lol

  • @BlazeZpaze
    @BlazeZpaze 10 หลายเดือนก่อน +2

    Still waiting for 2d combat system

    • @dev-worm
      @dev-worm  10 หลายเดือนก่อน +3

      i've uploaded some 2d combat systems on the channel im working on a platformer one now though!

    • @jeremiahaemile2008
      @jeremiahaemile2008 10 หลายเดือนก่อน

      @@dev-worm nice you will make platform one 😍😳

  • @user-rx3xl7zn1u
    @user-rx3xl7zn1u 10 หลายเดือนก่อน

    Could this be considered the code version of Custom Resources?

  • @DrW1ne
    @DrW1ne 10 หลายเดือนก่อน

    May you make a video about java classes in godot ?

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

  • @PizzaGames-zb3my
    @PizzaGames-zb3my 9 หลายเดือนก่อน +40

    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 หลายเดือนก่อน +29

      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 หลายเดือนก่อน +24

      It’s all personal if you are working by yourself. I use camel case for variables and nobody can stop me 😈

    • @KillerGameDev
      @KillerGameDev 9 หลายเดือนก่อน +16

      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 8 หลายเดือนก่อน +3

      ​@shlokbhakta2893 wait are people implying thats evil in python? i am NEVER abandoning my camelCase bruh that shit is invaluable

    • @shlokbhakta2893
      @shlokbhakta2893 8 หลายเดือนก่อน

      @@tam_69420 camelCase FTW!!

  • @truenincillo805
    @truenincillo805 10 หลายเดือนก่อน

    Please tutorial swim player godot 4

    • @dev-worm
      @dev-worm  10 หลายเดือนก่อน

      what do you mean by this? could you explain a little more?

    • @truenincillo805
      @truenincillo805 10 หลายเดือนก่อน

      @@dev-worm I have created a 2D platform project

    • @truenincillo805
      @truenincillo805 10 หลายเดือนก่อน

      @@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 10 หลายเดือนก่อน

      @@dev-worm Do you know how to create it but to activate it with the jump button?

    • @jeremiahaemile2008
      @jeremiahaemile2008 10 หลายเดือนก่อน

      @@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  5 หลายเดือนก่อน

      sorry you see it that way! what do you think wasnt good about it? so i can improve?

    • @ColorauGuiyino
      @ColorauGuiyino 5 หลายเดือนก่อน

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