|
|
|
|
Reply From: |
Zylann |
When you load a scene in Godot, its variables are set before the children to be added to the tree, so it’s possible your child node doesn’t exist at the time. It could even happen that your node isn’t in the tree yet, which is fine since the loader only sets variables.
If you want to deal with loading, you can also do your initialization in _ready() so that you’ll have access to the node there.
Hi Zylann ,
Thank you for the answer!
The CustomNode’s _ready()
called only once: when i add it to the root.
The Sprite node exists (I added it manually in the editor), and it is in the tree of course,
I do not understand, why has_node()
works in one case (selection), and why not in the other (save scene).
I did not change anything between the 2 operations…
bruteforce | 2017-07-13 17:13
The editor still has to add it to the game tree. The editor has its own tree. They aren’t the same thing.
JTJonny | 2017-07-13 17:34
Thanks!
I just need a starting point (function/event… anything), when a node is selected, and I can work with this selected node (in tool mode).
I assumed, this point is the NodePath variable’s setter method…
How should I do this properly?
bruteforce | 2017-07-13 17:53
Thank you!
The onready var is_ready = true
suggestion does not seem to work in tool mode (the variable has no value: not true, not false… just null).
I tried the call_deferred
too.
The new test code:
tool
extends Node2D
export(NodePath) var path setget setPath, getPath
func getPath():
return path
func setPath(newPath):
print("setPath()")
path = newPath
call_deferred("set_my_path")
func set_my_path():
print("set_my_path()")
Output 1 (when I select a node):
setPath()
set_my_path()
The functions called as I expected.
Output 2 (save scene without any modifications):
setPath()
The set_my_path()
was not called.
Output 3 (save scene, afler I changed anything in the code… ie: empty line then save)
setPath()
setPath()
set_my_path()
The setPath()
called twice, and the set_my_path()
called once…
I still don’t know what is happening here exactly, but I just realized maybe it doesn’t matter.
(Is there a detailed description somewhere?)
Now I think, that the set_my_path()
(via call_deferred
) is called only, if something has changed (in this case the has_node()
also works perfectly).
This information is actually enough for me.
What do you think?
bruteforce | 2017-07-14 11:17
From my understanding you want to use a function every time you change your path variable in the editor, or I’m I missing some thing? I’m asking this, because setget is not very useful unless you change the path.
JTJonny | 2017-07-14 17:18
Yes, I want to do some things, mostly draw helper lines, change textures… etc. based on the selected node. Only in tool mode.
That’s why I need this method.
I just surprised that the setter called when I save the scene… and the different outputs confused me.
Thank you for your time, and sorry for the lame questions… I don’t have much experience with Godot (and game development)
bruteforce | 2017-07-14 19:15
They aren’t lame questions. I don’t really know why setgets is called when the scene is loaded and when the scene is closed. I guess those variable are changing for some reason. I know when I was first messing with setget my game would crash on start up for this very reason, and I had to play with it until it worked. Good luck on your game!
JTJonny | 2017-07-15 01:45