What's the proper was to use a String and/or StringName with emit_signal()?

Godot Version

4.2.1

Question

I have a method which is meant to fetch a String from a dictionary. This is called inside of the emit_signal() method and ideally should allow emit_signal to send various signals stored in the dictionary. It didn’t work as a standard string and I saw also that it may need to be a StringName to work, but so far no luck there either. I’ve confirmed that my own method called get_upgrade_signal_by_id() is returning the proper values, but I’ve had no luck getting the emit signal to do what I’m expecting.

It looks like this:
Events.emit_signal(StringName(get_upgrade_signal_by_id(upgrade_id)))

What am I missing here?

Is Events an autoload? Do the signals exist in the Events class? are the signals connected somewhere?

Yes, Events is an autoload and the signals are in Events. If I call them directly without the method, there’s no issue and they work as expected.

So, let’s say you have a tool_upgraded as a signal in Events.

What you are saying is that Events.emit_signal("tool_upgraded") works fine but if you return it in get_upgrade_signal_by_id() it does not work?

Try doing something like:

var signal_name = get_upgrade_by_id(...)
var result = Events.emit_signal(signal_name)
print("Signal %s emitted: %s" % [signal_name, error_string(result)])

And post what error it gives you

This is what I get:

signal name: item_purchased
Signal item_purchased emitted: OK

UPDATE:

This works for emitting the signal. I found I had another problem that was unrelated that was preventing things from working properly on the Events script. Thanks for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.