connect() function not changing parameter value, how do I do this?

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

I am just making a proof of concept for a game that I am working on and I am having an issue.
On start, the first few selections work fine, the issue is that when I try to change “category” the new parameter value I want to set with the buttons that I am re-using do not change.

Here is my code:

extends Node2D

var category

var tile

func _process(delta):

$Control/forest.connect("pressed", self, "category_selecting", ["forest"])
$Control/desert.connect("pressed", self, "category_selecting", ["desert"])

if category=="forest":
	var f_b0 = $Control/button0.connect("pressed", self, "tile_selecting", [0])
	var f_b1 = $Control/button1.connect("pressed", self, "tile_selecting", [1])
if category=="desert":
	var d_b0 = $Control/button0.connect("pressed", self, "tile_selecting", [2])
	var d_b1 = $Control/button1.connect("pressed", self, "tile_selecting", [3])

func tile_selecting(build_type):
	tile=build_type
	print(tile)

func category_selecting(category_type):
	category=category_type
	print(category)
:bust_in_silhouette: Reply From: LeslieS

The larger issue is if this code block is actually in the _process() function. That function runs every tick of the computer.
In theory you are connecting the signals thousands of times when you only need to connect them once.
You should refactor the code to remove those signal connect code lines so that they are only run once and only one signal not two (or more) for each button.
Let the signal handling function sort out “forest”, “desert” etc.

func selecting(value): 
	if category == "desert": 
		do_desert_stuff(value)
	if category == "forest":
		do_forest_stuff(value)
func _ready():
	$Control/forest.connect("pressed", self, "category_selecting", ["forest"])
	$Control/desert.connect("pressed", self, "category_selecting", ["desert"])	
	$Button0.connect("pressed", self, "selecting", [0])
	$Button1.connect("pressed", self, "selecting", [1])    

You may even find that the parameters [0], [1], [2], [3] are unnecessary.

Thank you!
But the reason for those perameters are to be able to re-use buttons without having to have and entirely different set of buttons for when the player is selecting a placeable object from a different set of objects.
eg:
page 1 (forest): tree(button0) grass(button1)
page 2 (desert): cactus(button0) sand(button1)

so for each page of items we can use the same buttons but just have them return a different value which is decided by the category/page we are on.

And this is why I needed the buttons to give different parameters based off of the category.

I will try a different approach to this using your code, and also thank you for helping me fix the amount of errors lol.

Lazulily | 2022-12-30 06:52

Alrighty! So I fixed it using your approach + arrays! It now works perfectly.
Thank you for your help!

If anyone needs it in the future I’ll put it here.

extends Node2D

var category
var tile

var forest_tiles = [0, 1]
var desert_tiles = [2, 3]

func _ready():
	$Control/forest.connect("pressed", self, "category_selecting", ["forest"])
	$Control/desert.connect("pressed", self, "category_selecting", ["desert"])
	
	$Control/button0.connect("pressed", self, "tile_selecting", [0])
	$Control/button1.connect("pressed", self, "tile_selecting", [1])

func tile_selecting(build_type):
	if category=="forest":
		tile = forest_tiles[build_type]
		print(tile)
	if category=="desert":
		tile = desert_tiles[build_type]
		print(tile)

func category_selecting(category_type):
	category=category_type
	print(category)

Lazulily | 2022-12-30 07:05