Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.
goal.gd
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):
sequenceCheck();
func sequenceCheck():
match sequences.has(Global.currentSequenceIndex):
false:
sprite.visible = false;
area.monitoring = false;
true:
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
else:
Global.currentSequenceIndex += 1;
global.gd
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!