The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I need to my cycles will create about 20 similar functions in code, can I even do this?

        button4ik.connect("pressed", self, "_on_"+str(button4ik.name)+"_pressed")

here "button4ik" it's a button node which running in cycle, and this code create 20 signals for multiple nodes, but to create equal quantity of functions for it i can't use something like:

func ("_on_"+str(button4ik.name)+"_pressed")

how can I solve that?

Godot version 3.2.3
in Engine by (15 points)

1 Answer

+1 vote
Best answer

Hmm you'll need to at the very least write the functions into your code. You can pass in things to your connect method so that will help identify which button was pressed, and which method to call. Here is an example of some of the things you can do:

func create_buttons(num_buttons):
    for index in range(num_buttons):
        var but = Button.new()
        but.connect("pressed", self, "_on_any_button_pressed", [but, index])
        self.add_child(but)

func _on_any_button_pressed(button_that_was_pressed, index_of_button_that_was_pressed):
    var functions_to_call = ["do_thing_one", "do_other_thing", "do_cool_thing"]
    if index_of_button_that_was_pressed == special_index:
        do_special_thing()
    elif button_that_was_pressed.text == "attack":
        attack()
    else:
        var the_function_name_to_call = functions_to_call[index_of_button_that_was_pressed]
        call(the_function_name_to_call)
by (3,906 points)
selected by

thanks. you holier than people that build mosques

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.