godotengine / godot

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

Exported typed variables in a tool script have wrong initial value if not initialized #62363

Open KoBeWi opened 2 years ago

KoBeWi commented 2 years ago

Godot version

ecf1877

System information

Windows 10 x64

Issue description

@tool
extends Node
@export var test: int

test will have value saved to scene even when it's not changed. It will save 0. The revert arrow appears, but clicking it does nothing.

Steps to reproduce

  1. Create @tool script
  2. Export a typed variable, but don't initialize it
  3. Try to revert it to default in the inspector

Minimal reproduction project

No response

dalexeev commented 1 year ago

Addition: this bug occurs after restarting the editor (no reset buttons). After saving the scene, the inspector starts working correctly again.

I could not reproduce the bug with saving default values for nodes. Only for resources ([sub_resource]), but seems to be an old bug not related to @tool.

https://github.com/godotengine/godot/blob/a83eb16fba5bb3da086b41cbd79e6f95b09eb8ee/scene/resources/resource_format_text.cpp#L2174-L2178

hsandt commented 7 months ago

Just a few pictures to illustrate what happens when adding @tool later, with numerical properties:

# keep commented out for now
#@tool
class_name Arena
extends Node2D

@export var tilemap_cell_size: float = 16.0
@export var tilemap_width: int = 72
@export var tilemap_height: int = 37

Create Arena node, change properties to see revert arrow button appear:

image

Then uncomment @tool at the top of the script. Issue won't appear at once, but after editor reboot, revert buttons disappeared:

image

Oddly enough, in the reverse way, if you temporarily comment out @tool, the reset to default buttons will reappear immediately, and not disappear even if you uncomment @tool until next reboot.

So my current workaround is to comment @tool once during my work session if I need to revert the properties at some point.