What is virtual method?

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

what is virtual methods?

:bust_in_silhouette: Reply From: magicalogic

A method with just a pass statement inside. Gdscript doesn’t really support virtual methods like in java where there are abstract methods which have to be implemented in the child class before it can be instantiated so Godot will not try to ensure the method is implemented in the child class.

:bust_in_silhouette: Reply From: Wakatta

Virtual Methods

Are used for inherited classes when you need a different behaviour from said method as defined in the base class.

The act of doing so is called superseeding and if you’ve used Godot long enough you’ll realize that you’ve been doing this all along.


Base Class

class Node:
    virtual func _ready():
         print("get node ready")

Inherited Class

class Control extends Node:
    func _ready():
        print ("do something else")

var ctrl = Control.new()

So by default all sub classes will have the ready method and be able to change it’s operation.

With a normal method this is not so infact the complier may throw an error saying that the function cannot be redefined meaning in the example above the inherited Class will still have the ready method but what ever is defined in the base class will be the operation for every class that extends it or subclasses