Cannot override add_child function in 4.0.2

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

I am making a PanelMarginContainer as a PanelContainer that has a MarginContainer as its only child.

I want the children of my class to get actually added as children to the inner MarginContainer:

class_name PanelMarginContainer
extends PanelContainer

var _margin_container := MarginContainer.new()

func _ready():
    add_child(_margin_container)

func add_child(
    node, 
    force_readable_name=false, 
    internal=Node.INTERNAL_MODE_DISABLED
):
    _margin_container.add_child(node, force_readable_name, internal)

but I get the error:

The method "add_child" overrides a method from native class "Node". 
This won't be called by the engine and may not work as expected. 
(Warning treated as error.)

In previous versions (ask.godotengine.org/43151, github.com/godotengine/godot/issues/49777 and ask.godotengine.org/145904) - up to at least 4.0beta12 - this override seems possible.

Is there any way to:

  1. Override add_child as was previously possible?
  2. Alternatively circumvent the problem and still be able to create a custom reusable container that holds a consistent interface with default containers?

Update: an unorthodox workaround is part of this proposal

1998marcom | 2023-06-18 19:37