This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I am working on Tic-Tac-Toe logic right now and I have my "victory lines" grouped together in Godot. I am trying to check if a variable, same name in all objects, is the same value. I am essentially using an array as a very simple state machine. The groups are called as arrays at start for the sake of mechanical clarity.

onready var myButtons = get_tree().get_nodes_in_group('all_buttons')
onready var row1 = get_tree().get_nodes_in_group('row_1')
onready var row2 = get_tree().get_nodes_in_group('row_2')
onready var row3 = get_tree().get_nodes_in_group('row_3')
onready var column1 = get_tree().get_nodes_in_group('column_1')
onready var column2 = get_tree().get_nodes_in_group('column_2')
onready var column3 = get_tree().get_nodes_in_group('column_3')
onready var across1 = get_tree().get_nodes_in_group('across_1')
onready var across2 = get_tree().get_nodes_in_group('across_2')

Each button node has a code like this:

var stateMachine = ["EMPTY", "HAS_X", "HAS_O"]
var state = stateMachine[0]

So my question:
How would I check this local "state" variable throughout the entire group at once with a method? Obviously I will check it in the Main Scene, I just need to know what process to use.

Godot version v3.4.3.stable.official [242c05d12]
in Engine by (562 points)

1 Answer

+1 vote
for node in get_tree()get_nodes_in_group(X) :
      var state = get_tree()get_nodes_in_group(X)[0]
      if node.state != state :
             return false
return true

Just something I came up now. Starts with first node state, if any iterated node state is different from it - function returns false ( tic tac unmatched ). If everything is the same, loop continues until it reaches return true ( tic tac matched )

by (8,188 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.