0 votes

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.

asked May 12, 2016 in Engine by genete (674 points)

1 Answer

0 votes

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.

answered May 12, 2016 by genete (674 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 How to use this Q&A? before posting your first questions.