How to access Nodes From Instanced Scene

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

I have a main scene that preloads an enemy scene as a child for me and im trying to access its nodes but i cant seem to make it work.

Here is the code i used

onready var Spaniard=preload("res://Scenes/Enemies/Spaniard_Enemy.tscn")
onready var Enemy=$Enemy/Enemy

The spawn node Enemy is part of the main scene the other Enemy is Spaniard_Enemy.tscn the parent node of it is also called Enemy.

:bust_in_silhouette: Reply From: hinasis

As you preload a scene, you need to instance it in order to play with it.

You can change your first line to:

onready var Spaniard=preload("res://Scenes/Enemies/Spaniard_Enemy.tscn").instance()

Or call instance() later in the code, for example if you will have multiple copies of it:

var new_spaniard = Spaniard.instance()

You can now access to its childs and properties.

Then, if you want to add to viewport/screen, you need to add it to tree too:

add_child(new_spaniard)

I did instance it in my code but i can’t get the nodes

func _ready():
var instance
if Ctrl.Level==1:
	instance=Spaniard.instance()
	$Enemy.add_child(instance)

Newby | 2018-07-21 12:16

Where and how are you trying to get the children nodes?
Also, make sure the if statement is true, it may be the reason.

hinasis | 2018-07-21 13:50

Battle Scene nodes

Control<-------Parent of Battle Scene
Button1
Button2
Button3
PlayerSpawn
	Player
EnemySpawn
	Enemy <------(Parent of Spaniard_Enemy)instanced in script trying to access it and its children

I used

onready var Enemy=$Enemy/Enemy
_______________________________________________
var Enemy=null
_ready():
Enemy=Enemy=$Enemy/Enemy
_______________________________________________
var Enemy=null
var Spawned=false
_ready():
Enemy=Enemy=$Enemy/Enemy

if Ctrl.Level==1:
	instance=Spaniard.instance()
	$EnemySpawn.add_child(instance)
	QnA=$QnASpain
	Spawned=true
if Spawned==true:
	Enemy=$Enemy/Enemy

none of these worked
What am i doing wrong

Newby | 2018-07-21 15:08

I simple example works for me:
(paint tutorial time)
Imgur: The magic of the Internet

I can’t help you at all, because I miss some things:

  • what Ctrl.Level==1 is
  • what Enemy=Enemy=$Enemy/Enemy is (is it Enemy = $EnemySpawn/Enemy ?)
  • what are you trying to do in:
if Spawned==true:
    Enemy=$Enemy/Enemy

and where are you trying to access the instanced spaniard, i can’t see any get_node() or get_child()

If you agree, upload to some cloud storage your compressed project and I’ll take a look.

hinasis | 2018-07-21 17:18

Ctrl.Level1 is an autoload script i made as a control comand
Yes Enemy=$EnemySpawn/Enemy(forgot to write Spawn :P)
For the Spawned==true you can see that in my if statement Crtl.Level1==1 i instanced my Spaniard scene and declared that Spwaned is true (i used it to check if enemy has loaded in yet, didn’t work)
If you can refer to my first question you can see that i preloaded Spaniard_Enemy.tscn into my scene as Spaniard.
I also want to clarify that im trying to get the script in the parent node of Spaniard an its child AnimationPlayer.

Zip link name Historian Remastered
GitHub - PotatoNato/The-Historian

Newby | 2018-07-21 22:02

Figured the out problems
Thank you for your help

Newby | 2018-07-22 01:14