Example of connection from script. (スクリプトからシグナルを接続する例)


Since my understanding of when it is best to connect from a script was unclear, I created one example.

It is a simple example where the color of the point turns blue when the button is pressed.

We are modifying the ScoreBoard scene and the ScoreLine scene.

# ScoreBoard.gd


func add_row(player_name: String, player_points: String) -> void:
	var row :ScoreLine = preload("res://Scoreboard/ScoreLine.tscn").instance()
	player_scores[player_name] = player_points

	color_change_button.connect("pressed",row, "color_change") # add
# ScoreLine.gd (ScoreCol.gd?)


func set_player_name(player_name:String) -> void:
	name_label.text = player_name

# add
func color_change():
	# Change PointsLabel to blue.
	$PointsLabel.modulate = Color(0,0,255)

A video of the execution results can be found here.

I believe that nodes added with add_child cannot be set from the editor.

So I created this example as an approach to the node added by add_child.

The flow of applying this signal is as follows.

  1. Prepare a button to issue the signal. (ColorChangeButton node)
  2. Prepare a receiver method. (color_change method)
  3. Connect the button and receiver method with connect in add_row.
  4. Run the ScoreBoard scene and press the ColorChangeButton.
  5. The points of all rows will change to blue.

This time the node is applied to all the child nodes, but I did not present it in this example because it would be complicated if I wanted to apply it individually.

I hope this is helpful.











  1. シグナルを発行するボタンを用意します。(ColorChangeButtonノード)
  2. レシーバーメソッドを用意します。(color_changeメソッド)
  3. add_row内でconnectでボタンとレシーバーメソッドを接続します。
  4. ScoreBoardシーンを実行し、ColorChangeButtonを押します。
  5. 全ての行のポイントが青色に変更されます。



  • Nathan Lovato replied

    It's great to see you experiment like this to solidify your understanding.

    You really nailed signal connections, and this will definitely be a helpful reference for fellow students! Thanks for taking the time to share your findings in such details.

    If you wanted to apply the change to only one or certain nodes, you would use a condition inside the add_row function, or inside the color_change function. Either approach works and you would choose between not connecting or having branches in the connected function depending on a project's context.

    Here are two examples.

    Example 1. Connecting only certain lines:

    func add_row(player_name: String, player_points: String) -> void:
    	if player_points > 1000:
    		color_change_button.connect("pressed",row, "color_change")

    Example 2. Branching code in the connected function (all rows are connected, but they run different code branches when the button is pressed). The function String.to_int() used below converts the text into a whole number. In this code, we turn the score text blue if the player score is above 1000, and red if it is below 1000.

    func color_change():
    	if points_label.text.to_int() > 1000:
    		$PointsLabel.modulate = Color(0,0,255)
    		$PointsLabel.modulate = Color(255,0,0)


    1 love
  • cacapon replied

    Thank you Lovato for your comment!

    It was helpful to learn about the individual signal examples.

    I see, you are trying to connect only certain signals by inserting a conditional expression between them.

    I will refer to it.

    I know it's a lot of work to remake the course, but I look forward to it!

    I am glad that my comments are helping Lovato learn Japanese. :)


    Lovato さんコメントありがとうございます!