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,057
Reaction score
7,149
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 Posts

Latest Profile Posts

I have an idea, I am making low polygon 3D resources for a package in itch. but it occurred to me that I could export these as images and make MV / MZ compatible tilesets. What do you think of this?
I hate seeing threads where less-experienced user's ask for an opinion of their project, and the thread is full of negative feedback by others who are clearly not the target audience. I feel bad for the OP, and I hope they understand that no game can appeal to everyone. :frown:
Hm, just found out I can't use loops, yay.
-Ele
I just wanna Covid to be over and I have my normal life back... Feeling so bad these days, what can I do?

Forum statistics

Threads
109,074
Messages
1,041,867
Members
141,571
Latest member
1a23z11
Top