0 votes

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.


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

Scene structure:

        - 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:


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

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:

func take_damage (dmgToTake):

    curHp -= dmgToTake

    if curHp <= 0:
        while(i < 5):
            rootNode = loadLoot[i].instance()
            random = rand.randf_range(-25, 25)
            enemyY = self.position.y - random

            random = rand.randf_range(-25, 25)
            enemyX = self.position.x - random
            rootNode.global_position = Vector2(enemyX, enemyY)
            i = i + 1


func die ():

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):
            random = rand.randi_range(0, 4)
            rootNode = loadEnemies[i].instance()
            rootNode.global_position = Vector2(spawnRangeX[1], spawnRangeY[4])
            i = i + 1
in Engine by (22 points)
edited by

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.

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

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)


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

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.