0 votes

Hi! I'm making a space invasion type of game, and in it i have an enemy spawner and a pause button/system. The pause system works, but it only pauses 2D physics and input events, which doesnt stop the enemies from spawning.
This is my spawner script:

func _ready():
var rand = RandomNumberGenerator.new()
var enemyalien = load("res://mobs/alienyellow.tscn")
var screen_size = get_viewport().get_visible_rect().size
var time = true
var tic = 1
for i in range(0, 100000):
    var alien = enemyalien.instance()
    var x = rand.randf_range(0+16, screen_size.x-16)
    var y = -39
    alien.position.x = x
    alien.position.y = y
    time = false
    yield(get_tree().create_timer(rand_range(1, 2)), "timeout")
    time = true

As you can see,it's in the " _ready(): " function

This is the pause button script:

func _input(event):
if Input.is_key_pressed(KEY_P):
    $Background.visible = !$Background.visible
    get_tree().paused = !get_tree().paused

My question is, when the P button is pressed,how can i stop the enemies from spawning as well? I hope that i gave enough information and that i explained the problem successfully!

Godot version Godot Engine v3.3.2.stable.official
in Engine by (12 points)

2 Answers

0 votes

Did you try changing the pause mode for the node that has the spawning script to Inherit or Stop?

by (14 points)

Yeah,i just tried that on multiple spawners,and it didnt work on any of them.

0 votes

you could make a seperate function that does the spawning and put the function inside ready. then declare a variable under extends and call it ispaused = false then put a condition inside your spawn function that only runs while ispaused == false then on your input event modify is_paused = true

var is_paused = false

func _ready():

func spawn():
if is_paused == false:
your spawn method

func _input(event):
if Input.is_key_pressed(KEY_P):
enemy_spawn.is_paused = true
by (21 points)

something like that should work and i think you should be using event.iskeypressed over Input i feel that is doubling up on what you are doing i could be wrong there but either way if you are not calling event you should put an underscore in front of it _event

i think as well you can put

enemy_spawn.is_paused = not enemy.is_paused

and this should flip the condition so should work to unpause i believe but i haven't tested this

Well,my code is more organized after i tried this,it didnt work,but still,thank you for your answer.
I'll probably think of a completely new way to spawn the enemies.

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 Frequently asked questions and 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 [email protected] with your username.