- Home /
How to call a variable using a String for the variable name?
So I have a set of lines:
static var restartDrugged_1 = "Was all this just a flashback...";
static var restartDrugged_2 = "It's the drugs";
static var restartDrugged_3 = "Again";
and I have a variable that keeps changing dynamically
var restartDruggedTotal : int;
My idea would be to change the text that is displayed depending on the integer number so I wrote something like this:
var restartDruggedLine : String = "restartDrugged_" + restartDruggedTotal.ToString();
This means that the "restartDruggedLine" will have the name of the variable I want to call (e.g. restartDrugged_2 if restartDruggedTotal = 2).
But now how can I actually call the "restartDrugged_2"? I have a function that displays text on screen but if I run it with:
ShowText(restartDruggedLine);
I'll just get "restartDrugged_2".
Hope the question makes sense!
Comment
Best Answer
Answer by castor · Jun 07, 2013 at 05:24 AM
And just found the answer...by using Hashtables! Here is my current solution, if there is a better one please let me know.
var restartTotalLine = new Hashtable();
restartTotalLine[1] = "Text a";
restartTotalLine[2] = "Text b";
restartTotalLine[3] = "Text c";
and now i can do:
var restartTotalLine : String = restartTotalLine[restartTotal];