Godot Version
4.2.1
Question
How to access Scenes Nodes from File System without opening or instantiating them ? ,
e.g:
I have two scenes , The first one is the bass scene with “res://Test.tscn” path and the second is derived from the first one lets call it’s path is “res://TestChild.tscn” ,
And I want to access “res://Test.tscn” nodes,
I tried using :
load(“res://Test.tscn”).get_local_scene()
but that returns “res://TestChild.tscn” scene that derived from “res://Test.tscn” scene ,so how to get and access a scene and its nodes by its path without going to its derived by scenes ?
You don’t. The question is nonsensical. A packed scene file does not contain nodes that you can access. In order for the nodes to even exist, the scene needs to be instantiated.
I don’t want to create a copy of any thing ,I just want to edit the scene nodes by code without instentiating it to other scenes or opening it so its visible in the scene hierarchy .
instantiated ? ,Do you mean that I must instantiate an existing saved scene so i can edit its nodes even for editor tool purposes ?
Yes. Your question is like asking “How do I open a book without opening it?”. Instantiating a scene is how the nodes get created in the first place.
Maybe my question is complicated …
No , I see my question is like “How to open a book without seeing the book is being opened?” or “How to get a sentence from a book without seeing the book itself?” and both are possible ,
I mean when I have the book ,is not that mean that I have its papers too? ,so why i need to go and create the papers when I have the book itself" ?
I have a scene and it contains some nodes but it’s closed(It exist in the file system place but its not open in the editor itself),
I hope it’s clear now .
A packed scene file is just a description of how to create node objects. It doesn’t actually contain the node objects themselves because the node objects can only exist in the engine, not in a file. The process of actually creating these node objects is called instantiation. Until the packed scene is instantiated, the nodes do not exist.
You use the SceneState
fetched from PackedScene.get_state()
.
Accessing Nodes
var packed_scene = load('res://scene.tscn')
var state = packed_scene.get_state()
for i in state.get_node_count():
var node_name = state.get_node_name(i)
print(node_name)
Getting the Parent PackedScene
If the root node does inherit from a parent scene, get_node_instance(0)
will return the packed scene of the parent scene.
It may also be of use to know that get_node_index()
will return the index the node is in the scene tree. This index includes the order of the parent scene’s nodes in the derived scene.
var packed_scene = load('res://derived.tscn')
var state = packed_scene.get_state()
if state.get_node_instance(0):
var parent_packed_scene = state.get_node_instance(0)
var parent_path = parent_packed_scene.resource_path
print(parent_path)
for i in parent_packed_scene.get_node_count():
# Handle parent scenes here.
pass
1 Like