Is there a way to give the editor a hint about the size of a custom CanvasItem?

Godot Version

4.0 and onwards

Question

The Godot editor knows the dimensions of some CanvasItems, as it uses an orange border around these items when they are selected. The same border is also visible in a running game when you switch to the remote scene tree in the editor and select a node.

However, for your (gdscript) classes that inherit from CanvasItem, the Editor does not display the orange bounding box (because, obviously, it can’t know the bounding boxes of these CanvasItems). In a running game, the orange box is shown, but it has a small, standardized size. There used to be a get_rect_item in Godot 2 which the editor used to that purpose, but it is no longer exposed to scripts in Godot 3.

Is there a way to tell the Godot Editor and Engine the dimensions of CanvasItem-derived class via gdscript code so these hints can be shown? The reason I ask is because this would be helpful in debugging.

You can use custom drawing to draw the debug box yourself or inherit from something further up the hierarchy that maintains a bounding rect, like Control

That’s true and I’m aware, but I’m trying to re-use functionality that already seems to get provided by the Engine. For example CanvasItem has a item_rect_changed() signal, and it would be nice to have a way to define this item rect in my own classes that inherit from CanvasItem (or Node2, for that matter).

CanvasItem never emits this signal. It just implements it as a base functionality for derived classes to call. If you look at CanvasItem class in source code, you can see it doesn’t keep any Rect2s and never calls item_rect_changed() itself.

2 Likes