i cant delete the first element of the array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mostafa Saeed
func _ready():
	player = get_tree().get_first_node_in_group("player")
	if player != null:
		player.global_position = start.get_spawn_pos()

	#this is the for loop
	var watch = get_tree().get_nodes_in_group("watch")
	for watches in watch:
		watches.connect("more_time" ,_on_watch_more_time )
	var traps = get_tree().get_nodes_in_group("traps")
	for trap in traps:
	exit.connect("body_entered" , _on_body_entered)
	death_zone.connect("body_entered" ,_on_death_zone_body_entered )

# the problem is here i cant put remove_at() while having .size() function
#i want to delete the first node of the array
**func _on_watch_more_time():
	time_left += time_increase
	var watches_kids = watches.get_children().size()

 1. List item
:bust_in_silhouette: Reply From: godot_dev_

This line var watches_kids = watches.get_children().size() assigns an integer to the variable watches_kids. Then you are trying to call the remove_at function of the variable watches_kids (an int). Integers don’t have such a function, hence the error.

According to the documentation, remove_at will remove an element at the given index from an array, so you need to make sure watches_kids is an array for the error to be remove. You may want to careful think about your logic to fix this, since naively making watches_kids an array may not achieve what your goal is. For example, if you want to remove a child node from the watches node, removing an element from an array won’t achieve that.

:bust_in_silhouette: Reply From: HyperlinkYourHeart

size() returns an int, so watches_kids is an int, and you’re trying to “remove an element” from it, which doesn’t make any sense.

If you just don’t include the call to size() it should work, if there are children:

var watches_kids = watches.get_children()

If there is some other reason that you need to know the size, just make that a separate call:

var watches_kids = watches.get_children()
var watches_kids_size = watches_kids.size()

Indeed. However, if all the function does is

func _on_watch_more_time():
    time_left += time_increase
    var watches_kids = watches.get_children()

the removal of the first item in the array actually does nothing, since watches_kids is a local vairable

godot_dev_ | 2023-06-19 17:24

Yes good point, unless OP is doing something with the array that is now minus the first element there is not much point.

If the idea is to remove the first child from the “watches” node then I guess the code would be something like:

func _on_watch_more_time():
    time_left += time_increase
    if watches.get_child_count() > 0:

Or, you could pass the node in the signal or bind it to the signal handler maybe.

HyperlinkYourHeart | 2023-06-19 20:32