user2558928
user2558928

Reputation: 299

Is it possible to create a button factory with buttons these emit signal by pressed?

I want to create a button factory that creates buttons by a specific configuration as parameters. The button have to emit a signal if there is pressed. I can't find any method to add that functionality programatically. I know, that i can add buttons by the editor and add that function to it by a script. But with a factory, i don't need to add every button by my self.

Big thanks!

Upvotes: 1

Views: 1422

Answers (1)

Theraot
Theraot

Reputation: 40210

Buttons emit a signal when they are pressed, it is called "pressed". That is not a functionality you need to add.

Perhaps what you need is something else? The Godot editor is built on the same API as your game, so you can do from code virtually anything the editor can do… such as…


You can add a script from code

If you need to add a signal, the usual way is to attach a script that has the signal declared and the logic to emit it.

If you have such script created, you can load or preload it in your code, and then attach the script to the Object you want (with set_script). There is also a get_script method to retrieve it. And a "script_changed".

And yes, you can create scripts from code too. Create a GDScript object, set its source_code, and reload it. Then you can attach it to an Object just as if you had loaded it.

By the way, the script is a Resource which is why you can load it or preload it. See also ResourceLoader. It also means that you can save it with ResourceSaver. However, I remind that resource paths aren't always writable.


You can add signals from code

Yes, you can add dynamically signals from code. I mean custom signals that are not declared in a script, and that could be added at any moment. To do that you call add_user_signal on the Object. You can check with has_user_signal (use has_signal if you don't care if it was added with add_user_signal or not). And no, there is no remove method for these. So use these sparingly and with intent.

By the way, you can get an Array of all signals on an object with get_signal_list.


You can emit signals from code

You do that you can call emit_signal.

And you can call it on other objects too (I mean, you don't have to call emit_signal from an script attached to the Object that has the signal, it can be from a different one).


You can manage connections from code

I also remind you that you can connect a signal with the connect method. See also disconnect and is_connected.

Oh, and you can query the connections with get_incoming_connections and get_signal_connection_list (Here is an example, it also demonstrates get_signal_list). No, you don't need to worry about disconnecting the signals when freeing the object. Godot takes care of that.


And you can block signals too

You can have an object block its signals with set_block_signals, and check with is_blocking_signals. I - personally - haven't used this, never had the necessity. I just mentioning it for completeness sake.

Upvotes: 2

Related Questions