How to access the current scene nodes from new node added by .instance() method

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By damncoder
:warning: 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 :frowning:

Thanks

try: get_node("../node_wanted
../ acess the root node

tell me if it works :slight_smile:

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” :frowning:
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… :smiley: but feel free to not do so… :smiley:

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 :slight_smile:

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

:bust_in_silhouette: Reply From: azurvii

Haven’t tested myself, but I think this is what you are looking for:

get_tree().get_current_scene()

Ohh…absolutely forget about this! It works! Thank you.

damncoder | 2016-05-15 18:45

You’re welcome :slight_smile:

azurvii | 2016-05-16 00:58

:bust_in_silhouette: Reply From: Warlaan

I am sorry if I don’t provide the answer you are hoping for, but you should avoid accessing nodes in the way you described.
By referencing nodes outside of your scene you are a) making assumptions about the scene that your scene is going to be instantiated in without communicating them and you are b) creating a scene that cannot be run on its own.

In order to avoid that you could either store the referenced nodes in variables and require the instantiating code to fill them (either directly or using a setter or setup-Method - a setup method would probably be the best way as it would list all required variables as parameters), or you could use signals in those places where you would call methods on the referenced node.
For example if the scene you are creating is a target crosshair and you want the turret to always target it you could add a signal that triggers whenever the crosshair moves (“onMove” or something) and passes the new position as an argument. In the instantiating scene you would then assign a function to the signal that turns the turret.
That way the crosshair can be tested by itself and if someone else wants to use the scene they don’t have to read the source code or run it first to see which nodes are required with which names, because they can just open the menu for attaching signals and see a list of all signals.

Hmm… So, what is your solution for “Enemy AI” which spawned on the map (current scene) and must search targets in scene for attack?

-We must spawn random types of enemies in random position with random time. Spawned enemy don"t know situation on the map at the moment. How we can use “signals”? “Enemy AI” must scan all node tree, identify concrete node as target (turret) and attack it. If AI don`t find any targets, it switch to “idle” mode and continue scanning targets.

get_tree().get_current_scene().get_node("../MainLoop").get_child()

must works and it works - i tested it. Its wrong?

damncoder | 2016-05-16 07:28

I am currently working on something similar, and I actually have two solutions implemented. In my game the AI can have exchangeable strategies, types of perception, movement and skills.

Now when the game spawns a new creature it passes the level node to it so that the creature’s perception node can call a function on the level node that returns all creatures of a given player. This is the method that I currently use which is intended for creatures that can always see the whole map.

If I should add creatures later that should only care about enemies in a certain area it would be redundant to have the level return all possible targets and then write code yourself that filters out which ones are in the correct zone, since that’s exactly what physics engines are made for.
So in that case I would add an Area2D or something similar to the perception scene and use that to request all objects inside it.
Unfortunately you can’t name physics layers, so if you want to separate targets from other colliders using layers you still need to open the perception scene to tell which objects are returned as targets.
Alternatively you can add a method to all potential targets (something like getType() or getFaction()) that returns something like “AI” or “neutral” etc. That way you can skip all found colliders that don’t have that method and filter the rest by their faction / type.

That way querying objects is reduced to either of these two options:

  1. use a function call to have a node report their subnodes (so the code that defines which nodes to report is part of the scene that needs to work with the code)
  2. use a built-in mechanic like Area2Ds or CollisionShapes to query nodes based on their position.

Your solution works of course, but I think it’s more error-prone, since anyone working with it needs to know where to put new nodes in the hierarchy and how to name them. If my level-node doesn’t have a “getCreaturesOfPlayer(…)”-function I can use an assertion. If your scene doesn’t have a turret-node you can’t tell whether it’s a bug or whether there just don’t happen to be any enemies at the time.

Warlaan | 2016-05-16 08:05

Hmm… Yes, it seems your solution is more flexible than mine. Ok, i will keep it in mind. Thanks.

damncoder | 2016-05-16 17:05