sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
Hi everyone,
I'm trying to implement a catalog of books published in my novel publishing sim, in the most clean fashion with a bit of javascript. I have considered the route of manually creating variables, but I need about 150 arrays.

1. Player creates a new book - a book array is created, containing the Title of Book, Page Count, Quality Score, Royalties, Age of Book, etc (about 15 variables or more).

My current solution involves creating 150 variables manually, naming them Book01-Book150. I would like to be able to create a loop so that when a common event is called, like CreateNewNovel, it names a new variable Novel01Array, and then I'm able to use it in the rest of the procedures I want done.
The next call on the common event would create Novel02Array and so on for all 150.
 

Attachments

  • arrays.JPG
    arrays.JPG
    46.5 KB · Views: 15

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
[Edit: this has issues, see my follow-up post below for a correction!]

If I'm understanding you correctly...you can use the Range option of Control Variables to assign an identical value to multiple variables. Here I would suggest using Control Variables > Range (2 to 150) > Set > Script:
Code:
$gameVariables.value(1).slice()
This is because an array is an object; without the "slice" (a.k.a. "shallow copy"), all the variables would simply point at variable 1, meaning you'd have 150 identical, self-updating "copies" of the same array, rather than 150 independent arrays of values. :kaoswt:

In case it helps, my quick test event looks like this:
Code:
◆Comment:Initialise template
◆Control Variables:#0001 v1 = ['name', 0, 0, 0, 0, 0]  //etc
◆Comment:Shallow copy template into variables 2~150
◆Control Variables:#0002..0150 = $gameVariables.value(1).slice()
◆Comment:Now do some tests...
◆Control Variables:#0151 check = $gameVariables.value(123)[0]
◆Text:None, Window, Bottom
:    :The name of book 123 is \v[151]!
◆Script:$gameVariables.value(1)[0] = 'beautiful soup';
◆Control Variables:#0151 check = $gameVariables.value(123)[0]
◆Text:None, Window, Bottom
:    :The name of book 123 is still \v[151]!
◆Control Variables:#0151 check = $gameVariables.value(1)[0]
◆Text:None, Window, Bottom
:    :The name of book 1 is \v[151]!
Note that you need to make sure at least 150 variables are defined in the editor. Variables not defined in the editor will not function as expected during gameplay (e.g. failing to receive their assigned values, always returning 0). :kaoback:
 
Last edited:

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
Thank you!

How should I go about defining them so I can access them at any time ("globally"?) without problems? Control Variable >>??

Also, how do I go about updating say book148[5] without changing book 120[5]? I tried it just now with
123.JPG

But it changed the other array. I'd like to be able to update strings and variables contained in these arrays. Seems straightforward but perhaps I'm not using the correct code to do it?
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
Edit @sammc: whoops, just realised I made a big oversight in my previous post, sorry! My previous method makes variables 2~150 all point to the same array, while variable 1 has its own array. I.e. 2 independent arrays, when you want 150. :kaoback:

To make them all independent you can use a Script command like this:
Code:
// default array
var init = ['name', 0, 0, 0, 0, 0, 0];
// loop over variables 1~150, a separate shallow copy for each
for (var n = 1; n <= 150; n++) {
  $gameVariables.setValue(n, init.slice());
}
...then to change a particular value, you can use something like this in a Script command:
Code:
$gameVariables.value(42)[0] = 'new title!';
Swap 42 for the variable ID and 0 for the array index. If you want to overwrite the entire array, you can do something like this:
Code:
var newArray = ['new title!', 5, 18, 27.2, 0, 0, -0.1];
$gameVariables.setValue(42, newArray);
(No need for slice here since the array is only being assigned once.)

I think that's correct, tested a bit more thoroughly this time and it seems OK. Let me know if there's any more issues, though! Sorry again for the confusion~ :kaoslp:
 
Last edited:

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
OMG! I think you got it : D This is amazing, I knew it was a loop like what you have but I had no idea how to phrase it.
Thank you caethyril!

However I'm probably not using it correctly. Can you see what I did wrong here?

Here's what I think I'm doing. A common event is called when the CurrentBook variables are filled out with some data and "NewBookCreate" runs:
It creates 150 arrays one by one, giving them a unique number. CurrentBook variables are passed into ONE of the next arrays created.
Show Text displays Book 381 contents \v[381] which is the first array that was created.

(I am still not sure how script creating these arrays vs naming variables works...so I've attached whatever the heck it is I'm attempting in the images. Please refer to them)

https://ibb.co/KyFJF55
https://ibb.co/NLR15dw
https://ibb.co/6Ds51sD
https://ibb.co/F0r2Z9N
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
It seems I did misunderstand your initial post, sorry. :kaoslp:

Here's what I think I'm doing. A common event is called when the CurrentBook variables are filled out with some data and "NewBookCreate" runs:
It creates 150 arrays one by one, giving them a unique number. CurrentBook variables are passed into ONE of the next arrays created.
Show Text displays Book 381 contents \v[381] which is the first array that was created.
You probably don't want to put a loop that changes all arrays in an event designed to initialise only one array. I think my third script call would be appropriate there, i.e. something like this:
Code:
var varId = $gameVariables.value(1);  // ID of variable to assign the array to
var newArray = ['new title!', 5, 18, 27.2, 0, 0, -0.1];  // new array!
$gameVariables.setValue(varId, newArray);
Also I'm not sure about the "unique number" thing you mention...the loop script I provided sets the values identically for all arrays in the specified range? :kaoswt:

Regarding your screenshots:
  1. The error is due to you having written this:
    Code:
    gameVariables.value(48)
    ...instead of this:
    Code:
    $gameVariables.value(48)
    (Hey, at least it should be an easy fix! :p)
  2. The script call assigns an array to each of the variables with ID in the range 1~150. You then attempt to change the first element of variable 381, which has not been assigned an array value in that event. From what you've said, I'm guessing the first array should be in variable 381? If so, you can change the bounds on the loop, e.g.
    Code:
    for (var n = 381; n <= 530; n++) {
      $gameVariables.setValue(n, init.slice());
    }
    Also, as mentioned, you probably want to keep the "initialise all arrays" thing separate from "create new book"; run the former once on new game and the latter once per new book.
  3. Addressed above.
  4. Same info as screenshot 2, above.
 

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
So far so good, I got it to create an array and Show Text displays the array contents. They seem to be unique each time!

I'm noticing one glitch with one variable -- it adds 000000000 to the string that is inside the variable. For example, CurrentGenre = "None" will display as None00000000000000000 when I ShowText the array contents. The other numeric variables look great, though.

Thank you very much!
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
I'm noticing one glitch with one variable -- it adds 000000000 to the string that is inside the variable. For example, CurrentGenre = "None" will display as None00000000000000000 when I ShowText the array contents. The other numeric variables look great, though.
Strange. Maybe you're getting the entire array rather than a particular entry of it? E.g.
Code:
$gameVariables.value(5);      // gets entire array
$gameVariables.value(5)[1];   // gets second entry of array (first is 0)
Not sure, though...for me arrays get printed with their entries separated by commas, e.g. "None,0,0,0,0". A few ideas:
  • Have you assigned the string correctly, using either "double" or 'single' quotes? E.g.
    Code:
    $gameVariables.value(5)[0] = "The incredible book";
  • Is it a particular length of zeroes that gets added?
  • Is it just one variable and/or one array index this happens with, and if so which one?
  • If you're testing from an old save (inadvisable!): does it happen if you test from new game?
  • Are you sure you haven't added the zeroes in some test code somewhere and forgotten about it? (I've done that sort of thing before!)
 

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
It has been kind of random, 0000 or 00000000000000000000000000 or 00 etc. I'm not sure what's causing it, but I'm suspecting a plugin conflict.
It only happens with the string - the others look accurate to what they're supposed to be. It's the text variable (string I mean).

-I have tried using ' ' and " " and it makes no difference
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
If you suspect another plugin's causing it, you could try temporarily turning off all your plugins (Plugin Manager > shift-click to select all > right-click > turn OFF), saving your project, play-test, new game; if that solves it then you can try turning plugins back on and testing one by one to identify possible culprits. :kaophew: Otherwise I have no idea, sorry. :kaoswt2:
 

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
So I found a bit of code running in a common event that effected the 4th item in an array. I deleted it. However, the problem persists. I've gone through all my code at this point a few times.
I'm gonna need to look at my plugins next.
 

Attachments

  • pic2.JPG
    pic2.JPG
    31.7 KB · Views: 2
  • 33.JPG
    33.JPG
    28.2 KB · Views: 2

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
variable 36 is the one giving me trouble. The rest are numerical, but they also don't seem to be getting additional 0's added to them.
Thanks for the tips!
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
...well, I don't know why that'd be happening if you're doing things correctly. :kaoswt: All things considered, I decided to make and share a little demo project of my own showcasing this kind of thing. It uses a slightly different approach (one array for all books) but it seems to work OK for me. Maybe it'll help in some way? :kaothx:

Here's a link in case you're interested: view/download varArrayBooks.zip (Google Drive) [2 MB].
(Remember it's a .zip archive so you'll have to extract it before you can open the project in RPG Maker.)
 

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
I managed to "fix" the issue in the mean time by using a different variable. No issues now. 36 is in quarantine until I figure it out.
 

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
-- Wow, that's a great CRUD system, thank you for sharing!
I think I may end up using some of what you made here if I need to delete books or edit them.
Looks complicated so it would take time to tinker.
Mine is a bit simplified at the moment so I can bare with the complexity until I add more detail. You can really hurt yourself by adding too much too fast to a game!
 

Attachments

  • wip.JPG
    wip.JPG
    34.8 KB · Views: 6

sammc

Villager
Member
Joined
Apr 30, 2013
Messages
28
Reaction score
2
Primarily Uses
One other question still on the same topic...

I would like to regularly update a variable by adding up all of the numbers stored in different arrays.

AllBooksIncome = Bookarray001[4] + ... Bookarray999[4]

As books are added, I want to be sure I capture whatever income is earned by each book array that has been generated, and add all their numerical values to a totals variable. Is this done using a similiar script as provided by caethyril?
 

Latest Threads

Latest Profile Posts

SpyroFan67 wrote on HexMozart88's profile.
Hi Hex. It's good to see you back. You know, you didn't do anything wrong. That guy WAS being rude-you were RIGHT-and he continues to be rude and condescending in his posts. I know you went through a lot of trauma with being bullied. I honestly think you were just trying to protect other people so they wouldn't go through what you had to go through. I actually think that was quite noble of you to be honest.
Design Aesthetic Question Time. For the Main Menu:

Primary and Subclasses


Side By Side:

SbSClassesStyle.png

Or Stacked:

StackedClassesStyle.png
World building was one of the main reasons i got in to game dev, but geeez, it eats up the time... This is a WIP from today before i call it a night.. maybe 80% finished.
maps.png
Dial_000_f002_show.jpg
Wohoo, its finished. It took some time but slowly I get used to this retro 90s anime style.
Now I just have to finish the other 37 frames. for the first dialogue... oh lord, what have I done :LZSlol:
(and if you are confused: yes, I place various eastereggs about other great cyberpunk games/videos/books that I like in my game ;) )
Has anyone watched the movie "Tiny Toons: How I Spent My Summer Vacation"? It is a cartoon and it is freaking hilarious. We watched that movie so many times when our kids were growing up. Thankfully it has a lot of adult humor to keep the adults entertained that the kids don't really get.

Forum statistics

Threads
131,778
Messages
1,223,311
Members
173,563
Latest member
yiuro
Top