How does one create a varying number of variables in a loop, each with unique names?

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

Say you want to write a function that will create a specified number of variables. The user would call it like so:

make_vars(3)

The function would then do the equivalent of this:

var example1 : str #Let's pretend these variables are going to be strings.
var example2 : str
var example3 : str

How would this function work on the inside? If GDScript had an exec() function like Python, this would be easy. It doesn’t, however. What should I do?

Why you need variables? Maybe an array will suffice?

anuke | 2021-07-27 13:54

:bust_in_silhouette: Reply From: Luftensteiner

I would save the strings in a dictionary.
Something like this would probably work:

var strDict = {}

for i in toLoop:
strDict[i] = “str”