Making a bullet disappear when it has been shot/ thrown after a short while.

UnrulyElf10UnrulyElf10 Posts: 37Member

Hi, I code in GDScript.
I am making a game where you throw nuts at enemies, and if you don't hit the enemy the nut will kind of bounce off walls a little and roll around, but they never end up disappearing that way and you get alot of lag as then never disappear. If anyone can help out, it would be appreciated. I'm thinking after instantiating the bullet/nut, it can start a timer and then delete the nut after that timer runs out.
Thanks! :)


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

    I'd go for the timer route, maybe using a variable and delta so you can pause the timer if you need. Something like this is how I'd approach the issue:

    var timer = 0.0
    var timer_active = false
    const TIMER_TIME = 20 # in seconds
    
    func _process(delta):
        if (timer_active):
            timer -= delta
            if (timer <= 0):
                timer_active = false
                queue_free()
    
    func start_timer():
        if (timer_active == true):
            return # don't start the timer twice!
        timer = TIMER_TIME
        timer_active = true
    
  • UnrulyElf10UnrulyElf10 Posts: 37Member

    So I'd put this into the nut's code and when it is instantiated, it will run all of this?

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

    @UnrulyElf10 said:
    So I'd put this into the nut's code and when it is instantiated, it will run all of this?

    Correct, though you'll want to call start_timer in the _ready function if you want the timer to start once the object is instantiated.

  • UnrulyElf10UnrulyElf10 Posts: 37Member

    Thank you so much! This has really helped, the nuts fall quicker, and they then disappear after a short while! I'm just gonna make a nuts breaking animation so that they don't just disappear. Thanks again @TwistedTwigleg !

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin
    edited August 2

    Awesome and happy to help! I'm glad that it worked :smiley:

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file