godot: Exported variables break setget methods that are "ready" dependent
Godot version: 3.1.6
OS/device including version: Solus Gnome 4.0
Issue description:
Alright, this sounds kinda complex, but the issue is simple, so I’ll give context first.
I have this script:
extends Node
export (NodePath) var node_path = ".."
onready var node = get_node(node_path)
export (bool) var enalbed = true setget set_enabled
func set_enabled(enable):
enabled = enable
node.set_process(enabled)
The thing is, if I change the Enabled
value in the inspector, the set_enabled
method crashes when running the scene, because seems like the method is called before the _ready
callback, or at least before the onready
variable being set. So it reports that node
is null, therefore it is unabled to call set_process
.
I think that this could be fixed by changing the order in which setget
methods are called, making them wait for idle
process at least?
Steps to reproduce:
- Create a Node
- Add a child Node
- In the child Node add a script
- Copy paste the script above mentioned
- In the inspector set
Enabled
to false - Test the scene
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17 (15 by maintainers)
What I always do is
This will delay the setters instead of returning and not setting anything at all. Still I don’t like typing it out every time.
For this I figured I should update such properties deferred most of the time:
The above mechanism may cause
_update_properties
called multiple times in a single frame. Here’s more a sophisticated and optimized mechanism if you need to change properties that update often (picked this up from engine internals):I usually do this in my scripts like so:
Note that #6491 will help remove a few lines of that boilerplate, even more if it is made to work with
onready
nicely.There is the
Node.is_inside_tree()
method for that, this fixed the problem by the way!Two simple workarounds: https://github.com/godotengine/godot-proposals/issues/325#issuecomment-845668412