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

Godot

5681 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

[email protected]

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
[–] [email protected] 1 points 1 year ago (9 children)

Signals are best used when you use the prebuilt ones for each node (on the editor, it's a tab on the top right side, besides the node's parameters). For most cases, a signal is emitted when a certain event happens, like _on_animation_changed(): for an AnimationPlayer node.

I never checked the prebuilt signals for a TileMap (which I'm assuming you're using), but my suggestion is for you to check when you need the function to happen. If you want it to fire once another entity enters it (say, a player controlled character came in), it's likely you'll want to use an Area2D + CollisionShape2D nodes, the signal firing from the Area2D with _on_body_entered(body) - In this case, the body parameter is a pointer to the entity. You can call any functions it has and you can directly alter any of its variables, so you could hit a player from there.

The trick is really in the "when" you need to access the data. So, when will you actually need it those index numbers?

[–] [email protected] 5 points 1 year ago (8 children)

Signals are best used when you use the prebuilt ones for each node

Thas not true at all, I use custom signals all the time.

However, custom signal are not particularly useful within a class. They are useful for passing information up the node hierarchy. As Kids Can Code says: get_node down the tree, signal up the tree.

[–] [email protected] 1 points 1 year ago (7 children)

Is this the intended purpose for signals? Moving info up and down the tree? If so how are we supposed to send information from one script to another if one is not directly a child of the other? Do we send it to a shared parent node then send the info back down to the secondary script?

load more comments (6 replies)
load more comments (6 replies)
load more comments (6 replies)