Script call to see if a variable exists in an array.

LazESigma

Veteran
Veteran
Joined
Nov 9, 2013
Messages
50
Reaction score
2
First Language
English
Primarily Uses
Hello, I'm trying to simulate some aspects of a roulette wheel in a gambling game.
I'm using variables and currently have the numbers working.
I'm attempting to use a variable array to easily check if the Number entered is either Red or Black.
Rather than have 36 conditional branches, I was hoping to use a script call in the event.
Something like this:
Variable 38 holds the Number the player entered. Variable 40 simply holds an array of all the Red Numbers.
Variable 41 is a test variable to see if this works.
No matter what Number I enter into variable 38, my 41 variable is always zero.


obj = $gameVariables.value(38)

arr = $gameVariables.setValue(40,[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]);

for(var i=0; i<arr.length; i++)

{

if (arr == obj) $gameVariables.setValue(41,1)

}

Any help would be greatly appreciated!
 

Doktor_Q

I'm not a real doktor, but I am a real Q
Veteran
Joined
Aug 1, 2016
Messages
881
Reaction score
573
First Language
English
Primarily Uses
RMMV
So it looks like red/black here is just odd vs even. In that case, you can save yourself the pain in the butt of writing them all out:
Code:
if (obj % 2 == 1) { do your thing here }
% in javascript is the "modulo" or "remainder" operator, which nobody taught me about in highschool math for some reason. It has nothing to do with percents, so do not be confused.

Basically, if you do "A % B", it divides A by B and gives you the remainder. 10 % 5 = 0, because 10 is an exactly multiple of 5. 12 % 5 = 2, because if you divide 12 by 5, you get a remainder of 2.

So if you want to tell if a number is even or odd, all you gotta do is ask "if I divide it by 2, does it have a remainder?" Because that's how even and odd numbers are defined.

https://www.w3schools.com/js/js_arithmetic.asp Check out javascript's arithmetic in general, it can save you a lot of trouble sometimes.
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,123
First Language
English
Primarily Uses
RMMV
The suggestion above is definitely the best if you want to check evens vs. odds. Note that red/black is different than even/odd in roulette. I wanted to go through your posted code and show why it wasn't working the way that you wanted it to:

Code:
arr = $gameVariables.setValue(40,[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]);
This line of code isn't assigning anything to the variable you've named "arr". It's assigning the array [1, 3, 5, ...] to game variable #40.

Code:
if (arr == obj) $gameVariables.setValue(41,1)
This line of code is comparing a number to an array. Those will never be equal to each other, a number can't be equal to an array of numbers.

If you wanted instead to assign all of the red spaces to a variable named "reds", you would do:
Code:
const reds = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]

Then if you want to check whether the reds array includes the number the player picked:

Code:
const playerPick = $gameVariables.value(38);
console.log('Is it included? ', reds.includes(playerPick));

Whether something is included in an array or not is a true/false question - it either is included in the array, or it isn't. So this would be best saved to a switch, which is meant for true/false values.

The entire code could be:
Code:
const playerPick = $gameVariables.value(38);
const reds = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36];
$gameSwitches.setValue(42, reds.includes(playerPick));

So if there's anything different than odds/evens (like the colors), you can use this suggestion. For even/odd, definitely use the modulo as described above, it's much simpler.

(As a side-note, if you could use the CODE tags in the future it would make it easier to read the code)
 

LazESigma

Veteran
Veteran
Joined
Nov 9, 2013
Messages
50
Reaction score
2
First Language
English
Primarily Uses
Thank you both very much for both usage of the modulo operator and for correcting my incorrect code.
The roulette wheel is up and spinning!
 

Latest Threads

Latest Posts

Latest Profile Posts

Next goal is to make pull & push + lift & throw systems with mouse support and have skill levels in them and action indicators!
>What's your avatar about? Is it yours?
I did not do that drawing. It's a screenshot taken from a comic featuring characters from Touhou such as Tenshi and Iku (the girl in the avatar). It's drawn by someone under the name of Yohane.
Link to the comic (SFW, but navigating around Dynasty Reader is NSFW)
Progress on my game is nearly done, so I figured I show you guys the title screen. I hope you like it!
Title Screen.png
Does this ever happen to anyone?: your making a battle sprite [or any sprite] then when you mess something up you then see it as something diff, then working with it? its hard to say in words but it happens to me all the time
found out how to change pfp, literally there's a hidden thingy on the bottom of it

Forum statistics

Threads
109,167
Messages
1,042,669
Members
141,665
Latest member
beauty143
Top