Array Problem

Kaza

Villager
Member
Joined
Jan 12, 2016
Messages
22
Reaction score
2
First Language
English
I am having some troubles with using arrays within arrays.  I thought I had it figured out, but noticed when trying to change one, it will change the entire array.  Below is just a test I was doing but it doesn't do what I thought it should.  The original messages are all 0, which is what I had thought, but when I try to change 0,0  it also changes 1,0.    Would anyone be able to tell me what I'm doing wrong?


*Edit*


It must be an issue with making the array using a variable, because when I manually make the array with $gameSystem.testarray = [[0,0],[0,0]];   then I don't have any problems changing just one.  Is there some other way to do it or am I doing that incorrectly?


*Edit #2*


I'm pretty sure I figured out how to make this work for me as I haven't had any errors yet.  Let me know what you think.

Code:
$gameSystem.sellableitem = [];
for (var a=0;a<5;a++) { 
 $gameSystem.sellableitem[a] = [];
 for (var b=0;b<10;b++) {
  $gameSystem.sellableitem[a][b] = [];
  for (var c=0;c<20;c++) {
    $gameSystem.sellableitem[a][b][c] = [];
  }
 }
}


Code:
var t = [0,0];

$gameSystem.testarray = [t,t];

$gameMessage.add(String($gameSystem.testarray[0][0]));

$gameMessage.add(String($gameSystem.testarray[0][1]));

$gameMessage.add(String($gameSystem.testarray[1][0]));

$gameMessage.add(String($gameSystem.testarray[1][1]));

$gameSystem.testarray[0][0] = 1;

$gameMessage.add(String($gameSystem.testarray[0][0]));

$gameMessage.add(String($gameSystem.testarray[0][1]));

$gameMessage.add(String($gameSystem.testarray[1][0]));

$gameMessage.add(String($gameSystem.testarray[1][1]));
 
Last edited by a moderator:

Capitán

kind of a big deal
Veteran
Joined
Jul 9, 2013
Messages
572
Reaction score
145
First Language
Engilsh
Primarily Uses
RMMV
What are you trying to do exactly?
 

Kaza

Villager
Member
Joined
Jan 12, 2016
Messages
22
Reaction score
2
First Language
English
I'm eventually going to expand on this because I'm going to need too many variables to declare individually.  So say in one situation I have 10 npcs and I want to give all 10 of them 10 variables for properties and such.  It's much easier to do an array of 10 inside of another array of 10 than use the rpg maker for 100 variables.
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
795
First Language
English
Primarily Uses
RMMV
Uhh so to be clear do you want give an NPC 10 properties? Or 100 properties?


Or are you trying to do a list of 10 NPCs each with an array of 10 for properties?


For the latter there's already a list of events when a map scene is running. You can add an array of 10 to the NPCs that you want. Or if you want to index them you can too.
 

Kaza

Villager
Member
Joined
Jan 12, 2016
Messages
22
Reaction score
2
First Language
English
Hopefully this isn't going to be too confusing.  I am trying to make a kind of shopkeeper / economic trading game.  I planned on having roughly 4-5 towns with 10-15 shops per town.  Each building and shop will randomly generate if an npcs lives there and if they do how many and what their personality characteristics are and such. I was going to use arrays to track properties of the buildings such as is this shop owned, if so by an npc or player, what time does it open/close, what does it cost to buy, etc...  Then I needed to track the npcs characteristics as well such as their mood, likes, dislikes, if they have a job or not, etc...  Lastly I needed to track the items that are being sold at each shop with things such as their item type, price, quality, and if it's a player shop, their placed coordinates.


Let me try to give an example.


$gameSystem.npcinfo[0][0][0][0]  would reference City 1, Building 1, Npc 1, Alive/Dead


$gameSystem.npcinfo[1][2][3][5]  would reference City 2, Building 3, Npc 3, Job


So what I was doing above would work, except for some reason when creating the arrays using variables it messes up when changing a specific array.  I can manually create the array as stated in my edit and it works fine, but that basically defeats the purpose as there will end up being a few thousand variables.
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
795
First Language
English
Primarily Uses
RMMV
I understand and that's really ambitious.To clarify, the shop information isn't randomly generated, the shop are?


Arrays within arrays could become hard to manage; You could create a single array with an object for each shop and it's properties.
 

Kaza

Villager
Member
Joined
Jan 12, 2016
Messages
22
Reaction score
2
First Language
English
Yes, this will take awhile, but as I was thinking of a simple shop game it seemed quite bland with just selling things.  So to make it more interesting, I wanted the cities and residents to impact how the player has to run their shop.


Now that you talk about objects, I actually originally was using that for everything, but as I was generating npcs it seemed easier to use an array since they have something like 20 characteristics to generate.  However, with the shops and items, objects should still work.
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
795
First Language
English
Primarily Uses
RMMV
I agree; using objects for the shops and items would be for the best. With the residents, arrays could be better for handling the characteristics. @Kaza


To make your job easier with creating arrays within arrays.

Code:
//Creates arrays within an array  based on length
'use strict';
function createArrayInArray(array, length) {
  for(let i = 0; i < length; i++) {
    array.push([]);
  }
}

var myArray = [];
createArrayInArray(myArray, 10); //array of 10 arrays
console.log(myArray);
 

Kaza

Villager
Member
Joined
Jan 12, 2016
Messages
22
Reaction score
2
First Language
English
Thanks for the help!
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
556
Reaction score
795
First Language
English
Primarily Uses
RMMV
You're welcome; I hope it helps get the job done!
 

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

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,038
Messages
1,018,466
Members
137,821
Latest member
Capterson
Top