0 votes

Hi, I'm looking fo a way to signal bunch of objcts (like elevators) to tern on when some specific button will be pressed. But when it's activated signal is constantly emiting, is there any way to send this signal only when variable is changed?

Something like oneshoot when node is connecting but I need to recive this signal whenever it's changed.

I have found some method using set/get but in my situation everything needs to be inside of the class, my variables are changing internally.

Best!

in Engine by (227 points)

2 Answers

0 votes

Maybe your class should emit the signal when a variable has changed. And yes setters would be the right place for that.

by (719 points)
+1 vote

The Godot emit_signal() only fires once per call. So the idea will be to design your code so that it only fires when the variable is updated.

This design fires a signal called OnlyOnce when 4 seconds has passed

extends Control

    #+/- seconds
    var CustomTimer = 4

    signal OnlyOnce

    func _process(delta):
        #make the code for the timer
        if CustomTimer > 0:
            CustomTimer -= delta
        else:
            #Now we fire the signal only once
            emit_signal("OnlyOnce")
            #ResetTimer
            CustomTimer = 4

The other receiving object will have a link to the signal that looks like this:

extends Label

var SignalCounter = 0

func _on_Control_OnlyOnce():
    SignalCounter += 1
    self.text = str(SignalCounter)

This text will now update every 4 seconds.

We could also have avoided signals:

extends Label

var FireCounter = 0

func UpdateText():
    FireCounter += 1
    self.text = str(FireCounter)

This is very similar to the signal, it is a function that any object can call.

Then in the controller we call this function directly:

extends Control

    #+/- seconds
    var CustomTimer = 4

    func _process(delta):
        #make the code for the timer
        if CustomTimer > 0:
            CustomTimer -= delta
        else:
            #Instead of a signal we call the function in the other object
            $Label.UpdateText()
            #ResetTimer
            CustomTimer = 4
by (1,445 points)
edited by
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.
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 webmaster@godotengine.org with your username.