Javatpoint Logo
Javatpoint Logo

Classes in GDscript

The body of a script file is an unnamed class, and it can only be referenced externally as a resource or file. Class syntax is meant to be compact and can only contain member variables or functions. Static functions are allowed, but not a static member (this is in the spirit of thread-safety since scripts can be initialized in separate threads without the user knowing). In the same way, member variables (including array and dictionaries) are initialized every time an instance is created.

Below is the example of a class file;

Inheritance

A class can inherit form

  • A global class
  • Another class file
  • An inner class inside another class file.

Multiple inheritances are not allowed.

Inheritance uses the extends keyword:

To check if a given instance inherits form a given class, the is keyboard can be used:

To call a function in the base class (i.e., one extend –ed in our current class), prepend. (dot) to the function name:

It is useful because functions in extending classes replace functions with the same name in their base classes. So if we still want to call then, we can use it. (dot) like the super keyword in other languages:

Class Constructor

The class constructor, called on class instantiation, is named _init. The constructors of parent classes are called automatically when inheriting a class. So there is usually no need to call .init().

Unlike the class of a regular function like in the above example with .some_func, if the constructor from the inherited class takes arguments, they are passed like this:

This is better explained through examples. Say we have this scenario:

There are some things to keep in mind here:

  1. If the inherited class (State.gd) defines an _init constructor that takes arguments (e in this case), then the inheriting class (Idle.gd) has to define _init as well and pass appropriate parameters to _init from gd.
  2. gd can have a different number of arguments than the base class State.gd.
  3. In the example above, e passed to the gd constructor is the same e moved into Idle.gd.
  4. If gd's _init constructor takes 0 arguments, it still needs to pass some value to the State.gd base class even if it does nothing. Which brings us to the fact that we pass literals in the base constructor as well, not just variables.

Example

Inner classes

A class file can contain inner classes. Inner classes are defined using the class keyword. They are instanced using the ClassName.new() function.

Classes as resources

Classes stored as files are treated as the resources. They must be loaded form disk to access them in other classes. This is done using the load or preload functions. Instancing of a loaded class resource is completed by calling the new function on the class object;

Exports

Class members can be exported. This means their value gets saved with the resource they are attached to. They are available for editing in the property editor. Exporting is done by using the export keyword:

An exported variable must be initialized to a constant expression or have an export hint in the form of an argument to the export keyword:

One of the significant benefits of exporting member variables is to have them visible and editable in the editor. Shipping is done by using the export keyword.

One of the primary benefits of exporting members' variable is to have them visible and editable in the editor. This way, artists and game designers can modify values that later influence how the programs run. a special export syntax is provided.

It must be noted that even if the script is not while at the editor, the exported properties are still editable.

See below for (tool)

Exporting bit flags

Integers that are used in bitflag can store multiple true/ false values in one property. By using the export hint [int, FLAGS], they can be sent for an editor.

Restricting a flag in any number of numbered flags is also possible. The syntax is similar to the enumeration syntax.

In the example, Fire has value 1, and Water has value 2, Earth has value 4, and wind corresponds to value 8. Usually, constants should be defined accordingly.

Using bit flags requires some understanding of bitwise operations. If in doubt, Boolean variables should be exported instead.

Exporting arrays

Exporting arrays works but with an important caveat: while regular arrays are created local to every class instance, exported arrays are shared between all instances. This means that editing in one instance will cause them to change in all other instances. Exported arrays can have initializers, but they must be constant expressions.


Setters/getters

It is useful to know when a class' member variable changes for whatever reason. It may also be desired to encapsulate its access in some way.

For this, GDScript provides a setter/getter syntax using the setget keyword. It is used directly after a variable definition:

Whenever the value of a variable is modified by an external source (i.e., not from local usage in the class), the setter function (setterfunc above) will be called.

This happens before the value is changed. The setter must decide what to do with the new value. Vice-versa, when the variable is accessed, the getter function getterfunc above) must return the desired value.

Example:

Either of the setter or getter function can be omitted:

Get/Setter is especially useful when exporting variables to the editor in tool scripts or plugins, for validating input.

Local access will not trigger the setter and getter. Here is an illustration of this:






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA