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 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.

in Engine by (692 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.

by (692 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.