Method for scripted spawning enemies on Timer

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

I’m learning to make a space shooter game on Godot.

I’m working on spawning enemies on stage follow a plan which decides which enemy will be spawned and when they spawn. But I’ve searched and tried several methods but it didn’t work. You guys have any solution for me ? I’m just new to programming and this engine.

Thanks in advance !

Edit:
I posted this question because Im having an issue with method used in Your First Game tutorial. I plan to do the similar way in the tutorial but I will decide where and when an enemy come up, not randomly spawning.

First, I tried to spawn one scene of enemy (consists of 8 smaller scene) repeatedly when ever the “timeout” signal emits. But the scene just appeared on the screen once and didn’t appear again. (I’m sure that the Timer is autostart and not one shot.)

This is the stage’s scripts:

extends Node2D

var aship = preload("res://elements/elements/e_enemy_alpha.tscn")
export (PackedScene) var enemy

func _ready():
	pass

func _on_Timer_timeout():
	var e = enemy.instance()
	add_child(e)
	pass

Hi, you should share what you got till now so we can help. For example what you already tryed and the results you obtained.

The way mobs are spawn in “Your First Game” tutorial isn’t enough for your case?

p7f | 2019-01-22 17:00

Thanks for your reply! I’ve added some edit with my issues with the stage’s scripts above.

congbinh75 | 2019-01-23 05:52

The code seems right to me… could you share me the project so i can test what ia happening?

p7f | 2019-01-23 11:00

I haven’t known how to use Github yet so can you download it from Google Drive ?
My Project

congbinh75 | 2019-01-24 09:01

I’ll have a look. What version of godot are you using?

p7f | 2019-01-24 11:38

Hi,
The enemies DO spawn, but for some reazon the don’t move and stay out of the window. Let me see if i find something more.

p7f | 2019-01-24 12:07

:bust_in_silhouette: Reply From: p7f

Hey, i think i solved this:

The problem is not that the enemies don’t spawn… the problem is that your script does not detect the correct formation, so it does not move them, so they stay out of screen and you don’t see them.

Why is this happening? Because you relay of the 3 leftmost characters of the parent node to see the formation; you are doing this:

func _ready():
    current_formation = get_parent().name.left(3)
    add_to_group("enemy")
    _process(true)
    pass

func _process(delta):
    #Straight top to bot formation
    if current_formation == ("as1"):
        self.position += toptobot_direction * speed
        print (position.y)
        if self.position.y == 680:
            queue_free()

The first problem i noticed here is that you are checking for position.y exactly equal to 680 to free the enemies… this may not happen, because perhaps in one frame position is 679 and in the next 681 and you skipped 680, so you are not freeing the enemy. The second problem is that you rely on the 3 leftmost characters of the parents name to get the formation. BUT… as names are unique, when you instance another enemy, if there is a previous one alive, godot will change the name to “@as1…”, so the 3 leftmost characters would be “@as” and you dont get the formation. I solved all of this with this:

func _ready():
    current_formation = get_parent().name
    add_to_group("enemy")
    _process(true)
   pass

func _process(delta):
    #Straight top to bot formation
    if "as1" in current_formation:
        self.position += toptobot_direction * speed
        print (position.y)
        if self.position.y >= 680:
            queue_free()

Notice i changed if self.position.y == 680: by if self.position.y >= 680; also changed current_formation = get_parent().name.left(3) by current_formation = get_parent().name and at last i changed if current_formation == ("as1"): by if "as1 in current_formation:.

Hope it helps!

Yay! I fixed it with your solution! Thank you very much!

congbinh75 | 2019-01-26 08:59