Create new arrays in sequence with code

Discussion in 'Learning Javascript' started by sammc, Nov 6, 2019.

  1. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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.
     

    Attached Files:

    #1
  2. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    [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: Nov 8, 2019
    #2
  3. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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?
     
    #3
  4. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    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: Nov 8, 2019
    #4
  5. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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
     
    #5
  6. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    It seems I did misunderstand your initial post, sorry. :kaoslp:

    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.
     
    #6
  7. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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!
     
    #7
  8. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    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!)
     
    #8
  9. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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
     
    #9
  10. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    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:
     
    #10
  11. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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.
     

    Attached Files:

    • pic2.JPG
      pic2.JPG
      File size:
      31.7 KB
      Views:
      2
    • 33.JPG
      33.JPG
      File size:
      28.2 KB
      Views:
      2
    #11
  12. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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!
     
    #12
  13. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    ...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.)
     
    #13
  14. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    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.
     
    #14
  15. caethyril

    caethyril ^_^ Veteran

    Messages:
    1,276
    Likes Received:
    797
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
  16. sammc

    sammc Villager Member

    Messages:
    15
    Likes Received:
    2
    -- 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!
     

    Attached Files:

    • wip.JPG
      wip.JPG
      File size:
      34.8 KB
      Views:
      2
    #16
    caethyril likes this.

Share This Page