What is a class used for?

i tried but couldn’t really find a clear answer. could somebody help?

1 Like

to make inner class

2 Likes

what are both?

1 Like

what do you mean?

1 Like

both classes and inner classes, could you explain what they are without any unnecessary terms i need to look up?

1 Like

the explanation for the “class” syntax you meant is in the link, it basically create an inner class, because a script is basically an object class. but it you want to make a class inside a script, which is called inner class, you use the class syntax

1 Like

whats inner class used for

1 Like

A class is a kind of template for what is called an “instance”, or an “object”. It groups together information or attributes about a thing (these are also called “properties” or “fields”), and behavior that is closely related to that thing (these are called “methods”).

Imagine that you had dogs in your game. Each dog might be a little different, with different colors, or different favorite foods, but because all of them are very similar things you might write a class Dog in your game’s code, and your Dog class might have fields like var fur_color: Color or var favorite_food_name: StringName. Then you could create new instances of this Dog class, each with its own fur_color and favorite_food_name that makes it different from the others.

It might have methods like func bark(at_object) or func chase(chase_object), too, that affect how the dogs behave in the game, instructing each Dog object to perform some kind of action.

The difference between a class and an “inner class” is very small: It’s just about how you wrote it down in the code. They do essentially the exact same thing (with some minor differences about how attaching scripts to nodes works in the editor) and the important difference is just whether you wrote it down using class_name in the top of a file, or with a class inside the file, and whether you refer to it in other files like Dog or FileClass.Dog (where FileClass would be the name you wrote with class_name in the file containing that inner class).

Disclaimer: I’m pretty new to Godot myself. Classes and instances and fields and methods are pretty universal to programming, not just for Godot, but I could be mistaken myself on some of the Godot-specific info.

5 Likes

so its just dictionaries in a way?

2 Likes

so its just dictionaries in a way?

Sort of. You could think of classes as a schema for dictionaries. Where the instances of the class are like dictionaries, and the class itself is like a template describing what keys all those dictionaries are expected to have.

There’s a little more to it than that, especially if you get into things like class inheritance, but yep there are a lot of similarities there.

1 Like

They are templates. If you wanted to make a consumable item, then you would derive functions and variables from it.

This video explains how they’re useful

No, look up OOP on Google to understand what a class is. A dictionary my seem like it works like a class syntactically, but it isn’t. There is a reason they have different names.

An analogy here is an integer is the similar to a float, since they are both numbers, and made of binary 1s and 0s… but they are fundamentals different in vast ways.

Every script you make inherits a class, and is a class in itself in Godot. An inner class is technically another script inside the script, its just a feature to allow you to create more classes in one file.

A class is a data structure to define properties and methods. ( Or values and code) and a class is typically not mutable without changing its definition.

A dictionary is a data structure that contains a set of key value pairs. It can’t contain code, and is mutable.

Every node you work with is a class and most likely a class inheriting from another class.

If you look at the Node documentation you see another class inherited this class. And you can look at any child class and see it’s ancestry

A sprite2d class inherited from Node2d and CanvasItem and Node and Object,

This allows the sprite to call any method or use value defined by its parent classes.

1 Like