Remove the last item in a group?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster

Hi all,

I have a heap of HUD items showing lives, ammunition etc. I have my bullets all shown and I want to remove the last ones in the group.

They are in group called HUDbullets. How can I find one to remove it in code? Thank you.

(Godot 3.0)

:bust_in_silhouette: Reply From: DodoIta

SceneTree.get_nodes_in_group("group_name") returns an array containing the nodes in a group. So you could assign it to a variable, like such

var nodes = get_tree().get_nodes_in_group("HUDbullets")

and then use the Array methods to pick the right ones.

Hey thanks for that. I have been digging and have:

var results = []
	for node in get_tree().get_nodes_in_group("HUDBullets"):
		results.append(node)

I guess I now need to find the last in an array and then queue_free() it. I had a read at Array — Godot Engine (3.0) documentation in English I found back() and gave it a whirl like so:

#create array
var results = []
#stuff the bullets into the array
for node in get_tree().get_nodes_in_group("HUDBullets"):
	results.append(node)
#delete the last bullet in the array
if results.back():
	results.back().queue_free()

So hopefully that helps someone in the future and thanks so much to /u/Dodolta It’s really appreciated.

Robster | 2018-02-22 23:22

You’re welcome.
Good luck with your game.

DodoIta | 2018-02-23 08:23