Attention | Topic was automatically imported from the old Question2Answer platform. | |
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
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