How to create variables dynamically [solved]

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
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;
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,792
Reaction score
12,219
First Language
English
Primarily Uses
RMMV
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.  
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
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
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,792
Reaction score
12,219
First Language
English
Primarily Uses
RMMV
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.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
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;}
}
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,792
Reaction score
12,219
First Language
English
Primarily Uses
RMMV
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.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
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
 

Clock Out

Veteran
Veteran
Joined
Jun 14, 2016
Messages
92
Reaction score
44
First Language
English
Primarily Uses
RMMV
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.
 
Last edited by a moderator:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
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
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts



Almost 5 am.. and I decide to put up a Youtube video instead of sleeping. It's strangely addicting! :kaoswt:
If I survive the pandemic without 2nd degree obesity, I'll celebrate. The stress I have to deal with at work is so insane my candy consumption has increased by 400% just to help me survive the day sometimes.
I don't like calling my daily stuff coronadailies... anyone a better suggestion?
Well, looks like I need to build myself a quest system. Or really just a system for "Helpful voices" to talk to the player. I'm sure nothing will go wrong listening to them.... They're "helpful". :LZSwink:

Should be pretty easy with PIXI just letting me write all over the screen. :LZSexcite:
I'm starting to think I just dont like anime since the last one I enjoyed was none of them.

Forum statistics

Threads
95,595
Messages
930,339
Members
125,902
Latest member
bootlegtexas
Top