The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes
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.

in Engine by (137 points)

1 Answer

+1 vote
Best answer

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(gettree().createtimer(2.0), "timeout")

by (658 points)
selected by

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?

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

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():
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.