I would make your problem a little easier by changing your Train Cart
node into a Train
node that has 3 Train Cart
nodes as children of it. Then in your Train Cart
nodes do something like this:
# Train_Cart.gd
extends Area2D
signal cart_changed
var correct_color = 'blue' # or however you're keeping track of the colors
var has_correct_cat = false
func _ready():
self.connect("body_entered", self, "_on_Area2D_body_entered")
self.connect("body_exited", self, "_on_Area2D_body_exited")
func _on_Area2D_body_entered(body):
if body.color == correct_color:
has_correct_cat = true
emit_signal("cart_changed")
func _on_Area2D_body_exited(body):
has_correct_cat = false
emit_signal("cart_changed")
Then the rest of the logic is handled in your singular parent Train
node:
# train.gd
extends Node2D # or whatever you think it should be
func _ready():
for train_cart in get_children():
train_cart.connect("cart_changed", self, "_on_cart_changed")
func _on_cart_changed():
for train_cart in get_children():
if not train_cart.has_correct_cat:
return # if one cart doesn't have the correct cat just drop out of the function
# if we got here, all carts have the correct cat
# so place the logic to move your train here