[Solved] Accessing a dynamic named variable

How do I…

Access the variable “Answers.0[3]” dynamically

I’m trying “Answers.Variable(QuestionSelect)[3]”

With my number variable QuestionSelect set to 0

As a result I just get a 0

Answers is a structure and Answers.0 is an array

Based on what I understand, you have a structure variable called Answers that holds some data, likely in the form of questions or text. You want to dynamically control which data (or question) to select using a variable, rather than hardcoding each question.

While there might be simpler ways to do it, here’s a solution I prefer:


Answers["Question" + ToString(QuestionSelect)]

This way, you can change the QuestionSelect variable dynamically to retrieve different questions without hardcoding.

For example, if QuestionSelect = 1, this setup will output "Who is Nick?".

1 Like

Hi Danijel - thank you for replying

My variable structure is a little different, so I can’t make your code work for me…

"Answers." + ToString(QuestionSelect)[]

or

"Answers." + VariableString(QuestionSelect)[]

just doesn’t work…

It’s a multiple choice game, with multiple questions, all with 4 answers…

I see, so you essentially have a structure that contains arrays, which behaves similarly to a 2D array. This is why I added an additional variable to dynamically get the index from the arrays as well. Without that, I don’t think you’d be able to achieve this functionality.

The correct syntax would be:


Answers[Variable(QuestionSelect)][Variable(AnswerIndex)]

For example, your Answers structure might look like this:

[67, 42, 235, 34],
[23, 43, 87, 278]

This is similar to a 2D array. So, accessing Answers[1][2] would give you 87 (the third item in the second array).

If QuestionSelect = 1 and AnswerIndex = 2, the result would be: 87

Well that did it !

I only have four answers each round, so I can just repeat it from

to

I did not know you could reference a structure, like a 2d array.

Thank you very much for your kind help, stranger on the internet !

1 Like

I’d like to add [SOLVED] to the title, but I just can’t figure out how… Sorry.

1 Like