this post was submitted on 21 Sep 2023
44 points (100.0% liked)

Godot

5876 readers
9 users here now

Welcome to the programming.dev Godot community!

This is a place where you can discuss about anything relating to the Godot game engine. Feel free to ask questions, post tutorials, show off your godot game, etc.

Make sure to follow the Godot CoC while chatting

We have a matrix room that can be used for chatting with other members of the community here

Links

Other Communities

Rules

We have a four strike system in this community where you get warned the first time you break a rule, then given a week ban, then given a year ban, then a permanent ban. Certain actions may bypass this and go straight to permanent ban if severe enough and done with malicious intent

Wormhole

!roguelikedev@programming.dev

Credits

founded 1 year ago
MODERATORS
 

Hello all!

Like most people I find myself a recent refugee from the Unity fiasco. I've been trying to prototype a project in Godot and I've been running into an issue I would think would be pretty easy to find a solution to as it seems to be a pretty fundamental building block of any project in Godot. Perhaps I'm misunderstanding how to accomplish this in Godot, but essentially I'm instantiating a number of tiles to be used for a grid system in my game. I want these tiles to be able to emit their index and transform values and then have other scripts pick this information up as needed. From what I've read signals are the way to do this, however whenever I try to send a signal with or without parameters nothing seems to happen. I seem to be able to connect to the signal just fine but the method doesn't seem to be called.

Here's an example of me defining the signal and then emitting it:

signal index_transform()

index_transform.emit()

And here's how I am connecting and attempting to call the method in a secondary script:

func _ready() -> void:
	var hexGrid = get_node("/root/Main/Map/HexGrid")
	hexGrid.index_transform.connect(Callable(self, "_get_hex_index_transform"))

func _get_hex_index_transform():
	print("I'm Connected")

And when I'm passing parameters from what I understand I should only have to include the parameters like so:

signal index_transform(index, transform)

index_transform.emit(tile_index, tile_coordinates)
func _ready() -> void:
	var hexGrid = get_node("/root/Main/Map/HexGrid")
	hexGrid.index_transform.connect(Callable(self, "_get_hex_index_transform"))

func _get_hex_index_transform(index, transform):
	print("I'm Connected")
	print("INDEX: ", index," POS: ", transform)

However neither of these seem to work. What am I doing wrong?

you are viewing a single comment's thread
view the rest of the comments
[–] TechieDamien@lemmy.ml 10 points 1 year ago (3 children)

Are you emitting in a function that gets called. If you are not sure, put a breakpoint in that line and see if you stop there. Also check to see if you connect first before emitting. One more thing, you don't need to make a callable, you can just use the function as it is a first class member of the class now, eg:

hexGrid.index_transform.connect(_get_hex_index_transform)

Be sure not to leave in any brackets added after the function that autocomplete likes to add.

[–] plixel@programming.dev 1 points 1 year ago (2 children)

I'm emitting in the function that instantiates the tiles so it's definitely getting called. How do I verify that the connection is going through?

Also thanks for letting me know how to format it I was getting conflicting information regarding the syntax from various different tutorials and resources.

[–] TechieDamien@lemmy.ml 3 points 1 year ago* (last edited 1 year ago) (1 children)

I believe there is a property on the signal to see the connections. From what you just said, it is possible that you are emitting before the connection is made. You can verify this using a couple print statements. If this is correct, the solution would be to either move the emission later or move the connection earlier (_init happens before _ready). Depending on what these functions are doing, feel free to pick whatever solution works best for you.

[–] plixel@programming.dev 1 points 1 year ago* (last edited 1 year ago)

Hm..I didn't even consider that the emit might be happening before the connection. I have them both running from separate on_ready methods. Perhaps I should be running connection though _init. Typically in unity I would just use C# actions and use OnEnable to establish the "connections." It make sense that the code could be emitting before I even have a chance to connect to it, which is why my test fails.