xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
I made a script call
JavaScript:
$gameVariables.mypool = [1,2,3,4]; // database item ID
$gameVariables.cardpic = ["000","001","002","003"]; // file name in folder
var index = Math.randomInt($gameVariables.mypool.length);
$gameVariables.setValue(57, $dataItems[index].id);
$gameVariables.setValue(58, $gameVariables.cardpic[index]);
$gameScreen.showPicture(99, $gameVariables.value(58), 0, 200, 200, 100, 100, 255, 0);

But i've been getting a type null error once in a while. cannot read property 'id' of null.

What im trying to achieve is i have a different file name from the item ID. but i want them to appear based on the element position so i used the index.

EDIT : cause of error
JavaScript:
$gameVariables.setValue(57, $dataItems[index].id);

new working code
JavaScript:
$gameVariables.mypool = [1,2,3,4]; // item ID
$gameVariables.cardpic = ["001","002","003","004"]; // filename in folder
var index = Math.randomInt($gameVariables.mypool.length); // random number based on size of array
$gameVariables.setValue(57, $gameVariables.mypool[index]); // store in var ID 57 the value of nth element of array
$gameVariables.setValue(58, $gameVariables.cardpic[index]); // store in var ID 58 the value of nth element of array
 
Last edited:

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
561
Reaction score
239
First Language
PT-BR
Primarily Uses
RM2k3
Removed for not being correct.
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,354
Reaction score
2,477
First Language
English
Primarily Uses
RMMZ
Math.randomInt gives you a random number from 0 to the argument value (non-inclusive), but $data objects start their indexing at 1. If the number generated is 0, it tries to access $dataItems[0] which doesn't exist. I think you've missed that you're looking at the *index* of the array rather than the value that's in that index.

What you should have for line 3 is

var index = $gameVariables.mypool[Math.randomInt($gameVariables.mypool.length)];
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,468
Reaction score
1,863
First Language
EN
Primarily Uses
RMMZ
[Edit: ninja'd.] The value of index will be 0, 1, 2, or 3 because you've forgotten to index your mypool array. The entry at index 0 of any database array is null, hence your occasional error.

Try something like this instead:
JavaScript:
var index = $gameVariables.mypool[Math.randomInt($gameVariables.mypool.length)];
I.e. "set index equal to a random value from the mypool array".

I also suggest storing mypool directly in a game variable instead, e.g.
JavaScript:
var mypool = [1,2,3,4];
$gameVariables.setValue(123, mypool);
I think your approach (adding a new property) is OK, but you might as well use the normal approach instead just in case~

Also, you'll probably want to separate the initialisation (mypool = [1,2,3,4]) and the showPicture stuff into separate script calls/events. (I guess your current setup is just for testing.)
 

rexie09

Villager
Member
Joined
Apr 4, 2021
Messages
23
Reaction score
9
First Language
English
Primarily Uses
RMMZ
IGNORE THIS: THIS IS WRONG: With some testing you could change
Code:
$gameVariables.setValue(57, $dataItems[index].id);
to
Code:
$gameVariables.setValue(57, window.$dataItems[index].id);
as that seemed to stop the error and give what you wanted.
 
Last edited:

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
Why is the regular way of defining array better? is it speed?

JavaScript:
$gameVariables.mypool = [136,137,138,139,140];
$gameVariables.cardpic = ["136","137","137","139","140"];
var index = Math.randomInt($gameVariables.mypool.length);
$gameVariables.setValue(57, $gameVariables.mypool[index]);
$gameVariables.setValue(58, $gameVariables.cardpic[index]);

This is what i did, and it works fine now. I checked with the show message.
Do i need to add this every time i access the event? I have multiple conditional branches that uses the mypool array and cardpic array.. I thought if i keep redefining them the values get added to the array..

JavaScript:
$gameVariables.mypool.length = 0;
$gameVariables.cardpic.length = 0;
$gameVariables.setValue(57, 0);
$gameVariables.setValue(58, 0);
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,354
Reaction score
2,477
First Language
English
Primarily Uses
RMMZ
If you want to add values to an array rather than redefining it, use .push.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
If you want to add values to an array rather than redefining it, use .push.
JavaScript:
if A

 mypool = [1,2,3,4]

else if B

 mypool = [5,6,7,8]

else if C

 mypool = [9,10,11,12]

my event structure is like that.. im worried it would lag or something because in game, that array has 20 elements and there are 8 ifs.. i think using script calls for the array could make the processing lag.
 

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
561
Reaction score
239
First Language
PT-BR
Primarily Uses
RM2k3
Sorry, I didn't understand this:
$gameVariables.mypool.length = 0;
$gameVariables.cardpic.length = 0;
Does it have the same result as done this way?

JavaScript:
$gameVariables.mypool=[];
$gameVariables.cardpic=[];

If "cardpic" is the same order as "mypool", it could look like this:
JavaScript:
$gameVariables.setValue(57, $gameVariables.mypool[index]);
$gameVariables.setValue(58, $gameVariables.mypool[index].toString());

Edit:
I advise you to always use square brackets when creating functions and defining conditions:
JavaScript:
if(){

}

function myWizardfunction(){

}
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,354
Reaction score
2,477
First Language
English
Primarily Uses
RMMZ
It doesn't do anything. You can't set .length, its return value is determined by the number of elements in the array.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
I'm trying to empty the array after adding values to it, so the this code i thought of would reset everything to null
JavaScript:
$gameVariables.mypool.length = 0;
//$gameVariables.mypool = [];
//$gameVariables.mypool.splice(0, $gameVariables.mypool.length)
$gameVariables.cardpic.length = 0;
//$gameVariables.cardpic = [];
//$gameVariables.cardpic.splice(0, $gameVariables.cardpic.length)
$gameVariables.setValue(57, 0);
$gameVariables.setValue(58, 0);

Can i do the to string? mypool array is [1,2,3,4] while the cardpic array is ["001","002","003","004"].. the zero place holders in the string name would get error?
 

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
561
Reaction score
239
First Language
PT-BR
Primarily Uses
RM2k3
But i've been getting a type null error once in a while. cannot read property 'id' of null.
the cause of the problem is that you treated it as if the constant of the variable were the value, but it only returns the position.

Try:
JavaScript:
$gameVariables.setValue(57, $gameVariables.mypool[index]);

"index" is returning the position of the array and not the content.

Array [ 1, 2, 3, 4] <- Id Iventory Index 0 1 2 3

In game:
RMMV-Array.png
 
Last edited:

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
question #2 : suppose i have 4 types of arrays.. I store mypool [1,2,3,4] to gameVar ID 90, then use mypool again with [5,6,7,8] store to gameVar ID 91, then my pool [9,10,11,12] to gameVar ID 92, and lastly mypool [13,14,15,16] to game var ID 93..

suppose i want a new mypool that is gameVar ID 91 + gameVar ID 92, how do you do this? and store them to gameVar ID 94.. I know this code is array to variable.. what if the other way around?
JavaScript:
var mypool = [1,2,3,4];
$gameVariables.setValue(123, mypool);

question #3 : is there any way to easily implement the to text string, when my files are named "001" , "022", "100" ? Or should i just rename them all so that i dont need the 2nd array mypic?

question #4 : i want to add the value "0" 60 times, "1" 21 times, "2" 10 times, "3" 5 times ,"4" 3 times, "5" 1 time..
my for loop with the push command, gives error..
JavaScript:
let arr = [];
for (let i = 0;i <= 5 ; i++){
return arr.push[i];}

$gameVariables.setValue(77, arr);
console.log($gameVariables.Value(77));

EDIT:
for question #4 : i found
JavaScript:
var aa = new Array(4).fill(0);
var bb = new Array(2).fill(1);
var cc = new Array(1).fill(2);
var dd = new Array(6).fill(3);

var yy = aa.concat(bb,cc,dd);

console.log(yy);
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,468
Reaction score
1,863
First Language
EN
Primarily Uses
RMMZ
Question 2...not sure what you're asking, maybe one of these points will help:
  • You can get the value of a variable using $gameVariables.value(id), e.g.
    JavaScript:
    var arr = $gameVariables.value(77);
  • You can combine arrays using Array#concat like you've done in your solution for question 4.
Note that arrays a reference type: be careful if you're using multiple variables to reference an array.
  • var arr = [1,2,3,4];
    Assigns a new array to a variable called arr. This works by storing the array data in memory, then assigning a pointer to the variable.

  • var arr2 = arr;
    Makes a second variable arr2 that points to the same array in memory. I.e. it copies the pointer, not the array.

  • arr2[0] = 5;
    Changes the first value in the array (index 0) to a 5. The array is now [5,2,3,4]. Note that arr and arr2 still point to the same array, so they have the same value!

  • arr2 = arr2.slice();
    Sets arr2 equal to a shallow copy of its current value. This means that now arr and arr2 can have different values, since they now point to different objects in memory. See Array#slice for details.

Question 3: RMMV defines a padZero method that converts numbers to 0-padded strings, e.g.
JavaScript:
var value = 5;
console.log('pic' + value.padZero(3));
Expected output: "pic005".
JavaScript:
let arr = [];
for (let i = 0;i <= 5 ; i++){
return arr.push[i];}
$gameVariables.setValue(77, arr);
console.log($gameVariables.Value(77));
I know you've found a working solution, but there are several errors here that seem worth pointing out:
  1. The Script command is essentially an eval statement. The return keyword is not valid in eval code.

  2. Array#push is a function: its argument(s) should be enclosed by round brackets. Square brackets are used for indexing properties.

  3. Code is case-sensitive: there is no method named Value (capital "V") on $gameVariables.
An error-free interpretation:
JavaScript:
let arr = [];
for (let i = 0; i <= 5; i++) {
  arr.push(i);
}
$gameVariables.setValue(77, arr);
console.log($gameVariables.value(77));
Expected output: [0, 1, 2, 3, 4, 5].
You can find technical documentation and examples on Arrays here, in case you're interested:


It doesn't do anything. You can't set .length, its return value is determined by the number of elements in the array.
I personally avoid it, but it is possible to set an array's length: doing so will extend/truncate it.
 

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
561
Reaction score
239
First Language
PT-BR
Primarily Uses
RM2k3
Question 3:

It has this way, which is considered obsolete:
JavaScript:
var mypool=[1,23,456,45,123,7];
var cardpic=[]; /*reset*/
for(i=0; i<mypool.length; i++){
  if(mypool[i] < 10){
    cardpic.push("00"+mypool[i]);
  }
  if(mypool[i] > 9 && mypool[i] < 100){
    cardpic.push("0"+mypool[i]);
  }
  if(mypool[i] > 99){
    cardpic.push(mypool[i].toString());
  }
}


And I did it that way, which I found faster and more efficient:
JavaScript:
var mypool=[1,23,456,45,123,7];

var cardpic=[]; /*reset*/

for(i=0; i<mypool.length; i++){
   cardpic.push(("00"+mypool[i]).substr(-3));
}
 
Last edited:

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
Thanks for the help everyone.. the padZero was really cool.. the new Array().fill() does not work for MV. i cant populate the array any fast than manually entering.
 

Dev_With_Coffee

Veteran
Veteran
Joined
Jul 20, 2020
Messages
561
Reaction score
239
First Language
PT-BR
Primarily Uses
RM2k3
The solution for question 3 is basically the same as for question 4.

If you really save the "index" in variable 57:
JavaScript:
$gameScreen.showPicture(99, ("00"+$gameVariables.value(57)).substr(-3), 0, 200, 200, 100, 100, 255, 0);

But as you are going to call Picture at the exact moment you save it, just convert it to "String" and include the zero just to call the file by the correct name:
JavaScript:
$gameScreen.showPicture(99, ("00"+$gameVariables.mypool[index]).substr(-3), 0, 200, 200, 100, 100, 255, 0);

new Array().fill() does not work for MV
Better to use "For" in this case.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
Reaction score
38
I have another question.. im trying to check if an Id is in the array, so i found the .includes.. but i get type error here..
$gameVariables.value(105) is the mypool array... then $gameVariables.value(118) is the random element pulled from the array.
1619664615685.png
 

Latest Threads

Latest Posts

Latest Profile Posts

image_2021-06-13_213812.png

Todays Lesson: Tiles are a pain to make xD
The game is not even 2% complete, but i just wanted to release something for funsies. :)
It's on my google drive.
It would be awesome if i could get a couple ideas and the like!
So yesterday Dad took me to Volcano Bay, and it was almost ruined by a thunderstorm... then it passed by and the lightning warning got lifted, leading to an awesome time sliding and swimming. Hopefully we get to go to the arcade tonight. The one we're planning on has a restaurant built in with AWESOME food. Not to mention how big the arcade itself is!
image_2021-06-13_150033.png

Maybe I'll actually try making a retro pack. Already have the shark fin made so why not xD
Hey everyone, for everybody following along with my RPG Maker MZ streams: There will unfortunately not be a stream tonight. There is a big exciting event that will go live next week that has been taking all my time, so look forward to that!

Forum statistics

Threads
112,196
Messages
1,066,518
Members
145,805
Latest member
Amber22
Top