0 votes

So I'm trying to make a powerup that makes you have a shield around you. I managed to make it so that when it comes into contact with the powerup, the shield sprite is visible.

Now I'm struggling with making the shield sprite dissapear. I tried this with timer nodes and with what the answer of this post said: https://godotengine.org/qa/44603/using-yield-get_tree-create_timer-x.

I also followed this tutorial: https://duckduckgo.com/?q=space+shooter+tut+powerup+godot&t=ffab&iar=videos&iax=videos&ia=videos&iai=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DlafVipHPn4o but that didn't work either.

both scripts:

extends Area2D

var hp = 3

var PowerupTime = 2

var shieldT = Timer.new()

func _ready() -> void:
    shieldT.set_wait_time(2)
    shieldT.set_one_shot(true)
    self.add_child(shieldT)

func _on_Area2D_area_entered(area: Area2D) -> void:
    if area.collision_layer == 2:
        hp -= 1

func _process(delta: float) -> void:
    if hp <= 0:
        get_parent().queue_free()
        queue_free()

    if global.powerup1_active == true:
        shieldT.start()
        if !shieldT.is_stopped():
            get_parent().get_node("Shield").visible = true
            print(shieldT.time_left)
        else:
            get_parent().get_node("Shield").visible = false
            global.powerup1_active = false

        print(global.powerup1_active)

extends Area2D

onready var shieldTimer := get_parent().get_node("ShieldTimer")

var hp = 3
var shieldTime := 1

func _ready() -> void:
    get_parent().get_node("Shield").visible = false

func _on_Area2D_area_entered(area: Area2D) -> void:
    if area.collision_layer == 2:
        hp -= 1

func _process(delta: float) -> void:
    if hp <= 0:
        get_parent().queue_free()
        queue_free()

    if global.powerup1_active == true:
        shieldTimer.start(shieldTime)
        if !shieldTimer.is_stopped():
            get_parent().get_node("Shield").visible = true
            print(shieldTimer.time_left)
        else:
            global.powerup1_active = false

P.S I'm quite new to GDscript and Godot so I probably missed out something very important. Thanks :)

Godot version v3.4.1
in Engine by (99 points)
edited by

Can you add just the timer part of the code?

i added them a bit from memory, so it might not be exact

i added them a bit from memory, so it might not be exact

It's not possible for people to look at code and make useful suggestions if it's not the code you're actually using.

well, from what i have shown, please give me some suggestions and ideas and why my code isn't working?

1 Answer

+1 vote
Best answer

It is You just start your timer endlessly in process(). You should have noticed that, because your print statement must always show starting time in console. Your timer is never stopped, always running, so it never reaches this part of the code, that turns shield to invisible.

You already used boolean "powerupactive" to border if statements in process. You can reposition it, so the timer is only started once, visibility set to true or false also just once. Of course You should use "timeout" signal or setget for the most ellegant sollution, but whatever works is fine

by (5,557 points)
selected by

thank you so much! <3

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.