How to make the code simpler for checking the string for the right answer?

Godot Version



Hey guys,

Im new to the godot so excuse my ignorance.

I am trying to create the game where player has to input letters and build a word.

input fields for specific letters are connected to each other. So if user types A in input field other fields are also automatically typed

I also need to check if the user inputed correct letter.

I wrote this code but as you can see it is extremely long and I will need to recreate it for every level which will become almost impossible at some point.

Can anyone help me out, direct me to some documentation or anything really? I’ve been stuck here for days.

Im sorry again and thank you all!

here is the code:

extends CanvasLayer

# Declare variables to store references to the LineEdit nodes for the first group
@onready var input1_group1: LineEdit = $one1
@onready var input2_group1: LineEdit = $one2
@onready var input3_group1: LineEdit = $one3

# Declare variables to store references to the LineEdit nodes for the second group
@onready var input1_group2: LineEdit = $one4
@onready var input2_group2: LineEdit = $one5
@onready var input3_group2: LineEdit = $one6

# Declare variables to store references to the LineEdit nodes for the third group
@onready var input1_group3: LineEdit = $one7
@onready var input2_group3: LineEdit = $one8
@onready var input3_group3: LineEdit = $one9

# Variables to track if conditions are met for each group
var condition_group1_met = false
var condition_group2_met = false
var condition_group3_met = false

func _ready():
    # Connect the first group of input fields
    input1_group1.connect("text_changed", _on_input_group1_changed)
    input2_group1.connect("text_changed", _on_input_group1_changed)
    input3_group1.connect("text_changed", _on_input_group1_changed)
    # Connect the second group of input fields
    input1_group2.connect("text_changed", _on_input_group2_changed)
    input2_group2.connect("text_changed", _on_input_group2_changed)
    input3_group2.connect("text_changed", _on_input_group2_changed)

    # Connect the third group of input fields
    input1_group3.connect("text_changed", _on_input_group3_changed)
    input2_group3.connect("text_changed", _on_input_group3_changed)
    input3_group3.connect("text_changed", _on_input_group3_changed)

func _on_input_group1_changed(new_text: String):
    input2_group1.text = new_text
    input3_group1.text = new_text
    if new_text == "A":
        condition_group1_met = true
        condition_group1_met = false

func _on_input_group2_changed(new_text: String):
    input1_group2.text = new_text
    input3_group2.text = new_text
    if new_text == "B":
        condition_group2_met = true
        condition_group2_met = false

func _on_input_group3_changed(new_text: String):
    input1_group3.text = new_text
    input2_group3.text = new_text
    if new_text == "C":
        condition_group3_met = true
        condition_group3_met = false

func check_conditions():
    if condition_group1_met && condition_group2_met && condition_group3_met:
        print("Good Job for All Groups")

this is for godot 4 but should work i believe.

extends CanvasLayer

# Declare arrays to store references to the LineEdit nodes for each group
var input_groups = []

# Variables to track if conditions are met for each group
var condition_groups_met = []

func _ready():
    # Populate the arrays with LineEdit nodes and set up connections
    for i in range(1, 4):
        var inputs = []
        for j in range(1, 4):
            var input = find_node("one" + str(j) + str(i))
            input.connect("text_changed", self, "_on_input_changed", [i - 1])

func _on_input_changed(new_text: String, group_index: int):
    # Update other LineEdit nodes in the same group
    for i in range(3):
        if i != group_index:
            input_groups[group_index][i].text = new_text
    # Check if condition is met for this group
    condition_groups_met[group_index] = (new_text == ["A", "B", "C"][group_index])

func check_conditions():
    if condition_groups_met[0] && condition_groups_met[1] && condition_groups_met[2]:
        print("Good Job for All Groups")