5 Boxes that have a sequence that Player need to hit.

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

Hi, my question is, how can i do 5 boxes that when Player is near he has to press enter in the right sequence , and if he miss it all of boxes close. and he need to restart, how can i do it?

İf box1 pressed ==true :
box2 = true


I guess something like this

ramazan | 2021-11-12 16:12

:bust_in_silhouette: Reply From: Ertain

Try this: place a signal on each of the boxes (maybe something that’s tied to an Area2D, an Area, or other collision nodes) that checks for the proper input from the player when the player is near the box. When the player opens the boxes in the correct sequence, the player is rewarded.

For example, an _on_area_entered() or an _on_box1_area_entered() function is called when the player enters the Area2D of a box. That will then assign true to the corresponding boolean variable which is used to keep track of the boxes and the player’s input. The variables could look like this:

# These could also be contained in an array.
var player_is_near_box1: bool = false
var player_is_near_box2: bool = false
var player_is_near_box3: bool = false
var player_is_near_box4: bool = false
var player_is_near_box5: bool = false

# Keep track of which box is open. Again, these could be contained in an array.
var box1_open: bool = false
var box2_open: bool = false
var box3_open: bool = false
var box4_open: bool = false
var box5_open: bool = false

# This array keeps track of the sequence in which the boxes were opened. As each box is opened, the box number is appended to this array.
var sequence_of_boxes: array = []
# The sequence in which the boxes are *suppose* to be opened.
var correct_sequence: array = [1,2,3,4,5]

When the player is near a certain box, these will be assigned the proper value and used in the input check for the player:

# Each box can have their own function. A general function can be written, but checks for each box would have to be included.
func _on_box1_area_entered(Area2D area) -> void:
    player_is_near_box1 = true
func _on_box1_area_exited() -> void:
    player_is_near_box1 =  false

# Check for the player's input.
func _input(event):
    # Replace "ui_enter" with whatever corresponds to pressing the "enter" key.
    if event is InputEvent and event.is_action_pressed("ui_enter"):
        match true:
            player_is_near_box1:
                if not box1_open:
                    box1_open = true
                    sequence_of_boxes.append(1)
                    _check_box_sequence()
            player_is_near_box2:
                if not box2_open:
                    box2_open = true
                    sequence_of_boxes.append(2)
                    _check_box_sequence()
            player_is_near_box3:
                if not box3_open:
                    box3_open = true
                    sequence_of_boxes.append(3)
                    _check_box_sequence()
            player_is_near_box4:
                if not box4_open:
                    box4_open = true
                    sequence_of_boxes.append(4)
                    _check_box_sequence()
            player_is_near_box5:
                if not box5_open:
                    box5_open = true
                    sequence_of_boxes.append(5)
                    _check_box_sequence()

Each time a box is opened, the sequence is checked. If the sequence is incorrect, all the boxes are closed, and the player has to restart.

# Check the sequence in which the player opened the boxes. If everything is correct, reward the player. Otherwise, close all the boxes and reset the variables.
func _check_box_sequence() -> void:
    if sequence_of_boxes.size() == correct_sequence.size():
        # The two arrays are the same, and so the player has opened the boxes in the correct order.
        if sequence_of_boxes == correct_sequence:
            # Reward the player here.
        else:
            # The player has incorrectly opened up the boxes. Close the boxes and reset the variables.
            box1_open = false
            box2_open = false
            box3_open = false
            box4_open = false
            box5_open = false
            sequence_of_boxes = []
            # "Box closing code" goes here.

I haven’t tested this code, but I hope it gets you on the right track.