Arrays In Variables And How To Check If Value Is Present In Array!?

Discussion in 'RPG Maker VX Ace' started by Tsar CUBE, Feb 25, 2019.

  1. Tsar CUBE

    Tsar CUBE Villager Member

    Messages:
    9
    Likes Received:
    1
    First Language:
    Russian
    Hello!

    I have noticed that it is possible to set a whole bunch of interesting things to be present inside a Variable from one of the threads on this forum :)

    What I need to know is how to add values to a Variable that holds a given array and then how to check if that value is present.

    I need this for a number of systems. But one example would be to check what sort of NPCs have been recruited for the army. The variable would hold their names. Initially the variable would have no values, but as you add various people to your army, their names would get added in. So names like "Bob", "Bills", "Jack", "Fortesque", etc...

    I would then want to know how to set up a Condition branch to check if a Variable that has multiple names inside of it, has a specific one, like if the above example has "Bill" in it! Ie, an army that has someone named "Bill" in it would get a morale bonus, etc...

    I have a feeling that I saw something like this somewhere before but after spending 3 hours in google I came up with nothing (maybe I'm just blind o_O )


     
    #1
  2. TheoAllen

    TheoAllen Self-proclaimed jack of all trades Veteran

    Messages:
    4,212
    Likes Received:
    4,668
    Location:
    Riftverse
    First Language:
    Indonesian
    Primarily Uses:
    RMVXA
    Use ...
    Code:
    <array name here>.include?(<value here>)
     
    #2
    Tsar CUBE likes this.
  3. Tsar CUBE

    Tsar CUBE Villager Member

    Messages:
    9
    Likes Received:
    1
    First Language:
    Russian
    I should have mentioned, but I'm a noob in regard to scripting ^_^;

    I have something like this:

    $game_variables[302] = ["Bob","Bill","Jack",]

    I'm wondering how I can add another value to it like +"Phill" or something

    And then to check if the above has "Bill" somewhere inside.

    I get the feeling that the solution you posted would work too but I have no idea how to set up a real array in the first place to use it :(

    EDIT: I'm an idiot, I just plugged in $game_variables[302] as the array name and it worked... Thank you for that!!

    I would still like to know how to add more values to that array though :)
     
    Last edited: Feb 25, 2019
    #3
  4. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,461
    Likes Received:
    2,851
    Location:
    Philippines
    First Language:
    Tagalog
    To add a new value to an array you can use the push method

    $game_variables[302].push(value)

    push will add the value into the next index in the array

    if you want to input a value at a specific index you would do

    $game_variables[302][index] = value
     
    #4
    Tsar CUBE likes this.
  5. Tsar CUBE

    Tsar CUBE Villager Member

    Messages:
    9
    Likes Received:
    1
    First Language:
    Russian
    Awesome, thank you so much, it works!! :D

    I suppose if I push it as a specific index, and if that index already has a value, then it will overwrite that value? or would it push it aside instead?

    EDIT: ALSO!! Is it possible to remove a value without knowing what its index is? Like removing "Bill" if "Bill" is present in the array?
     
    #5
  6. A-Moonless-Night

    A-Moonless-Night WINTER IS COMING Veteran

    Messages:
    477
    Likes Received:
    342
    Location:
    New Zealand
    First Language:
    English
    Primarily Uses:
    RMVXA
    #6
    Engr. Adiktuzmiko and Tsar CUBE like this.
  7. Engr. Adiktuzmiko

    Engr. Adiktuzmiko Chemical Engineer, Game Developer, Using BlinkBoy' Veteran

    Messages:
    14,461
    Likes Received:
    2,851
    Location:
    Philippines
    First Language:
    Tagalog
    If you need more info on the array usage/methods you can actually see good definitions and examples in the Ace help file's "Index" section. Just go to the help file, click the index section then find Array on the list

    TBH, I recommend reading/referencing the help file when you're trying to learn how to manipulate the different RGSS objects available to you. I personally learned a lot from that and still look at it from time to time.

    You cant use push in an index because push doesnt take index as argument.

    If you're talking about the
    array[index] = value,
    then yes it will overwrite whatever value is currently saved in that index.
     
    #7
    Tsar CUBE likes this.
  8. Tsar CUBE

    Tsar CUBE Villager Member

    Messages:
    9
    Likes Received:
    1
    First Language:
    Russian
    Thank you, that works! :D

    Also, thanks for the link, I took a quick look through it and it seems like it has pretty much all the answers I'll need in regard to the arrays :D

    @ Engr. Adiktuzmiko

    Yes! Thank you, that's what I meant ^_^;

    I had a look at the help file before but it felt really confusing to me, at least until getting the replies on this thread. I think I sort of get how it works now though (on a basic level at least ^_^; ).
     
    #8
    Engr. Adiktuzmiko likes this.

Share This Page