How to use all of the arrays in a group of similar nodes.

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

I’m making a game where a player runs through different checkpoints around a level and I am trying to make a system where each instance of a goal has its own places in the sequence of goals that show up. The goal script has an array called sequences which has the different parts of the sequence it shows up in, starting at 0, and counting up wards. Its an array so that way I can give multiple spots in a sequence to the same goal. Each goal instance is also in the goals group.

The arrays are then compared to a currentSequenceIndex variable in a separate global script, to know when to hide their sprite and disable their Area2D’s so that way the ones that aren’t currently used can’t be interacted with, and then re-enable everything once their place in the sequence comes up.

The currentSeqenceIndex variable counts up by one when the player interacts with the currently enabled goal’s Area2D. I also want to dynamically set a maximum ammount for the sequence called maximumSequenceIndex based off of the highest sequence number out of all of the goals in the goals group. This is so that way the currentSequenceIndex variable resets back to 0 if it hits the maximum.

Is there a way to get each entry of each array in the goals group, compile it into one single array, and then set the max as the maximumSequenceIndex variable?

Here is the code for the goals and the global script respectively:
The game crashes on startup if you try and run the code.

extends Node2D

//Note: Other instances of the Goal have different values for this, such as [1,3] and [2]. [0] is the default and is the first one in the sequence of goals
export var sequences = [0];

onready var sprite = $Sprite;
onready var area = $Area2D;

func _process(delta):

func sequenceCheck():
	match sequences.has(Global.currentSequenceIndex):
			sprite.visible = false;
			area.monitoring = false;
			sprite.visible = true;
			area.monitoring = true;

func _on_Area2D_body_entered(body):
	if body.is_in_group("player"):
		if Global.currentSequenceIndex == Global.maximumSequenceIndex:
			Global.currentSequenceIndex = 0
			Global.currentSequenceIndex += 1;

extends Node

var goals = get_tree().get_nodes_in_group("goals");
var maximumSequenceIndex = goals.sequences.max()

export var currentSequenceIndex = 0;

Any help will be highly appreciated!

:bust_in_silhouette: Reply From: Inces

Game crashes because Your global script tries to call tree. If I understood correctly, it is autoload or resource, so it can’t have access to the tree(). You surely get corresponding error in console. The very next line of code would also provoke crash - nonexistent property “sequences” in GDScript array

So You want to only find highest element among all arrays ?
You can do :

    var highest = 0
    for node in get_tree().get_nodes_in_group("goals") :
            var top = node.sequences.max()
            if top > highest :
                   highest = top
    return highest

However I don’t understand what do You want to organize in another array ?

Also a tip. You propably don’t want to make these checks in process(). Learn about setget keyword.