Connecting a custom signal with a method expecting arguments

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Blissful

Hi all!

Is it possible to connect a signal and a method that requires arguments?
More specifically, in the below code (attached to my grid map) I add an enemy to a grid and connect it’s death signal to a method in that grid map

func place_enemies(pos:Vector2):
 var new_enemy = enemy_scene.instance()
 new_enemy.position = (map_to_world(pos) + tile_offset)
 sorter.add_child(new_enemy)
 set_cell_content(new_enemy.position, ENEMY)
 new_enemy.connect("death", self, "vacate_cell")

func vacate_cell(pos:Vector2):
 var grid_pos = world_to_map(pos)
 grid[grid_pos.x][grid_pos.y] = null

Pretty straightforward. I just want to make sure that the data the grid was holding on to gets removed when the enemy dies.
However, the method requires a position to know which cell to clear of data and I don’t know how to work that into the whole connect thing.

Can this be resolved with a signal? Or am I going about this the wrong way entirely?

Thanks!

:bust_in_silhouette: Reply From: whiteshampoo

I tried this little script, and it works:

extends Node2D

signal test


func _ready() -> void:
	connect("test", self, "test_method")


func _process(delta):
	if Input.is_action_just_pressed("ui_left"):
		emit_signal("test", "left")
	if Input.is_action_just_pressed("ui_right"):
		emit_signal("test", "right")


func test_method(dir : String) -> void:
	print(dir)

docs:

I hope that helps you

Thanks for the quick answer!

I’ll try this after work, but it does seem like a good solution!

By the way, if you don’t mind me asking a follow up question:
I’ve seen people create functions with → void before, but a quick search online didn’t clarify much for me.
Would you mind explaining what the function of it is, or where I can find the relevant documentation? :slight_smile:

Thanks!

Blissful | 2020-05-29 07:52

Thats for Static Typing

just like

var variable : float

tells Godot, that this is a float,

func method/function() -> float:

tells Godot that this method/function has to return somthing, that is a float.
void for nothing. (just return without value or no return at all)

As you can see in this QA, without static-typing, you can produce hard to find bugs.
Use static typing whenever possible. :wink:

whiteshampoo | 2020-05-29 08:07

Hey!

I just had the chance to test this all out and it’s worked great!

Thanks for the explaining and introducing me to Static Typing as well, I can see how useful it is! Time to update my methods haha.

Thanks again :slight_smile:

Blissful | 2020-05-29 11:54

Maybe you also want to have a look at this, before updating :wink:

whiteshampoo | 2020-05-29 12:03

Thanks a lot, I will check that first then ^^

Blissful | 2020-05-29 19:16