How to delete all instances of Node?

PendiPendi Posts: 9Member

Hey,

So I just started learning this engine and I tried out the "Your First Game" tutorial available on the Godot website, anyway, I've finished what they have covered in the tutorial, and what I notice right now is that when I get sent to the game over screen, the instances of my Mob still remain. Is there anyway I can queue_free all of the mobs that were instanced?

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    You could try something like this (untested):

    var main = get_node("PATH_TO_MAIN")
    for child in main.get_children():
        # This works because _on_Visibility_screen_exited is a function only mobs have.
        if (child.has_method("_on_Visibility_screen_exited")):
            child.queue_free();

    You'd just need to add whenever you want to delete all of the mobs.

  • PendiPendi Posts: 9Member
    edited March 2018

    Okay I have this code whenever the game_over function is called, and it's not working. Any thoughts?

        for child in get_node("Mob").get_children():
                if (child.has_method("_on_VisibilityNotifier2D_screen_exited")):
                    child.queue_free()
    
  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    Which script is this attached to?

    I think the reason it is not working is because you are trying to get the mob node, not the node where all the mobs are spawned to. The node you are adding the mobs to, called Main in the tutorial, is the node you need to get using get_node.

  • PendiPendi Posts: 9Member
    edited March 2018

    It is attached to my main script

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin
    Answer ✓

    Ah, then this should work:

    for child in self.get_children():
     # I'd double check to make sure the function is spelled exactly the same as the function in mob.
        if (child.has_method("_on_VisibilityNotifier2D_screen_exited"):
            child.queue_free()
  • PendiPendi Posts: 9Member

    Thanks a million! I've been stuck on this for hours

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    No problem, happy to help!

  • KraynoKrayno Posts: 1Member

    Thanks, TwistedTwigLeg, your solution also worked for me. So confirmed to work 2/01/2019.

    Couldn't work this one out either haha. I originally tried to $Mob.queue_free() under the function game_over but it only
    seemed to remove one. Copying and pasting lead the game to crash as it couldn't find a node named "Mob".

    Confused, I searched how to print all the nodes by using print_tree() which lead me to discover that they were named @Mob@2, @Mob@3 etc. At that point, I was at a loss. Even if I had a bunch of $Mob.queue_free's, if the node I was looking to free was not there, it would crash the game.

    Thanks Twisted to teaching me about for statements :).

  • woksterwokster Posts: 1Member
    edited January 27

    Another way:

    after created instance of mob

    var mob = Mob.instance()
    add_child(mob)

    we can add it to group
    mob.add_to_group("enemies")

    and after it in game_over:

    var enemies = get_tree().get_nodes_in_group("enemies")
        for child in enemies:
            child.queue_free()

    I think it will be more optimal, because you do not need to do a method check. I'm right? I am a novice.

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    @Krayno said:
    Thanks, TwistedTwigLeg, your solution also worked for me. So confirmed to work 2/01/2019.

    Couldn't work this one out either haha. I originally tried to $Mob.queue_free() under the function game_over but it only
    seemed to remove one. Copying and pasting lead the game to crash as it couldn't find a node named "Mob".

    Confused, I searched how to print all the nodes by using print_tree() which lead me to discover that they were named @Mob@2, @Mob@3 etc. At that point, I was at a loss. Even if I had a bunch of $Mob.queue_free's, if the node I was looking to free was not there, it would crash the game.

    Thanks Twisted to teaching me about for statements :).

    Somehow I totally missed this reply, whoops! You are welcome @Krayno, I'm glad it was helpful :smile:

    @wokster said:
    Another way:

    after created instance of mob

    var mob = Mob.instance()
    add_child(mob)

    we can add it to group
    mob.add_to_group("enemies")

    and after it in game_over:

    var enemies = get_tree().get_nodes_in_group("enemies")
      for child in enemies:
          child.queue_free()

    I think it will be more optimal, because you do not need to do a method check. I'm right? I am a novice.

    Yeah, I think that would be better for performance because not only are you not checking for a method in every iteration of the loop, but also you are not looping through every child in main but rather just the nodes you have added to the group.

    So yeah, I think it would be faster, and as a bonus you will be able to get the enemy nodes wherever they are in the scene tree so long as they are added to the group. :smile:

Sign In or Register to comment.