Instancing a node causes it to lose it's script properties

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

This is a bit of a weird one I can’t figure out. So I am creating a game and I have an Enemy node with a basic follow/hunt script but when I try to instance them to spawn in, they seem to lose the script functionality, so instead of coming after me, they just stand there.

EDIT:

Sorry about lack of information, Enemy gets spawned in directly under MainScene

Scene structure:

MainScene
        - Canvas Layer
        - TileMap_Ground
        - Player
        - TileMap_Above
        - Camera2D
        - Chest
        - SpawnTrigger
        - SpawnArea
        - Enemy (I'm assuming it gets placed after everything when it spawns in)

Enemy Node:

KinematicBody2D
        -Sprite
        -CollisionShape2D
        -Timer

Enemy Script:

extends KinematicBody2D

var curHp : int = 5
var maxHp : int = 5
var i = 0
var enemyY
var enemyX
var rootNode

var moveSpeed : int = 150
export var xpToGive : int = 30

var damage : int = 1
var attackRate : float = 1.0
var attackDist : int = 80
var chaseDist : int = 400

var loadCoins = load("res://Coins.tscn")
var loadSword = load("res://Sword.tscn")
var loadArrow = load("res://Arrow.tscn")
var loadBow = load("res://Bow.tscn")
var loadShield = load("res://Shield.tscn")

var loadLoot = [loadCoins,loadSword,loadShield,loadBow,loadArrow]

var rand = RandomNumberGenerator.new()
var random

onready var timer = get_node("Timer")
onready var target = get_node("/root/MainScene/Player")
onready var main = get_node('/root/MainScene/')

func _ready ():
	
	var screen_size = OS.get_screen_size()
	var window_size = OS.get_window_size()
	OS.set_window_position(screen_size*0.5 - window_size*0.5)
	timer.wait_time = attackRate
	timer.start()

func _physics_process (delta):
	
	var dist = position.distance_to(target.position)
	
	if dist > attackDist and dist < chaseDist:
		
		var vel = (target.position - position).normalized()
		
		move_and_slide(vel * moveSpeed)
		
func _on_Timer_timeout():

	if position.distance_to(target.position) <= attackDist:
		target.take_damage(damage)

func take_damage (dmgToTake):
	
	curHp -= dmgToTake
	
	if curHp <= 0:
		while(i < 5):
			rootNode = loadLoot[i].instance()
			random = rand.randf_range(-25, 25)
			#random.randomize()
			enemyY = self.position.y - random
	
			random = rand.randf_range(-25, 25)
			#random.randomize()
			enemyX = self.position.x - random
			main.add_child(rootNode)
			rootNode.global_position = Vector2(enemyX, enemyY)
			i = i + 1
			
		die()

func die ():
	target.give_xp(xpToGive)
    	queue_free()

Spawn script:

extends Area2D

var i = 0
var rand = RandomNumberGenerator.new()
var random
var savedRand
var spawnX
var spawnY

onready var spawnArea = get_node("/root/MainScene/SpawnArea")

var loadEnemy = load("res://Enemy.tscn")
var loadEnemies = [loadEnemy,loadEnemy,loadEnemy,loadEnemy,loadEnemy]
var spawnRangeX = [10, 50, 120, 180, 220]
var spawnRangeY = [15, 40, 75, 100, 150]

var rootNode

func _on_Area2D_body_entered(body):
	if body.name == "Player":
		while(i < 5):
			rand.randomize()
			random = rand.randi_range(0, 4)
			rootNode = loadEnemies[i].instance()
			spawnArea.add_child(rootNode)
			rootNode.global_position = Vector2(spawnRangeX[1], spawnRangeY[4])
			i = i + 1

I do not think that is enough information for somebody to help you.

Can you share the code for how you are instancing the Enemy node so people can take a look. And also some details of how your Enemy node / scene is structured would help.

AndyCampbell | 2020-12-09 12:28

I’ve updated it to include as much info as I can. Thanks

JustMyke | 2020-12-09 22:45

Have you tried using global_position instead of position and instead of (target.position - position) you can use position.direction_to(target.position) or with global_position global_position.direction_to(target.global_position)

PanTrakX | 2020-12-11 16:12

Hey,

Thanks for the reply. I tried that and it caused Vector2 issues. It’s not just the following script they lose, it’s also the loot drops as well.

I can try and work around the Vector 2 issues but I don’t think that is the cause.

Thanks though

JustMyke | 2020-12-14 09:28