How to create variables dynamically [solved]

Hi guys 


How i can create a variable dinamicly in rpgmaker mv script ??


Am try to create a variable with incrementation.


Tanx for help, tell me if not possible ?!

Code:
Pid = 1; Listi= 1;

while(Listi<=5){
PIDNAME[Pid] = $gameScreen._pictures[Pid]._name;
Pid++; Listi++;
}


// Or i try this !
PIDNAME(Pid) = $gameScreen._pictures[Pid]._name;
PIDNAME+'Pid' = $gameScreen._pictures[Pid]._name;
PIDNAME+(Pid) = $gameScreen._pictures[Pid]._name;
var pid = 1;


var listi = 1;


Not sure why you've got the listi variable up there - you can simply use the pid variable:


for (var pid = 1; pid <= 5; pid++) {


  pidname[pid] = $gameScreen._pictures[pid]._name


}


if you put the var inside the for loop like I've done above, it'll be a local variable only and you won't be able to access it outside the loop.  If you do the var above that line, you will be able to.


I wouldn't give them names that start with upper case letters.  
var pid = 1;


var listi = 1;


Not sure why you've got the listi variable up there - you can simply use the pid variable:


for (var pid = 1; pid <= 5; pid++) {


  pidname[pid] = $gameScreen._pictures[pid]._name


}


if you put the var inside the for loop like I've done above, it'll be a local variable only and you won't be able to access it outside the loop.  If you do the var above that line, you will be able to.


I wouldn't give them names that start with upper case letters.  

is no work 


i test  


pid = 5;

pidname[pid] = 20;

console.log (pidname5 );


So the variable pidname5 should = 20
You didn't put what I said.


var pidname = [];
var pid = 5;
pidname[pid] = 20;
console.log(pidname[pid]);




I am assuming your $gameScreen._pictures[pid]._name is correct - I didn't look that up.


Also, when things "don't work", you need to tell us what happens.  Do you get an error message?  What does the message say?  Saying it "doesn't work" won't help us solve the problem because it gives us no information about why it doesn't work.
You didn't put what I said.



var pidname = [];
var pid = 5;
pidname[pid] = 20;
console.log(pidname[pid]);




I am assuming your $gameScreen._pictures[pid]._name is correct - I didn't look that up.


Also, when things "don't work", you need to tell us what happens.  Do you get an error message?  What does the message say?  Saying it "doesn't work" won't help us solve the problem because it gives us no information about why it doesn't work.

Tanks a lot, is for my menu item in full picture.

For why the var  listi  . is because i need to use it.

Code:
 // Give full list item for test
 getitems = 1;
 while(getitems<=40){
 $gameParty.gainItem($dataItems[getitems], 1);
 getitems++;
 }
 
 
 
// Listing item menu
// Listi= test premiere list
Itemlist = 1; // test itemid 1 in DB
Listi=1; // List loop (while) Listi/ max item show in screen
Pid = 100; // Picture id (100-1 eatch loop)
Xitem = 0; // X coor for the item + picturesize+ marge
Yitem = 0; // Y coor for the item + picturesize+ marge
InpicX = 100; // Initial X of menue item list
InpicY = 140; // Initial Y of menue item list
MargeX = 50; // Marge bettwenn 2 picture X 
MargeY = 50; // Marge bettwenn 2 picture Y
PIDNAME = '';// identifie le nom de limage a id de limage 
// Test if have item
while(Listi<=40){
Playerhaveitem = $gameParty.hasItem($dataItems[Itemlist]);
if (Playerhaveitem=true) {
$gameScreen.showPicture(Pid, '1-pelle-lv2', 0, InpicX+Xitem, InpicY+Yitem, 100, 100, 255, 0);
$gameScreen.setPictureCallCommon(Pid, 21, 1);
// On va cherche le nom et le donne
PIDNAME[Pid] = $gameScreen._pictures[Pid]._name;
}
console.log (Itemlist, Listi, Pid, Playerhaveitem);
if (Listi==10 || Listi==20  || Listi==30) {
	Xitem=0; Yitem+=102+MargeY; Itemlist++; Listi++; Pid--;
}else{Itemlist++; Listi++; Pid--; Xitem+=110+MargeX;}
}
yeah, you need to put var at the start to declare a variable.


Also = will set playerhaveitem to true, not test to see if it is true. You could also just do if $gameParty.hasItem($dataItems[itemlist]).


This is pretty basic stuff - you might benefit by going through some javascript beginner tutorials - w3schools will have one, but there will be other good ones out there.
yeah, you need to put var at the start to declare a variable.


Also = will set playerhaveitem to true, not test to see if it is true. You could also just do if $gameParty.hasItem($dataItems[itemlist]).


This is pretty basic stuff - you might benefit by going through some javascript beginner tutorials - w3schools will have one, but there will be other good ones out there.



$gameParty.hasItem($dataItems[itemlist]


Hum tank you for this one
for (var pid = 1; pid <= 5; pid++) {


  pidname[pid] = $gameScreen._pictures[pid]._name


}


if you put the var inside the for loop like I've done above, it'll be a local variable only and you won't be able to access it outside the loop.  If you do the var above that line, you will be able to.



JavaScript variables declared with the var keyword are not block scoped. A variable declared with var inside of a loop will be accessible outside the body of the loop. More troubling is that variables declared without var become global so always declare with var, as you suggested later, and never declare variables in the body of a loop. Though the by now the JavaScript engine is probably smart enough not to redeclare the variable over and over again. It's probably a bad practice anyway.
JavaScript variables declared with the var keyword are not block scoped. A variable declared with var inside of a loop will be accessible outside the body of the loop. More troubling is that variables declared without var become global so always declare with var, as you suggested later, and never declare variables in the body of a loop. Though the by now the JavaScript engine is probably smart enough not to redeclare the variable over and over again. It's probably a bad practice anyway.

tankxs for the info friend

Original thread: https://forums.rpgmakerweb.com/threads/how-to-create-variables-dynamically-solved.64862/