Polymorphism in GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WuaUP

I’m good at C#, but I’m a rookie at Godot.

I want to do complex logic, so I will use a lot more C# in Godot.

I wish I could do some things in GDScript, but I’m ignorant.

In C# I can define a parent class and a daughter class that inherits from the parent.

I have the instructions “new” and “override” to specify the form of the inheritance. This is powerful.

In the son, with “override” I say it is compatible with the past.

With “new” I say that it is incompatible with the past, and that it uses the previous method of the father.

For example,
father fatherObject = new son ()

fatherObject is a parent object that uses a son object.

The way to use it will depend on the implementation of the methods, that is to say if when defining them in the son, “new” “override” is used in the methods or variables.

fatherObject will invoke the methods defined in the father if “new” is defined

fatherObject will invoke the methods defined in the child if they are found to be “override”

How is this done in GDScript?

Any link that explains it? I have not found. Thank you

Please, I need the translation of this code from C # to CDScript. Thanks for the answers!

// Example code in C#:

class A {
public virtual int Hello() {
return 1;
}
}

class B : A {
new public int Hello(object newParam) {
return 2;
}
}

class C : A {
public override int Hello() {
return 3;
}
}

A objectA;
B objectB = new B();
C objectC = new C();

Console.WriteLine(objectB.Hello(null)); // 2
Console.WriteLine(objectC.Hello()); // 3

objectA = objectB;

Console.WriteLine(objectA.Hello()); // 1

objectA = objectC;

Console.WriteLine(objectA.Hello()); // 3

/////////////////////////////////////////

WuaUP | 2018-05-28 18:22

:bust_in_silhouette: Reply From: eons

In GDscript, derived classes can override methods by default by writing one with the same structure (what you see with the virtual methods _init, _ready, etc.).

class Base:
  func foo():
    pass
  func bar():
    pass

and

class Derived extends Base:
  func foo():
    pass

will use the overriden (overwritten) foo and the base bar.

I recommend you to check

For tricks and techniques to simplify some structures and logic.

Thank you for responding, unfortunately the answer does not answer the question :frowning:

According to his explanation, GDScript only has override, but I am especially interested in the keywords “new”.

The power of the polymorphism lies in the implementation of the keyword “new” in the method that is overwritten.

In such a way that thanks to this we can make old objects support new implementations.

The important thing is that the class looks according to the filter of the nature of the variable.

// Example code in C#:

class A {
public virtual int Hello() {
return 1;
}
}

class B : A {
new public int Hello(object newParam) {
return 2;
}
}

class C : A {
public override int Hello() {
return 3;
}
}

A objectA;
B objectB = new B();
C objectC = new C();

Console.WriteLine(objectB.Hello(null)); // 2
Console.WriteLine(objectC.Hello()); // 3

objectA = objectB;

Console.WriteLine(objectA.Hello()); // 1

objectA = objectC;

Console.WriteLine(objectA.Hello()); // 3

/////////////////////////////////////////

This type of code has extraordinary power to make the new code fit seamlessly with the old code without having to refactor the entire application. I use it a lot to make applications evolve without killing previous code and in the particular case of games, a character can evolve with different evolutions.

How is this done in GDScript?
Thanks

WuaUP | 2018-05-28 18:18

ah, no, I do not think that is possible, also is some kind of method overloading and is not supported, the method name overrides the base methods no matter if the signature is different.

Since is dynamically typed (and inheritance is based on delegation), it cannot know what class you want to access, and will always choose B if defined.

The way to do that with this dynamic language is to have a condition where derived class method calls base class
if param == something: return .hello().


This may be probably possible to request for 3.1 which is getting optional typing.

If you want to ask for this feature on github, try to give some good examples of the usage to convince people because the idea of devs is to keep GDScript code base simple.

If you are interested, this is the PR which will be merged in a month or so.
https://github.com/godotengine/godot/pull/19264

eons | 2018-05-31 19:26