Unique resources in @tool scripts in editor - best practices

Godot Version

4.3

Question

I have a scene Box of AnimatableBody type with a single CollisionShape2D (rectangle). Box has set_size(Vector2) method which resizes a rectangle collision shape. Of course I want every instance of the Box to have it’s own size so I create unique shapes for them in _ready() like this:

@tool

@onready var _collision: CollisionShape2D = $CollisionShape2D
@onready var _rect: RectangleShape2D = _collision.shape

func _ready() -> void:
	if not Engine.is_editor_hint():
		_rect = RectangleShape2D.new()
		_collision.shape = _rect

I use if not Engine.is_editor_hint(): here because it made me crazy that once in a while a shape inside editor was recreated for Box.tscn, making my git repository dirty with some unintentional changes to Box.tscn. Without if these resource are recreated every time I reopen and save the scene.

But now, since I use LDTK importer and import addons scripts are also considered “editor hints” then every time I call set_size() for new instance in LDTK importer it also modify original scene because inside Editor it’s not unique resource anymore.

So what’s the best practice here? How to make the shape unique per every instance but also prevent any unintentional changes in original Box.tscn scene (so also in git repository)?

I think there is a checkbox in the inspector for resources, when it is an exported variable, to “make local”.

This would be the shape resource of the collision shape child of the scene.

Or you could make the sub scene local to scene.

1 Like