Can you use get_node when using it in a function that's loaded in via another script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BasDeBeer

I can’t wrap my head around this and maybe I’m approaching this from the wrong angle:

In my “MainScene” scene I have a bunch of tiles. I have another Scene called “Cursor” that I have placed in the MainScene. I want to be able to select the tiles with the cursor and be able to interact with them (upgrade them).

I simplified the code a bit to get to the problem.

In the Cursor scene I have:

var mainSceneNode = load("res://MainScene.gd").new()

func _input(event):
     if Input.is_action_just_pressed("key_z"):
          mainSceneNode.drop_item_on_game_area(cursorLocation)

in the mainScene I have:

func drop_item_on_game_area(cursorLocation):
      var tile = get_node("Tiles/Tile1")

The get_node works fine in the MainScene when I call this function from within the MainScene. But, when I call the function from within the Cursor scene (as showed above), it just returns null. I’ve tried a lot of stuff, like trying to reference to the whole tree
(get_tree().get_root().get_node("/root/MainScene/Tiles/Tile1") or with absolute path (get_node(“/root/MainScene/Tiles/Tile1”) .

Is it just not possible to use get_node when using functions from other scenes this way?

Hi,
your mainscene is a script? of what type is it?
Where do you add it as child into the tree?

Maybe when you print the tree on ready with print_tree_pretty ( ) will clearify the relations.

klaas | 2020-09-01 20:42

Hey Klaas,

This is the tree:

 ┖╴MainScene (Node2D)
    ┠╴Cursor (Node2D)
    ┃  ┠╴Position2D
    ┃  ┖╴Sprite
    ┠╴Tiles  (Node)
    ┃  ┠╴Tile1 (Node2D)
    ┃  ┃  ┖╴Sprite
    ┃  ┠╴Tile2 (Node2D)
    ┃  ┃  ┖╴Sprite

There list continues with 30 more tiles.

BasDeBeer | 2020-09-01 20:54

ok, this is the tree from MainScene view

But where is mainscene in the tree? Try to print the tree from root.

get_tree().get_root().print_tree_pretty()

Have you tried to find the node?

var node = get_tree().get_root().find_node("Tile1",true,false)
print_debug( node.get_path_to(self) )

klaas | 2020-09-01 21:02

Apologies, this is the full tree from root:

 ┖╴root
    ┖╴MainScene
       ┠╴Cursor
       ┃  ┠╴Position2D
       ┃  ┖╴Sprite
       ┠╴Tiles
       ┃  ┠╴Tile1
       ┃  ┃  ┖╴Sprite
       ┃  ┠╴Tile2
       ┃  ┃  ┖╴Sprite

If I try to run that code from within the _ready(): function it works:

…/…
At: res://MainScene.gd:25:_ready()

However, I run into the same problem if I try to run it from the drop_item_on_game_area(): function.

Error: Attempt to call function 'get_root' in base 'null instance' on a null instance.

BasDeBeer | 2020-09-01 21:21

so it seems you havent add the mainScene to the sceneTree, thats why get_root() failed because there is null returned on get_tree()

or do you think when you load() the mainscene you get a reference to the mainscene in the scenetree? thats not gona work.

Try this … (in cursor)

onready var mainSceneNode = get_parent()

func _input(event):
     if Input.is_action_just_pressed("key_z"):
          mainSceneNode.drop_item_on_game_area(cursorLocation)

klaas | 2020-09-01 21:27

I assumed load() would give a reference, yeah. But I see why that won’t work now :slight_smile:

Thanks man, this works now as I wanted to and gave me better understanding of the node/tree system!

BasDeBeer | 2020-09-01 21:33

:bust_in_silhouette: Reply From: IHate

get_node(“/root/MainScene/Tiles/Tile1”) or $“/root/MainScene/Tiles/Tile1” should work

var Tile_node = $“/root/MainScene/Tiles/Tile1”   

func do_stuff():
  
  Tile_node.cell_size = Vector2( 64, 64 ) # example

Make sure the Cursor scene is inside the MainScene maybe you didn’t add it as a child did you instanced the Cursor scene at runtime or did you add it to the MainScene in the editor?.

Hey,

I added the Cursor scene as a direct child of the MainScene. I added it via the editor.

get_node(“/root/MainScene/Tiles/Tile1”) gives the same null error sadly.

BasDeBeer | 2020-09-01 21:24