Finding the name of a specific button in a script connetced to multiple buttons

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

Hi, so I have some code that will select some buttons in a group and when that button is pressed stuff happen, but the hting is, I have a dictionary that the keys have the names of the buttons. So when I try to edit the dictionary with the button using the code from before, it doesn’t get the name it gets the id of the button

here is my code

func _ready(): 
for button in get_tree().get_nodes_in_group("Wheats"):
	button.connect("pressed", self, "_some_button_pressed", [button])
	
func _some_button_pressed(button):
    #the "button" is the id of the said button, not the name
    print(Main.WheatDict)
    Main.WheatDict[button] = true
    yield(get_tree(). create_timer(Main.WheatTimer), "timeout")
    button.disabled = false
    Main.WheatDict[button] = false

This can be done lots of ways, but, since your button variable above contains the actual node reference to a given button - why not just use that as the key in your Dictionary too? That’s what I intended in my original suggestion (in the other question).

jgodfrey | 2023-01-09 20:03

I am little bit confused, do you mind explaining that more clearly?

DrSpyros | 2023-01-09 20:06

Hmmm… Rereading, I see that you are ALREADY using the node reference as the Dictionary key. Maybe I don’t understand your current problem. What do you mean by this?

So when I try to edit the dictionary with the button using the code from before, it doesn’t get the name it gets the id of the button

jgodfrey | 2023-01-09 20:19

Okie. So the code I have selects the buttons from the group I have called “Wheats”
When one of the buttons is pressed the code “func _some_button_pressed(button):”
And because the dictionary keys have the same names of the buttons, I try to edit the dictionary using the button, but the thing is, the variable button is not the name as I need it to be, it’s the button id, e.g “print(WheatDict[button])” returns “TextureButton:1535” NOT “Wheat1” as I need it to

I don’t know if I explained it good enough, I am not good at explaining

DrSpyros | 2023-01-09 20:25

I understand the code you posted above, and I see that your Dict uses the button reference itself as the key and the button’s disabled state as the value. That all looks fine. However, I don’t understand why (or where) you need the Wheat1 reference you mention? Where is that code?

jgodfrey | 2023-01-09 20:38

The “Wheat1” is the button and one of the keys in the Dictionary

DrSpyros | 2023-01-10 12:50

Where did the dict get a button name as a key rather than a button node reference? To be useful, all of the keys need to be the same kind of data.

Where’s the code that’s adding the Wheat1 dictionary key?

jgodfrey | 2023-01-10 14:02

The Dict is made in the Main file, the file where I set all variables when game starts, that’s why it says Main.WheatDict

DrSpyros | 2023-01-10 14:48

Can you post the code that’s adding the name-based keys? I assume you’ll just want to change it to use node-reference keys instead, but it’s hard to say without seeing it…

jgodfrey | 2023-01-10 14:51

Sure thing,

extends Node

var WheatDict = {
	"Wheat1": false,
	"Wheat2": false,
	"Wheat3": false
}

DrSpyros | 2023-01-10 15:29

Hmmm… OK. I’d suggest that you use the button’s reference there too but, if that’s done in the singleton itself, you may not have easy, direct access to those references. A few thoughts…

  • Rather than build that “static” dict with node names (as above), you could pass the node references into a method in that singleton and use them in the Dict’s construction.

Or, maybe easier… You could change the Dict to simply be keyed by the button name rather than it’s node ref (since that’s how the original dict is constructed). To do that, you’d need to change this code:

func _some_button_pressed(button):
    #the "button" is the id of the said button, not the name
    print(Main.WheatDict)
    Main.WheatDict[button] = true
    yield(get_tree(). create_timer(Main.WheatTimer), "timeout")
    button.disabled = false
    Main.WheatDict[button] = false

Just change those two lines that reference WheatDict[button] to WheatDict[button.name].

jgodfrey | 2023-01-10 16:11

Also, happy to take a look at the project if it’s available. With that, it’d be easier to offer more targeted advice.

jgodfrey | 2023-01-10 16:23

That would be nice to get some advice, I am pretty new to this and could help a lot. I am making an update to the game right now, It’s also available on itch.io if you want to see, the current download doesn’t have a lot of content

drspyrosuwu.itch.io/breadkingdom

DrSpyros | 2023-01-10 16:58

Also thanks a lot (again lmao)

DrSpyros | 2023-01-10 17:06

Thanks for the link. And, maybe you understood this, but to offer better advice, I was referring to the Godot project itself - not the build of the game.

jgodfrey | 2023-01-10 17:09

Oh, I’m sorry I misunderstood. To be honest, I don’t know how to do that

DrSpyros | 2023-01-10 17:17

If it’s in Github, just provide the link to the repo.

Otherwise, you’d need to:

  • Zip up the Godot project folder
  • Upload it to an online host (Google Drive, Dropbox, OneDrive, …)
  • Provide a link to the file.

jgodfrey | 2023-01-10 17:23

Oh, okie. I have a google drive file

here
Bread Kingdom – Google Drive

DrSpyros | 2023-01-10 18:16

OK, I see that you’re using the button name as the Dict key now. With that, is this working as expected?

jgodfrey | 2023-01-10 18:38

It actually is working!

DrSpyros | 2023-01-10 18:40