What is the difference between these two class?

Are these fundamentally different stuff or they are the same type of thing? I am a bit confused. I know how to do classes in Python (which is more similar to second example) but seeing these two makes me confused bit.

(never mind the declaration names variables etc, I am asking about the structure and declaration stuff)

class_name Player

var health: int = 100
var name: String = "Unnamed"

func take_damage(amount: int):
    health -= amount
    if health <= 0:
        print("Player", name, "has died!")


class MyClass:
    func my_method():
        print("Hello, GDScript!")


The second is an “inner class”, you can’t define a top level class like that.

Thanks I never knew about it.

Scripts in Godot always define classes, implicitly. The class keyword is for inner classes, which is basically a bunch of syntax for addressing and scope behaviour. You should usually create classes with scripts unless you have a reason to use an inner class. Other than that they behave identically.

1 Like

Thanks for clarifying it. That makes total sense now.

Do I have to define the variables (message, variable) prior to the constructor ?

class_name My_Class

func _init(_m, _v):
	
       message = _m
       variable = _v

Also do we use “self” like in Python? I see some scripts use it and some do not and it is abit confusing :frowning: Sorry to bother too much.

Yes, class variables need to be declared outside of any function.

self is optional, unless you’ve declared a local variable with the same name.

1 Like

Thank you, super helpful.

One is an inner class and the other is a class

1 Like