Variables, and using variables to form the names of other variables.

Godot Version

Godot 4

Question

Hello, I have a question regarding using a variable’s value to form the name of a variable in my ‘GlobalResources’ autoload script. I am making these audit paper collectables, and I just want to know how I can have the ‘id’ of each collectable match up to an id variable in the autoload script. This will allow my game to keep track of what audit papers have been collected. I don’t know how to get my ‘id’ variole to finish the name of the variable in the autoload script.

Thank you in advance for answers

This is unfortunately close to impossible with GDScript.
Instead, you can use an Array or a Dictionary variable in your GlobalResources autoload to store and read the status of your papers.
Or you can create a custom Resource called AuditPaper that you can then reference in an Array of you GlobalResources.

1 Like

If I understand (maybe I don’t) you could use to_int.

id = “audit_paperid_1”.to_int

Thank you, your advice has led to the system I wanted!

1 Like

Great! Make sure to mark my response as a solution so that others know the issue is resolved in case they ever encounter a similar issue.
And if you don’t mind, maybe you could share what you decided to use in your code :slight_smile:

You can use get to retrieve variables by string/name

GlobalResources.get("audit_paperid_" + str(id))
1 Like