GDNative: How do I register a signal and emit it within c++, and then used yield to await for this signal in gdscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hidemat

I have Example class that inherits from Reference, and this is the _register_methods() method:

void Example::_register_methods()
{
    // register other methods including open()

	register_signal<Example>((char*)"opened");
}

Then within my code I call the emit_signal method:

void Example::open()
{
	run_other_code();

	emit_signal("opened");
}

Then in GDscript I initilize my class and call the open method:

func _ready()"
    var example = Example.new()
    example.open()

    yield(example, "opened")

    #some other code

EDIT: I was able to connect to the signal like this:

example.connect("open", self, "_on_Example_open")

So I guess there is a problem with calling the yield keyword this way?

Not sure what your overall goal is, but this seems like a weird use case for a signal.

Signals are for communicating between nodes/scenes that don’t really own each other but need to occasionally communicate anyway; in this case you are trying to make an object locally, have that object send a signal, then catch that signal right after that when you could do the same thing by just calling the method on that local variable.

Again, not sure what your goals are but I would not worry about this specific issue unless the use case really warrants it

w411-3 | 2022-03-09 02:31

Hello I need your help.
I don’t understand.
I want to send a signal in C++ and get it in GDNative to play an animation.
Do you think you can help me ?

Rem | 2022-12-16 10:50

You’ll need to be a whole lot more specific for anyone here to help you. All I can say immediately is it sounds like a weird design that would lead you to want to send a signal between C++ modules and GDNative just to play an animation.

Keep thinking on it and do ask your own new question if you think someone can help; just make sure you give enough detail

w411-3 | 2022-12-17 02:28

:bust_in_silhouette: Reply From: r.bailey

Same as the comment written above, I am not sure what you are trying to achieve.
Is the signal you are connecting to the proper class and do you have the method that the signal connects to exist to do anything ? Example of what I mean below.

func _ready()
var example = Example.new()
example.connect("open", self, "_on_Example_open")
example.open()    
yield(example, "open")
#some other code

func _on_example_open() -> void:
#when the signal gets sets back to this class it will run whatever code you have here

Also if you are trying to time something to be done once you open it, you can pass variables in the signal code and that gets received by the class and set it to do something instead of yield. For example below.

void Example::_register_methods()
{
    // register other methods including open()

    register_signal<Example>((char*)"open", "booleanValue", GODOT_VARIANT_TYPE_BOOL);
}
 void Example::open()
{
    run_other_code();
    bool finished = true; 
    emit_signal("open",finished);
}

In the gd class declare the boolean variable.

   var finished

func _ready()
    var example = Example.new()
    example.connect("open", self, "_on_Example_open")
    example.open()    
    #some other code
func _on_example_open(valuein) -> void:
  finished = valuein 
   func _process(delta: float) -> void:
	if (finished) 
		DoSomething() 
            finished = false
  
  func DoSomething -> void: 
    #do something when the signal comes in that the open is finished. 

Hope this helps in your journey

Yeah this helps me out. Sorry I was a bit unclear. The signal should have been named opened. Basically, when I call example.open() I’m doing some initialization of the object, and I have to wait until that is done before I keep going, which is why I was trying with yield. But the ways you mentioned seem viable to me so, I’ll try that. Thanks.

hidemat | 2022-03-10 19:22

You’re welcome, good to hear.

r.bailey | 2022-03-11 17:46