Is there any way to use variable length of arguments?

Godot Version

4.2.1

Question

I want to make a function:
update_control(type, updater, ...other_args)
But i don’t know how to make length of arguments variable.
I tried ...name name... *nameand it caused errors.

It’s not currently possible. You could use an Array or Dictionary for the rest of the arguments.

1 Like

In Godot, you can use an array or a dictionary to handle variable-length arguments if you want to pass multiple arguments to a function without specifying them explicitly. Here’s an example of how you can achieve this:
func update_control(type, updater, other_args=):
# ‘other_args’ is an array that can hold any number of additional arguments
# You can use it as needed in your function

# Example: Print the type and updater
print("Type:", type)
print("Updater:", updater)

# Example: Loop through other_args
for arg in other_args:
    print("Other Arg:", arg)

Example usage:

update_control(“example_type”, “example_updater”, “arg1”, “arg2”, “arg3”)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.