+3 votes

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
in Engine by (65 points)
edited by

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?

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

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

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

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

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.

1 Answer

+2 votes
Best answer

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!

by (3,400 points)
selected by

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

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.