private void RemoveAllPlayers() { // Removes ALL the entries from list of player node names due to the method's purpose. this.listPlayerNodeNames.Clear(); // Create a regex string string regexString = String.Format("@?({0})@?.*", "Player"); // Get a list of player node names List <string> playerNodeNames = new List <string>(); foreach (Node2D node in this.GetChildren()) { // For each node within the current node's (world node) children... if (Regex.IsMatch(node.Name, regexString)) { // Add the player node names onto the list if the regex string matches it. playerNodeNames.Add(node.Name); } } if (playerNodeNames.Count > 0) { // If the node name list is not empty, then... foreach (string s in playerNodeNames) { // Get all the nodes from the pertaining to // those names and remove them from the game. KinematicBody2D instance = this.GetNode <KinematicBody2D>(s); instance.Hide(); instance.QueueFree(); } } }