How to initialize variable after instancing a node

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

Im trying instance the Spaniard Enemy Scene to my Enemy Node to be a child of my Battle Scene so i can access the AnimPlayer but it keeps initilizing the variable before the scene gets instanced and crashes.
How can i make it so that it will initialize the variable after my scene has been instanced

extends Control

#ENEMYTYPE ONREADY
onready var SPAWN=$Enemy
onready var Spaniard=preload("res://Scenes/Spaniard_Enemy.tscn")

#PLAYER/ENEMY ONREADY
onready var Player=$Player/Player
onready var Enemy=$Enemy/Enemy
onready var PlayerAnim=$Player/Player/Sprite/Animate
onready var EnemyAnim=$Enemy/Enemy/Sprite/Animate

#QnA ONREADY
onready var QnA=$QnA

#VARIABLES
var num

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

func _process(delta):
	if PlayerAnim.current_animation=="Hurt" or EnemyAnim.current_animation=="Hurt" or PlayerAnim.current_animation=="Attack" or EnemyAnim.current_animation=="Attack" or PlayerAnim.current_animation=="Dead" or EnemyAnim.current_animation=="Dead":
		get_node("Choice1").disabled=true
		get_node("Choice2").disabled=true
		get_node("Choice3").disabled=true
		get_node("Choice4").disabled=true

I can’t figure what do you mean, show us the error shown in the debugger.
(take a SS of your godot window if you can)

Also, as a suggestion, change your _process() content to the next code to be more readeable:

func _process(delta):
	var animations = [PlayerAnim,EnemyAnim]
	var anim_states = ["Hurt","Attack","Dead"]
	var choice_nodes_to_disable = [$Choice1,$Choice2,$Choice3,$Choice4]

	for anim in animations:
		if anim.current_animation in anim_states:
			for choice in choice_nodes_to_disable:
				choice.disabled = true

hinasis | 2018-07-21 05:28