How to pause a "func _ready():"

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

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 =
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!

:bust_in_silhouette: Reply From: Jelo

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

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

codaholic | 2021-07-05 01:29

:bust_in_silhouette: Reply From: sir306

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 is_paused = false then put a condition inside your spawn function that only runs while is_paused == 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

something like that should work and i think you should be using event.is_key_pressed 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

sir306 | 2021-07-05 05:22

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

sir306 | 2021-07-05 05:32

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.

codaholic | 2021-07-06 11:37