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

Tsar CUBE

Villager
Member
Joined
Aug 24, 2013
Messages
9
Reaction score
1
First Language
Russian
Primarily Uses
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 )


https://www.houseofnames.com/fortesque-family-crest
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,206
Reaction score
7,476
First Language
Indonesian
Primarily Uses
RMVXA
Use ...
Code:
<array name here>.include?(<value here>)
 

Tsar CUBE

Villager
Member
Joined
Aug 24, 2013
Messages
9
Reaction score
1
First Language
Russian
Primarily Uses
Use ...
Code:
<array name here>.include?(<value here>)

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:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,005
First Language
Tagalog
Primarily Uses
RMVXA
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
 

Tsar CUBE

Villager
Member
Joined
Aug 24, 2013
Messages
9
Reaction score
1
First Language
Russian
Primarily Uses
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

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?
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,005
First Language
Tagalog
Primarily Uses
RMVXA
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.

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?

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.
 

Tsar CUBE

Villager
Member
Joined
Aug 24, 2013
Messages
9
Reaction score
1
First Language
Russian
Primarily Uses
You can use:
Code:
$game_variables[302].delete("Bill")

Also, I'd recommend reading through this info on arrays: https://docs.ruby-lang.org/en/2.0.0/Array.html

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 ^_^; ).
 

Latest Threads

Latest Profile Posts

I'm back from an 11 year hiatus. Lol
I sometime envy the people that haven't played Skyrim yet. I feel like I could trade the whole world to be able to make my first pilgrimage to High Hrothgar all over again
Come join us while we play Pocket Mirror by Astral Shift! :LZSjoy: Starting at 2PM ET today!
Tried changing profile icon but...It just felt weird, you know? Who would even want to talk to me if I didn't have this fat yellow rodent thing....Or whatever it is as a mascot....No one, that's who! I am nothing without it. NOTHING!!..
I am having such a good hair day but because it's thunderstorms all day I don't go out and as such nobody will see my hair >.< It's not often my hair cooperates!

Forum statistics

Threads
112,338
Messages
1,067,553
Members
145,980
Latest member
aki42
Top