Godot-proposals: Add a way to declare functions as abstract in GDScript

Created on 2 Jun 2020  路  5Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:

Basically Terraria or Starbound, but entirely exploration focused.

Describe the problem or limitation you are having in your project:

When overloading a function, I want to make sure the function is overridden in derived classes. I don't want to have myself or anyone else in the team wasting time looking for something as simple as not overriding a function.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

In other languages, I would do this by setting the function to be abstract or pure and leaving out the function body.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

Here is an example of what I tried to do a few seconds earlier:

abstract func get_acceleration();

This is how I expect it to work in most languages that allow for object oriented programming. If the function is not defined in the child class, an error is thrown immediately. It helps to find bugs early instead of some time later when the function is actually called (granted I remember to cause an error in the "unimplemented" version).

I believe I heard recently that GDScript was being re-written for Godot 4. This is definitely something I would want to see added to help keep scripts a little more bug free.

If this enhancement will not be used often, can it be worked around with a few lines of script?:

I'd use this very often, but regardless, the alternative would be to define the function in the base class and hope it is overridden.

Is there a reason why this should be core and not an add-on in the asset library?:

I believe this is something that should be built in to the language in the same way static type checking should be built in. It helps to identify and correct errors quickly. A benefit anyone who writes scripts could take advantage of, regardless of their project.

gdscript

Most helpful comment

@cloewen8 assert() allows displaying a custom message since Godot 3.2:

assert(false, "Please override `some_method()` in the derived script.")

I would recommend this over push_error() as it will cause the project execution to stop, making the error easier to notice.

All 5 comments

I usually just use push_error() or printerr() in the base function. This warns you if a child class is not implementing said function.

I usually just use push_error() or printerr() in the base function. This warns you if a child class is not implementing said function.

Thank you, I thought I searched for a function like these but apparently I missed them. A similar result to using assert in the base function but at least now I can describe the error.

@cloewen8 assert() allows displaying a custom message since Godot 3.2:

assert(false, "Please override `some_method()` in the derived script.")

I would recommend this over push_error() as it will cause the project execution to stop, making the error easier to notice.

We can even write abstract classes

func _init():
    allowInstantiation()

func allowInstantiation():
    assert(false, "This class is abstract")

And in derived class

func allowInstantiation():
    pass

What a language!

We can even write abstract classes

func _init():
  allowInstantiation()

func allowInstantiation():
  assert(false, "This class is abstract")

And in derived class

func allowInstantiation():
  pass

What a language!

I mentioned this in the original post as my current workaround. It works, only problem is that the function has to be called for the assertion to be invoked.

Was this page helpful?
0 / 5 - 0 ratings