Enemy disappear between Scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rayu
func spawn_Player1(num):
    for i1 in range(num):
        var p1 = player1.instance()
        p1.connect("Player1_pressed", self, "_on_Player1_pressed")

func _on_Player1_pressed():
    if Globals.timeLeft > 0:
        Globals.timeLeft += 5
        if Globals.timeLeft > Globals.maxTime:
            Globals.timeLeft = Globals.maxTime
    Globals.Score += 1
    yield(get_tree().create_timer(2.0), "timeout") 

func spawn_Enemy1():
    var rand1 = floor(rand_range(0, Enemy1.size()))
    var piece1 = Enemy1[rand1].instance()

On Player Pressed, the Scene waits 2 seconds before it’s changing Scene. I would like to add a function where the Enemy is no more Visible in those 2 seconds.

:bust_in_silhouette: Reply From: AndyCampbell

Do you want EXISTING enemies to disappear for 2 seconds and then appear again?
In this case, you can loop through all enemies and set the “visible” property to false, wait 2 seconds, then set it true again 2 seconds later. You could do that before and after your change_scene call. To do this, I would probably add a hide() function on your enemy. You can call that when you loop over each enemy. That function will set the enemy visible to false, then start a 2 second time which will call a function to set visible to true again.


Do you want to delay spawning NEW enemies for 2 seconds?
For that case, in the function that adds your enemy to the tree, just use another yield delay like you used before
yield(get_tree().create_timer(2.0), “timeout”)

Enemy Script:

func _ready() -> void:
	get_node("Node2D").connect("Player1_pressed", self, "_on_Player1_pressed")

func _on_Player1_pressed():
	yield(get_tree().create_timer(2.0), "timeout")

What am i doing wrong?

Rayu | 2020-11-17 14:48

Try this

Put this in your enemy script

func hide_for_time(t):
	visible = false
	yield(get_tree().create_timer(t), "timeout")
	visible = true

Put this in your existing function

func _on_Player1_pressed():

That assumes Enemy1sprint is a child of the node where this script is running. If that is not true you need to change the NodePath in get_node to the correct path.

But remember you want to do this for all the Enemy
One option is put them all in a Group, then call a hide function on every Enemy.
Details of Groups here

Or, probably better you can use a signal for this. You could create a “level_completed” signal. The Main script could react to that by changing the level, and each Enemy could react to that by hiding for 2 seconds. You can watch a good tutorial about signals here

AndyCampbell | 2020-11-17 15:45

Enemy2 Script: (because if Player1 is pressed 2,3,4 should disappear)

signal level_complete1

func _ready() -> void:
	$Enemy2sprite.connect("Player1_pressed", self, "_on_Player1_pressed")
# I've connected the Player1_pressed signal in this Script again, it is also connected in a different function in the main script. Can you use this signal in more than 1 func? it should be right?
func hide_for_time(t):
	visible = false
	yield(get_tree().create_timer(t), "timeout")
	visible = true
func _on_Player1_pressed():

then the signal goes to the Main Script: (right?)

var Enemy2lvl = preload("res://Enemy/Enemy2.tscn")

func _on_wanted1_pressed():
	Enemy2lvl.connect("level_complete1", self, "_on_level_complete1")

func _on_level_complete1():

#but the Enemy2sprite is not in the Main Scene, it’s in the Enemy Scene, but i used a signal shouldn’t it work?
i have also tried this:

func _on_level_complete1():

Rayu | 2020-11-18 07:18