Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | damncoder | |
Old Version | Published before Godot 3 was released. |
Hi,
We have main scene with enemy spawned by code:
var enemy_ship = preload( "res://enemy_ship.scn" ).instance()
get_node(".").add_child(enemy_ship)
How to access to main scene nodes from this node?
get_node()
does not work - it has access only to enemy_ship.scn nodes
Thanks
try: get_node("../node_wanted
../
acess the root node
tell me if it works
tiernich | 2016-05-15 05:15
My main node is “Main”. I want access to node “Turret”, which is child of “Main”
So, get_node("../Main/Turret")
?
damncoder | 2016-05-15 05:45
no, just: get_node("../Turret")
the ..
represent Main, the root node
tiernich | 2016-05-15 05:47
Not works, returns “null”
But “…/” - i get access to Main node
damncoder | 2016-05-15 05:56
you are doing something wrong, look this line:
get_node("../"+friend.get_name()).FriendsClose.remove(index)
this is from a current project where an instanciated object acess other(also instancied) object in the main scene…
tiernich | 2016-05-15 06:00
ok, thanks i must analyze my code…
damncoder | 2016-05-15 06:02
if you post part relevant of the code i can take a look
tiernich | 2016-05-15 06:05
i solve the problem by get_node("../").get_child(0)
, where 0 is “Turret”,
but… there is the way how to obtain ID of the child?
get_node("../Turret")
still not works…
damncoder | 2016-05-15 06:53
there is: Object instance_from_id( int instance_id ) -> Get an object by its ID
is this what are you looking for?
tiernich | 2016-05-15 14:07
sorry if i am being persistent here but look this image
in the left side i have a script of a ship not created yet. Look the line
print(get_node("…
now the right side, this is the structure of the game, the ship is created by an script in ApolonPlanet
and i can acess all structure… Stage1, Nav, Camera, System1 in this case… if you can give more info, maybe i can spot the error… but feel free to not do so…
tiernich | 2016-05-15 14:23
mmmm… i found int get_instance_ID ( ) const
- it returns ID.
instance_from_id( int instance_id )
Thanks. For now i don`t know how it will be works in my code - i must test it.
damncoder | 2016-05-15 14:30
Yeah, your scene system is clear. But … code on the left - is a ‘ship’ or ‘ApolonPlanet’ code?
I tried to access to main loop nodes exactly from ‘spawned ship’ code
My game system is:
-There are some turrets, controlled by player (spawned by main game loop script)
-There some enemies with “search random turret&attack” AI (spawned by main game loop script)
The problem is - how to find 'turret" node in the scene and identify it as “turret”
(there are many different game objects in the scene)
damncoder | 2016-05-15 14:54
code on the left is a script attached to the ship… ApolonPlanet spawn the ship
ok, i once have a similar problem, i have an AI that look for enemies in the scene, i attached a area2d and analyze to find ‘possible enemies’ like this:
if a body enter in contact with the area2d, access a variable in the script of the body that tell me his class, like friend, enemy, etc
something like:
_on_area2d_enter( body ):
if get_node("../"+body.get_name()).Class == "Enemy":
#attack
so, by body.get_name()
, if i have a Turret1 node, will be:
get_node("../"+"Turret1"
this is equal to
get_node("../Turret1)
then i acess the variable in his script
get_node("../Turret1).Class
dont now if this resolve your problem, but can give you some ideas
if you dont want an area2D, you can keep track of objects created and their type in array and search for then, when find, you will have his name and class(like enemy), by his name you can access it with get_node…
tiernich | 2016-05-15 16:27
Thank you for help. I used your recommendations and overall it works for me. I`ll continue optimizing my code.
damncoder | 2016-05-15 18:40