Javascript - How to dynamically Access SL variable in GetVar() inside a For-Loop?

Oct 15, 2019

I need to pass a value from the JS back into variables in SL. The variables are set up like a 'faux' array since SL doesn't have an array type variable. 

I would like to use a For-Loop to pass a value from JS into the SL variables. I can't figure how to dynamically access each SL variable. See my code below. It's not the whole code I will need ultimately need. I am just trying to work through this one problem I am having at the moment. 

// Generate 2 random numbers that will be used in the BillCodeType -- It's a number used in UB-40 forms. The 1st digit is always zero so I don't worry about that. The 1 is for Hospital. All my examples will be for a hospital. 

var randomThirdDigit = Math.floor((Math.random()*3)+1);
var randomFourthDigit = Math.floor((Math.random()*8)+1);
var LoopValue = 0;

// var BillTypeCodeList = []; 

var RandomNumber = 100 + (randomThirdDigit*10) + randomFourthDigit;

var player = GetPlayer();

player.SetVar("ThirdDigit",(randomThirdDigit*10));
player.SetVar("FourthDigit",randomFourthDigit);
player.SetVar("BillTypeCode",100 + (randomThirdDigit*10) + randomFourthDigit);

// First need to setup the loop counter value

//I pass the loopcount back into SL 
if (player.GetVar("LoopCount") == 0){
LoopValue = player.GetVar("TextEntry");
} else {
LoopValue = (player.GetVar("LoopCount")-1);
}

player.SetVar("LoopCount",LoopValue); /* Add LoopCount back to SL */
//var EachHolder = Holder + i ;
player.SetVar("Holder1", RandomNumber);
var EachHolder = '\"' + 'Holder2' + '\"';
player.SetVar('\"' + 'Holder2' + '\"', RandomNumber);
player.SetVar('Holder22', EachHolder);

/* Use a For Loop to add values to SL variables to see if we have use this value earlier
var i;
for (i = 0; i < player.GetVar("TextEntry"); i++) {
var EachHolder = "\"Holder" + i +"\"";
if (player.GetVar(EachHolder) != 0 && player.GetVar(EachHolder)!= RandonNumber)) {
player.SetVar(EachHolder, RandomNumber);
}
}*/

2 Replies
Michael Maier

Ok. I might have solved although it's klunkier than I like. I will only let them choose between 1-10 examples. 

  1. I generate all the random numbers (making sure there are no duplicates) and put those in an array. 
  2. I iterate through the array and copy the values to SL using a Switch Statement. 

It's not pretty, but it works. 

//
//
//


var randomThirdDigit = 0;
var randomFourthDigit = 0;
var LoopValue = 0;
var RandomNumber = 0;
var BillTypeCodeList = [];

var player = GetPlayer();


// First need to setup the loop counter value
if (player.GetVar("LoopCount") == 0){
LoopValue = player.GetVar("TextEntry");
} else {
LoopValue = (player.GetVar("LoopCount")-1);
}

player.SetVar("LoopCount",LoopValue); /* Add LoopCount back to SL */

// Generate random number list and add the array
for (i = 0; i < player.GetVar("TextEntry"); i++) {

randomThirdDigit = Math.floor((Math.random()*3)+1)
randomFourthDigit = Math.floor((Math.random()*8)+1)
RandomNumber = 100 + (randomThirdDigit*10) + randomFourthDigit;

if (BillTypeCodeList.includes(RandomNumber)){
;
} else {
BillTypeCodeList.push(RandomNumber);
}

;
}

// Copy random number list from array to SL
for (i = 0; i <= player.GetVar("TextEntry"); i++) {

switch (i) {
case 1:
player.SetVar("Holder1", BillTypeCodeList[i-1]);
break;
case 2:
player.SetVar("Holder2", BillTypeCodeList[i-1]);
break;
case 3:
player.SetVar("Holder3", BillTypeCodeList[i-1]);
break;
case 4:
player.SetVar("Holder4", BillTypeCodeList[i-1]);
break;
case 5:
player.SetVar("Holder5", BillTypeCodeList[i-1]);
break;
case 6:
player.SetVar("Holder6", BillTypeCodeList[i-1]);
break;
case 7:
player.SetVar("Holder7", BillTypeCodeList[i-1]);
break;
case 8:
player.SetVar("Holder8", BillTypeCodeList[i-1]);
break;
case 9:
player.SetVar("Holder9", BillTypeCodeList[i-1]);
break;
case 10:
player.SetVar("Holder10", BillTypeCodeList[i-1]);
break;
default:
;
}

;
}

//
//
//

Michael Maier

I figured out a better solution. 

//Setup Variables
var randomThirdDigit = 0; //CMS Bill Code Type 3rd digit
var randomFourthDigit = 0; //CMS Bill Code Type 4th Digit
var LoopValue = 0;
var RandomNumber = 0;
var BillTypeCodeList = []; //All the random numbers will get added to array
var j = 1; //Counter that I use the while loop
var player = GetPlayer(); //Need player object to access SL variables

// First need to setup the loop counter value
LoopValue = player.GetVar("LoopCount1");

//Initialize the Holder Variables in SL
var thisHolder = "";

while (j <= 10) {
thisHolder ="";
thisHolder += "Holder" + j;
player.SetVar(thisHolder,0);
j++;
}

// Generate random number list and add values to the array
for (i = 0; i <= LoopValue; i++) {

randomThirdDigit = Math.floor((Math.random()*3)+1)
randomFourthDigit = Math.floor((Math.random()*8)+1)
RandomNumber = 100 + (randomThirdDigit*10) + randomFourthDigit;

if (BillTypeCodeList.includes(RandomNumber)){
;
} else {
BillTypeCodeList.push(RandomNumber);
}

;
}

//Interate thru the array and copy to the SL variables
j =1;
thisHolder = "";

while (j <= LoopValue) {
thisHolder ="";
thisHolder += "Holder" + j;
player.SetVar(thisHolder, BillTypeCodeList[j-1]);
j++;
}

This discussion is closed. You can start a new discussion or contact Articulate Support.