How can I sync two scripts?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By genete
:warning: Old Version Published before Godot 3 was released.

I want that two scripts of two different nodes executes one conditional action at one fixed interval synchronized.

Also I want that any of both can be activated or not to perform the action. But when it is activated it must be in sync wit the other.

This is the code I have:

extends Node2D

const INTERVAL=0.25
var time=0
var check=true
var active=true

func _ready():
	set_fixed_process(true)
	activate(false)
	pass

func _fixed_process(delta):
	time=time+delta
	if not active: return
	var rest=int(time/INTERVAL)
	if(rest%2 and check):
		var zero=get_child(0)
		var one=get_child(1)
		var two=get_child(2)
		if zero.is_hidden(): zero.show()
		else: zero.hide()
		if one.is_hidden(): one.show()
		else: one.hide()
		if two.is_hidden(): two.show()
		else: two.hide()
		check=false
	if(not rest%2):
		check=true



func activate(a):
	active=a
	get_child(0).show()
	get_child(1).hide()
	get_child(2).hide()

But doesn’t work. Once one of them is de-activated and re-activated again it gets out of sync.

:bust_in_silhouette: Reply From: genete

Finally I figured it out:
I need two Timers: TimerOn and TimerOff. They are configured to run one shot and both have signals to start each other:

# TimerOn
extends Timer

func _ready():
	pass

func _on_TimerOff_timeout():
	start()

# TimerOff
extends Timer

func _ready():
	pass

func _on_TimerOn_timeout():
	start()

Then each node that performs the actions is connected to the timeout() signal in this way:

extends Node2D

var active

func _ready():
	activate(true)
	pass


func activate(a):
	active=a
	get_child(0).show()
	get_child(1).hide()
	get_child(2).hide()


func _on_TimerOn_timeout():
	if not active: return
	get_child(0).show()
	get_child(1).hide()
	get_child(2).hide()


func _on_TimerOff_timeout():
	if not active: return
	get_child(0).hide()
	get_child(1).show()
	get_child(2).show()

With this pair of Timers it is always sure that the on and off triggers are synchronized.