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?
Signals are the same as events. It's just a different name. So, use signals to let other nodes/code know when something has happened (or is about to). It would only make sense to use a signal here if the values were changing and you wanted to let other nodes know about it. Like index_transform_changed(new_value).
I'm not sure what the tiles are for, but they're probably part of a collection or board of some kind. I would make this board it's own scene and have the board manage things. It could also make available transforms and indexes to other nodes, but that seems like something that would be best encapsulated within the board node itself. Then have the board get references to the children is controls via get_node, or using the $ syntax, etc.
Thank you! Okay so the tiles are part of a map, and I have a parent node called map with two separate children: hexgrid (where I'm instantiating the scenes) and spawn_objects( where I'm trying to gain access to the index and transform from hexgrid) my intent is to have hexgrid generate the grid and tiles and have spawn_objects instantiate an object within the tile at a certain position within it. Is this perhaps something I should combine into the same script? I typically like to have things modular and keep each component so a single specific task.
Ok, tell me if I got it right. Your node structure is currently like this:
And you want things to happen in this order:
If I got it right, then this is a situation where signals are not needed. What I would do:
spawn_objects
as a child node$spawn_objects.spawn_this_thing()
or declaring the node as an@onready var spawner = $spawn_objects (...) spawner.spawn_this_thing()
Since you'll be calling the spawn code from within the hexgrid, you'll have full access to both nodes' variables, parameters and functions. If you needed to get a "spawn_here" variable from the parent while within the child, you could use
get_parent().spawn_here
Interesting!! Yeah that's exactly what my node structure is. So basically instead of using signals and sending from one Map child to the other, use spawn_object as a child from hexgrid and then just call the spawn object function as needed since the hexgrid script already has a reference to the index and location of each tile. Thanks so much for the advice I'll give it a shot! I ended up working out the signals issue I was having anyways but it seems like your suggestion is a cleaner solution!
I hope it works! If it doesn't, just reply back and I'll see if I can help further