0 votes

I'm having trouble on trying to instance an enemy object. The enemy script and the enemy_group script are below

enemy.gd

extends KinematicBody2D

var SPEED = 300
var mov = Vector2(0, 0)
var frames = 0
var maxFrames

func maxFrames_():
    var nums = [30,60, 90] #list to choose from
    return nums[randi() % nums.size()]


func _ready():
    randomize()
    maxFrames = maxFrames_()
    position.x = 730
    position.y = int(rand_range(100, 700))
    SPEED = int(rand_range(200, 400))


# warning-ignore:unused_argument
func _process(delta):
    frames+=1

    if frames == maxFrames:
        get_tree().call_group("node_enemy_group", "enemy_generator")
        frames = 0


    mov.x = -SPEED

# warning-ignore:return_value_discarded
    move_and_slide(mov, Vector2(0, 0))
    if position.x < -40:
        queue_free()

enemy_group.gd

extends Node2D

export (PackedScene) var enemy

func _ready():
    pass

func enemy_generator():
    var new_enemy = enemy.instance()

    add_child(new_enemy)

I added a node group in the node that will control the generation of new enemies. I've already check if the node group name is correct and I don't know what to do. Can someone help me?

asked Jul 30 in Projects by Wagner28 (20 points)

1 Answer

0 votes
Best answer

You didn't say where the error was located, so I can only assume it's on this line:

var new_enemy = enemy.instance()

Based on your code, the variable enemy is not assigned a value. Stating

export (PackedScene) var enemy

exposes the enemy property to the Inspector, where it must be assigned a value. Have you checked there to ensure it is not null?

answered Jul 30 by kidscancode (10,848 points)
selected Jul 30 by Wagner28

I found the mistake! I was following the steps I saw in a course I've bought and now it's working, thanks!

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.