How to correctly connect buttons to a Dictionary

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

Hello, I’m putting together a shop and am getting help with a separate section of it, but I’ve run into an issue that is far out of my depth. I was told to connect all the shop buttons and go to the advanced menu to add an extra call argument: String. After doing so, my three buttons (so far) look like this

func _on_ButtonO_pressed(extra_arg_0):
extra_arg_0.connect("pressed", self, "on_button_pressed", ["Player"])
_buy(0, 0)
(Global.button_sound())

func _on_Button2_pressed(extra_arg_0):
	extra_arg_0.connect("pressed", self, "on_button_pressed", ["RedPlayer"])
	_buy(price2, 1)
	(Global.button_sound())
	
func _on_Button3_pressed(extra_arg_0):
	extra_arg_0.connect("pressed", self, "on_button_pressed", ["BluePlayer"])
	_buy(price3, 2)
	(Global.button_sound())

Now as far as dictionaries go, I have no idea what I’m doing, but this was the const I created:

const SPRITE_DICT = {

"Player": "res://Player.tscn::1",
"RedPlayer": "res://Player.tscn::2",
"BluePlayer": "res://Player.tscn::3"
}

Now my question is 1, I have no idea what the extra call argument does, but I’m sure it’s what allows the dictionary to distinguish entries, and 2, when I click on a button in the shop scene, I get an error on any one of the buttons that says

"Invalid Call. Nonexistent function ‘connect’ in base ‘String’.

Does anyone know why this is happening, and how to make the dictionary function properly?

:bust_in_silhouette: Reply From: Wakatta

More code is needed for a proper evaluation but from what i can see you’re passing in a string on your connect function when you should be passing in a node reference hence the error since connect is a virtual function of Object and not of type String.

So instead of

extra_arg_0.connect("pressed", self, "on_button_pressed", ["Player"])

its supposed to call a reference from the dict like this

extra_arg_0.connect("pressed", self, "on_button_pressed", [SPRITE_DICT["Player"]])

hmm I tried it out and at first it didn’t try and tell me something was wrong, but after clicking on the buttons in-game, I got the same

"Invalid Call. Nonexistent function ‘connect’ in base ‘String’. error

I think this means it has something to do with when I added the extra call in the advanced menu, any idea why it’s still doing this?

Spafnar | 2021-02-26 23:43

The code you’ve provided is not enough

For instance “res://Player.tscn::1” what is that a reference to?

also you have func _on_ButtonO_pressed but in your connect you have "on_button_pressed"

You have simply not provided enough information to get a proper response.
Normally one would do all signal connections in the _ready(): function but here you’re connecting them on button_press which is extremely weird considering there are no disconnects so once again unless more code is provided its hard to say if the problem is not somewhere else entirely

Wakatta | 2021-02-27 00:05

Ah good catch, as far as the path, thats the SpriteFrame resource for the “new skin”, and all the current skins’ paths are what is in the dictionary.

I tried changing the coding from the on_button_pressed to match what it is on all the functions, for example :

func _on_ButtonO_pressed(extra_arg_0):
extra_arg_0.connect("pressed", self, "on_ButtonO_pressed", [SPRITE_DICT["Player"]])
_buy(0, 0)
(Global.button_sound())

But the error code still appears. I don’t want to frustrate anyone with my impotence so if there’s any code in particular you need to see I can show it to you. I don’t know myself why the buttons are all being connected instead of putting them in the ready function, I’m just doing what was recommended. Essentially, player hits button; it purchases skin, player selects skin with button, it tells game to load in that skin via the dictionary.

Spafnar | 2021-02-27 00:29

No need to worry whats actually happening is that SPRITE_DICT["Player"] returns a string from your dictionary "res://Player.tscn::1" but you need a node reference can you post a pic of your scene tree

Wakatta | 2021-02-27 00:38

How do I import the screenshots it looks like I can only put an image url and it doesn’t seem to be working

Spafnar | 2021-02-27 04:32

the link alone is fine

Wakatta | 2021-02-27 04:41

Ok also I just figured out I was actually wrong earlier and what you said was correct, i misread the other guy’s original suggestion. I fixed it now, so all the buttons now are connected to the ready function. As far as the picture you want i’m still confused on how exactly to upload it, its on a folder in my computer I took three screenshots im not sure what you mean by link.

Spafnar | 2021-02-27 05:02

Hakuna Matata, glad you got it resolved.

Wakatta | 2021-02-27 05:17