Getting signal sender and/or parameters in c++

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

Hi all,
how do i get the sender and/or the parameters of a signal in c++?

does the receiving funciton need a special signature?
void eventhandler(void* sender, …)

tnx, in advance.

:bust_in_silhouette: Reply From: Zylann

In C++ or GDScript, there is no sender argument by default when you handle a signal. If you need it, one way is to send it as one of the arguments.
If you can’t, you may use the optional “binds” parameter when you connect to the signal, so they will be passed to the signal handler when the signal is emitted.

To listen for a signal in C++, you have to register a function with the reflection API and then connect it by name, the same way as GDScript:

// Normal connect
obj.connect("signal_name", this, "method_name");

// Connect with extra binds which will be passed as extra parameters
obj.connect("signal_name", this, "method_name", Node::make_binds(obj));

hi tnx for answer,
but that part i knew :slight_smile:

i meant how the “consumer” (which is a c++ function) has to look like
or how i acess the parameters inside my function.

using your example:

is it: void method_name**(Node* obj)**?
or do i have to use a void* and cast it inside?
or anything else? :slight_smile:

Charles Woodhill | 2017-03-15 23:00

Because signals work through the same system GDScript uses to call C++ functions, they just have to have arguments supported by Variant.

If what you want to receive is a Node and you don’t store it, you can probably have a Node & node parameter, because it inherits Object and Object is supported by Variant.

Just so you know, I say not to store it because nodes don’t inherit Reference, so if a script were to destroy the node, you could end up with a dead pointer to it later on. So you can alternatively accept (or retrieve) a NodePath if you want to keep a reference to it after the signal is handled.

Zylann | 2017-03-16 02:04

works like a charm!
tnx for help :slight_smile:

Charles Woodhill | 2017-03-16 10:36