godot icon indicating copy to clipboard operation
godot copied to clipboard

Setting exported node properties from the inspector bypasses their setter

Open theraot opened this issue 2 years ago • 3 comments

Godot version

v4.0.alpha11.official [afdae67cc] (when the feature was added) and newer (still happens in RC6)

System information

Windows 10

Issue description

I'm unable to handle when the user sets an exported node property (this feature: https://github.com/godotengine/godot/pull/62185) from a setter in the editor (tool script).

I don't know if this is intentional or not, but for me it was unexpected. I had to revert back to NodePaths.

Steps to reproduce

  • Create a scene
  • Attach a script to a node
  • Add @tool at the top of the script
  • Export a node property with a setter, preferably with an easy way to see if it executed:
    export var node:Node:
    set (mod_value):
    	prints("node", mod_value)
    	node = mod_value
    
  • Save, close and open again the scene to make sure the tool script is running in the editor
  • Set the node property from the inspector
  • Observe that the setter didn't run

The setter is not bypassed when running the game, it is only bypassed in the editor.

We can compare with a NodePath property, which setter is not bypassed regardless if it is in the editor or in game runtime.

Minimal reproduction project

nodepath.zip

theraot avatar Feb 27 '23 14:02 theraot

It definitely works, but I had some issues getting it to run at first.

Can you try saving / closing and opening the scene and see if that makes the script work?

I just did this myself a few hours ago, and I noticed my GDScript code wasn't getting updated when I made changes. Closing and opening the scene seemed to make the setter work for me.

lyuma avatar Feb 28 '23 05:02 lyuma

Nope. I cannot get the setter of the node export to work. Addendum: on the editor I cannot get it to work, on runtime it works.

theraot avatar Feb 28 '23 07:02 theraot

Ah, I missed that there was both a Node and a NodePath. Only the NodePath export works.

I can confirm the @export var node: Node does not seem to invoke the getter/setter while in the editor.

Set metadata/_editor_prop_ptr_node
Set metadata/_editor_prop_ptr_node
set node path
node_path 
Set node_path
set node path

it seems that somehow it uses metadata/_editor_prop_ptr_node instead of node for the node variable. If there was a custom _set, perhaps it can possibly detect changes to that property, but it would be a hack.

lyuma avatar Feb 28 '23 08:02 lyuma

Adding to this: the value of a @export var node: Node property will always be null inside the editor (i.e. for @tool scripts), even when a node is selected.

michaelhue avatar Apr 12 '23 09:04 michaelhue

This issue has the topic:gdscript label, but it exists in C# as well (see #74141, #76182, or #76190).

I'll also add that the above comment says the inspector will always show null, however I've found that the inspector always shows what you drag into the property, regardless of the actual property's value (i.e. drag a node in, set to null in code, still see node in inspector).

Tuckertcs avatar Apr 18 '23 13:04 Tuckertcs

@Tuckertcs The inspector will show the Node correctly. What @michaelhue was saying is that if you try to get the value from a tool script (a script running in the editor) it will get null.

theraot avatar Apr 18 '23 18:04 theraot

The example given here: https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-get-property-list works but when holding_hammer is of type Node, the setter logic actually never runs therefore making the use case impossible.

krazyjakee avatar May 12 '23 18:05 krazyjakee