User signals with arguments

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

Hi friends, I am trying since yesterday making some code work that is dependent on a custom signal.I am having some problems making it work, maybe it is the way my setup works.

This is the signal:

add_user_signal("go_to_next_text",[{"name": "current_chapter", "type": TYPE_STRING},{"name": "current_dialog", "type": TYPE_STRING}, {"name": "current_text_finalized", "type": TYPE_INT},{"name": "total_texts", "type": TYPE_INT}])

Then, on the needed part, I connect it, together with the needed arguments that are:

connect("go_to_next_text", self,"go_to_next_text", [chapter, dialog, start_at, dialog_array.size()])

The mentioned function go_to_next_text:

func go_to_next_text(chapter, dialog, start_at, total_text):
	print("FUNC STARTED!", chapter, dialog, start_at, total_text) # testing purpose
	var accept = Input.is_action_pressed("ui_accept")
	while not accept:
		if accept:
			emit_signal("go_to_next_text",chapter, dialog, start_at, total_text)

but it looks like the function is never called as I don’t get even the print “FUNC STARTED”

I have already read the documentation about signals and even the reddit posts relevant to the theme… but I wasn’t able to figure it out what I am doing wrong

Also, I know this is another kind of doubt, I can create another question, but since I am on the theme, is it possible to simplify this process by making a yield(object, "go_to_next_text", array_with_arguments) and pass the arguments directly on the yield somehow?

Can you make a sample project for it?

volzhs | 2016-08-08 06:37

I dont know add_user_signal method, but if i do simple

extends Node
signal go_to_next_text(chapter, dialog, start_at, total_text)

func _goto(chapter, dialog, start_at, total_text):
	print("gotcha!")

func _ready():
	self.connect("go_to_next_text", self, "_goto")
	
	emit_signal("go_to_next_text", null, null, null, null)

it works as expected (print out “gotcha!” :slight_smile: )

splite | 2016-08-09 10:18

Btw,

var accept = Input.is_action_pressed("ui_accept")
while not accept:
        if accept:
            emit_signal("go_to_next_text",chapter, dialog, start_at, total_text)

really? :slight_smile:

# let accept be false, so:
while !false # = true, ok, go to cycle
    if false # do nothing
        emit # never called
    # stay forever in loop (accept never change)
_activate_doomsday_device() # dont worry :)

or

# let accept be true, so:
while !true # = false, do nothing
    if false # never called
        emit # never called
    _activate_doomsday_device() # dont worry :)

edit: minor formatting issues
edit2: added silly joke with doomsday device :))

splite | 2016-08-09 10:57

Hey friend, thanks for replying.

I got the help of one of the devs and looked like I was trying to get blood from a rock

The signal is kinda like a shortcut to add_user_signal, other than that, they work the same.

I solved my problem by making global variables that could be changed based on the results of other functions…
Not the most elegant sollution, but it is now working flawlessly.

It was the last thing I was trying to correct/tweak before releasing my plugin to the public

SMRT-Godot

brunosxs | 2016-08-13 01:59

Looks sweet, i will give it a try (would be very cool to have texts in xml & external editor for translators), but right now, i cant make it work (pretty sure its PEBKAC).

Btw & OT, “smrt” means “dead” (as in “black dead”) in my language :smiley:

splite | 2016-08-15 09:18

Another friend from the community pointed me that, which was not the intention at all.
It came from the Simpsons, when the original voice actor for homer, mispelled SMART when talking how smart homer is… The situation was so funny they left it there.

The video

Why the name? Well… I always felt SMRT(an idiot, not death haha) while making this plugin, as I was creating while learning. Since it is not offensive, I kept the name.

brunosxs | 2016-08-15 13:12

:bust_in_silhouette: Reply From: luislodosm

This is an example for signals with multiple arguments:

coin.gd

signal coin_taken

func _ready():
    add_to_group("coins")

func take_coin():
	emit_signal("coin_taken", 100, "gold")

coin_counter.gd

func _ready():
    var coins = get_tree().get_nodes_in_group("coins")
	for coin in coins:
		coin.connect("coin_taken", self, "add_coins")

func add_coins(number,label)
    print("You have got " + String(points) + " " + label)

In general:

Script1: emit_signal("signal_name", arg1, arg2, arg3)
Script2: coin.connect("signal_name", self, "triggered_method")