Godot Version
4.3
Question
I keep running into an issue that feels messy to resolve and could use some suggestions.
So, to explain:
- I have a scene that has Nodes which are referenced in its script using @onready.
- I have another system which instantiates the scene
- This system has context it needs to pass to the @onready nodes
- Since the node was freshly instantiated, it has not called ready and the system ends up referencing these null @onready fields
Shoving the data that the nodes contain into the base node of the scene isn’t really an option. So I feel like I can only instantiate and then update them the next frame.
Maybe there’s a way to invoke ready right after instantiating?
Or maybe there’s a way to have these nodes ready at init time instead of ready?
From the above it seems like you make a variable in onready and did not provide it’s value that’s why you got null.
Can you share the screenshot of code or error which you are encountering so I tell you more precise what is wrong here.
Try using calling deferred() function when instantiating a scene
Ah I realized I wasn’t instantiating a scene resource and was instead just calling new() on the class type I defined. Still seeing if this works though.
Thanks
Ok, based on online searches about this error:
“Failed to instantiate scene state of “”, node count is 0. Make sure the PackedScene resource is valid.”
It seems my scene may be corrupted somehow…
(It definitely has nodes inside of it)
Someone mentioned reading the scene as text, but it seems fine to me (not really sure what I’m looking for though):
Figured it out (not really…)
I ended up detaching the script temporarily and running the game.
Launching showed a runtime(?) error in the script and I commented out the portion and reattached the script which made everything work.
Wish I could say I understood what just happened though.
The error was on another instantiate and it said it was cyclical- weird thing is that the function shouldn’t be getting called normally so not sure there either.
But things are working again sooo
1 Like