0 votes

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)
Godot version 3.5.1
in Engine by (58 points)

1 Answer

+1 vote

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.

by (372 points)
edited by

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.

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)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.