|
|
|
|
Reply From: |
Will Nations |
The issue here is that variables are initialized during (or a bit before? Can’ recall) the _init()
notification. Nodes enter the tree during the _enter_tree()
notification and they can’t even be guaranteed to have access to their children until the _ready()
notification fires.
This is because every node calls _init()
when first instantiated, followed by a series of calls as it’s local scene becomes a part of the scene tree: each node, from the top down, calls _enter_tree()
(resulting in a cascading downwards function call, so you know its parents exist already), and then _ready()
is called when all children have been added and are ready (resulting in a cascading upwards function call, so you know its children now exist).
If you add the onready
keyword to your property, then the variables will exist and be accessible because Godot Engine will wait until the ready call is ready-to-go before it attempts to initialize the property (which it will do just before it actually calls _ready()
).
Thanks for your answer, I understand what you mean. I changed the line with export variable to export(Color) onready var body_modulate setget set_color_for_sprite
. However, it didn’t work which is quite strange assuming you’re right. I was looking for a workaround and thanks to hilfazer’s answer I eventually came up with this:
export(Color) var body_modulate setget set_color_for_sprite
func set_color_for_sprite(new_color):
body_modulate = new_color
if (Engine.is_editor_hint()):
$sprite.modulate = body_modulate
func _ready():
$sprite.modulate = body_modulate
which does what I want - I can change the color in editor and it’s updated in real-time and then it’s set when I play the scene without breaking. The only annoyance is that when I save the scene (control+s) it outputs the same error as the last time. It doesn’t really change anything but it spams my output window a little.
EDIT: I can check child count using get_child_count() > 0
to get rid of that error but that looks pretty dirty. I’m wondering if this is a bug with onready
.
CosmicKid | 2018-02-02 14:46
I just tried this all for myself in my own editor.
I didn’t really need any of my recommendations. I was able to update the color and prevent errors both at design time and runtime using the following…
- Node2D (script)
- Sprite
Script:
tool
extends Node2D
export(Color) var color = Color(1,1,1,1) setget set_color
func set_color(p_color):
color = p_color
if has_node("Sprite"):
$Sprite.modulate = color
Now, when I updated it to use the onready
keyword and assumed that the node existed, I did get the desired functionality, but I would receive error messages only whenever I saved the scene (not whenever I just set the property in the editor). That might be a bug.
# Caused a bug on save
tool
extends Node2D
export(Color) onready var color = Color(1,1,1,1) setget set_color
func set_color(p_color):
color = p_color
$Sprite.modulate = color
Will Nations | 2018-02-02 15:33
That’s exactly what happens to me too. I’ll stick to has_node
, it’s much cleaner. Thank you for help!
CosmicKid | 2018-02-02 19:04