0 votes

what is virtual methods?

Godot version latest
in Engine by (25 points)

2 Answers

0 votes

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.

by (1,051 points)
+1 vote

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

by (4,304 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.