0 votes

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)
 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?


asked May 29 in Engine by Blissful (22 points)

1 Answer

+1 vote
Best answer

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:

- connect
- emit_singal

I hope that helps you

answered May 29 by whiteshampoo (976 points)
selected May 29 by Blissful

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? :)


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. ;)


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 :)

Maybe you also want to have a look at this, before updating ;)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.