Creating Shape2D Subclass

Godot Version

4.1-stable

Question

I would like to implement a class representing a 2D sector for visualizing what the enemies can see in my 2D game. Because it is a shape, I wanted to make it inherit from the built-in Shape2D class so that I could use my Sector2D wherever a Shape2D is allowed. When I just write the definition for a draw function:

func draw(canvas_item: RID, color: Color) -> void:
	pass

I get the error

res://src/util/shapes/Sector2D.gd:12 - Parse Error: The method "draw()" overrides a method from native class "Shape2D". This won't be called by the engine and may not work as expected. (Warning treated as error.)

So, is defining another Shape2D impossible or is there another approach?

Maybe try this _draw?

I want to name it “draw” if possible because that’s what the function is called in Shape2D.

Only virtual methods can be implemented in a script. Shape2D.draw() is not a virtual method.

Shape2D is a class that represents physics shapes. It has nothing to do with visualization.

If you want to draw a random shape use a Node2D and implement its CanvasItem._draw() method as @Bohe suggested or use a Polygon2D if you just need to draw a polygon.