godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.84k stars 20.15k forks source link

Custom Themes execute code in Editor without Tool mode #94797

Open TheDuriel opened 1 month ago

TheDuriel commented 1 month ago

Tested versions

Reproducible in Godot Engine v4.2.2.stable.official.15073afe3

System information

Windows 10 - Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 2070 SUPER

Issue description

Custom Theme Resources will have their _init() function executed within the editor context, despite no Tool annotation being present.

This may also apply to other Resources defined in the ProjectSettings, if any.

Steps to reproduce

Define a custom Theme Resource as such:

class_name MyTheme
extends Theme

func _init() -> void:
    code goes here.
    For example you may subscribe to SceneTree.node_added() and modify the node.

Detailed example: https://github.com/TheDuriel/DurielUtilities/blob/main/FancyTheme/FancyTheme.gd

Define this Them as a Custom Theme in the Project Settings.

Restart the editor.

The Themes _init() function will not be executed, despite the lack of a Tool annotation.

Minimal reproduction project (MRP)

N/A

KoBeWi commented 1 month ago

Does not seem to happen with all resources (tried with the icon). Probably the Theme is loaded very early, before engine sets the editor hint, so the script instance is created as if the resource was loaded at runtime.

TheDuriel commented 1 month ago

Possibly just a 'needs documentation' case then. It was rather unexpected behavior. (my custom theme tried skinning my whole editor.)

KoBeWi commented 1 month ago

Well, regular scripts should not be executed in editor context. If the context is initialized too late then it's a bug. It can be documented as limitation if the fix is unfeasible.