+1 vote

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.

in Engine by (399 points)

1 Answer

0 votes

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)
by (327 points)

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)

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

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

I simple example works for me:
(paint tutorial time)
https://imgur.com/a/gT17Dlc

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.

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
https://github.com/PotatoNato/The-Historian

Figured the out problems
Thank you for your help

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.