Problem with multiple Conditional Branches

Does anyone know why something like this might only work sporadically?

If V1 > 17
V2 = 6
If V1 = 17
V2 = 7
If V1 = 16
V2 = 8
If V1 = 15
V2 = 9
If V1 < 15
V2 = 10

The V2 rows are all indented, but for some reason my iPad isn’t cooperating. These are Event Commands and not scripts.

These are not nested Conditional Branches. Each one should be checked in the order they appear.

V1 is a random number from 3-19.

Is there any logical reason this series of Conditional Branches shouldn’t work? If so, is there a better way to do this?

I can post a screenshot of the actual portion of the Event tomorrow. But something is nagging at me that there is something wrong with this logic.

Thanks in advance for any assistance. This is driving me a little crazy.
specifically, what is "not working"? are multiple firing off? are none?

unless you have a parallel event running that alters V1, im not sure what else could be happening here that would throw it off.
The V2 variable is remaining at a value of 10 even when it shouldn’t.
Sounds like " If V1 < 15 " is considered always true, as it comes at the end of your conditions.

Just to make sure, is there a > instead of <?

For debugging, you could add a textmessage in every condition and see if it's running through.
Like "Changed value to \v[2]."
if they're not required to be not-nested, nest them by pairs of true/false logic.
Code:
if v > 17:
  n = x
  exit
else
  if v < 15:
  n = x
  exit
  else
   if v == 16:
     n = x
     exit
... etc.

if V > 17, it will never be < 15, so process and exit.

additionally, call a script command to forcibly check var.between(x,y), if you know for sure the logic is sound.
just to rule out that something must be overwriting the value.
use two methods to check for two errors, that can't possibly interact with one another.
Yeah, a few Show Text commands should show you exactly what's going on.

Put one right after you assign the random value to V1 and display what V1 is.

Then copy and paste that immediately inside each Conditional Branch, display what V1 is again.

If there's some other event or plugin changing the value between when it's assigned and when it's checked, that will be immediately obvious when the displays don't match. And if they do math, it should point out where your logical error or typo in the conditions is.
Is there any logical reason this series of Conditional Branches shouldn’t work? If so, is there a better way to do this?
If you are looking for a direct solution that avoids conditional branches (assuming the numbers for V2 are falling in line with V1), try this script call:
JavaScript:
const v = $gameVariables._data;
v[1]    = Math.randomInt(17) + 3;
v[2]    = (24 - v[1]).clamp(6, 10);
Do a Show Text with v[1] and v[2] and you should get your results.

Use other variables as needed and check for other issues (parallel events, plugin conflict or assignment of variables, etc.).
@Zia_ Thanks for that suggestion. I did try to use Text to trouble shoot, but I didn't initially spot anything.

@gstv87 I tried the nested option before I posted and I got the same results.

@ATT_Turan & everyone. Here are screen shots of the applicable part of the Event where I inserted Text commands to make sure the Switch is on that triggers that part of the Event, and at each point in the process.

What I discovered is that the first and last Conditional Branches work. The ones in between don't. So the >17 and <15 seem to work consistently, but the =17, =16, and =15 don't. Even though the value of the Variable is equal to those numbers.

Spoiler: Conditional Branch Series
Conditional Branch Issue 1.pngConditional Branch Issue 2.pngConditional Branch Issue 3.png

@bass2yang I will try your suggestion next and if it works, I will try to understand exactly what that is doing. EDIT: I tested your script in a test event and it works great and is a lot more efficient! Thank you so much for that! If you have time, I would love if you could explain what the script is actually doing in each step.

But I would really like to understand why the Conditional Branches are behaving the way they are.

There isn't anything changing the value of the any variable. As I mentioned, ,if the Variable is 18 or 19, the >17 Conditional Branch works.


If the Variable is <15, the <15 Conditional Branch works. It is the 3 in between that aren't working for some odd reason.
you splicing them from a string?
convert them to integer, JIC.
you splicing them from a string?
convert them to integer, JIC.
I don't think I am splicing from a string. I am randomly generating numbers that are pushed into an array. Then I am splicing each number from the array and assigning the number to a Variable.

But, if I was splicing from an array of strings, how would I convert that splice to an integer? Thanks!
If you have time, I would love if you could explain what the script is actually doing in each step.
Sure thing! I prefer direct assignments when I know that the sequences and variables are predictable with some math. This helps alleviate the "if this is 1, make that 10, if this is 2, make that 9, etc."

Steps:
Spoiler
JavaScript:
const v = $gameVariables._data;
- reference v as the game variables data list/array - works like the text box and damage formula references
JavaScript:
v[1] = Math.randomInt(17) + 3;
- set game variable 1 to a random number between 3 and 19
JavaScript:
v[2] = (24 - v[1]).clamp(6, 10);
- assign game variable 2 with the value as presented:
- the sequence of 15 is to 9, 16 is to 8, 17 is to 7 creates a sum of 24 consistently
- using the difference of 24 and v[1] gives you the proper sequence of results
- since the range above 17 that must always become 6 and the range below 15 must always become 10, you use .clamp() to set the value limits for your variable.

As for the if statements, I tried recreating your steps in js (can't access MV at the moment) and it works.

My guess is whatever is being parsed/spliced from game variable 542 may not be what you are looking for, or, the value is being altered and assigned during the event command processing.

Have you tried keeping only the 15, 16, 17 assignment values and removing the < 15 and > 17 statements? Does that work properly at least?

Also, try using else ifs and put the 15, 16, 17 first and then the < 15 and > 17 last in the order of event commands.
I don't see screenshots of the messages being shown in the game, so we can't check that for anything that looks odd.

I do, however, think that what @bass2yang suggested is best - there's no need to do all these conditionals when you can simply do the math.

Note that none of that needs to use JavaScript except the clamp() at the end (and even that you could use a couple of Conditional Branches for). As always, I recommend using event commands when possible for maximum comprehension.

Control Variables -> CC 1 AC = 24
Control Variables -> CC 1 DEX = Rand 3-19
Control Variables -> CC 1 AC -= CC 1 DEX
Conditional Branch -> CC 1 AC < 6
- Control Variables -> CC 1 AC = 6
Conditional Branch -> CC 1 AC > 10
- Control Variables -> CC 1 AC = 10
Then I am splicing each number from the array and assigning the number to a Variable.
Array#splice returns an array holding the removed items:
After all, you can splice more than 1 element out at a time. To get the actual value(s), you should index that array; e.g.
JavaScript:
[5,6,7].splice(2, 1)[0];
Explanation:
  1. [5,6,7].splice(2, 1) means "remove 1 entry of [5,6,7], starting at index 2 (third entry)". It returns the array [7], because that's the only value that was removed.

  2. We now have the array [7], but we want the first value of that array. So we index it using the usual notation: array[0]. That's why there's a [0] at the end.
By default the Conditional Branch command uses type-strict checks for its "equals" and "not equals" operators. I.e.
  • Array of 1 number: [14] === 14 will fail, but [14] < 15 will succeed.
  • Number value: 14 === 14 will succeed, as will 14 < 14.

Also, note that these lines are equivalent:

[1,2,3].splice(0, 1)[0] [1,2,3].shift()
@bass2yang Thanks for that explanation and @ATT_Turan thanks for that suggestion!

@caethyril You win the prize for providing the solution! And the prize is Zombie's undying affection and gratitude! Urgh...Argh!

I didn't realize that splice() returns an array. So splicing a value from an array into a Variable is apparently making that new Variable an Array. I don't think I understood that in the past and apparently it wasn't a problem before, but it is a very good thing to know and consider.

So, the simple solution was to convert those 6 splice() commands to $gameVariables.value(542)[0], and [1], and [2], etc. That apparently doesn't make the new Variable an array.

Conditional Branch Issue Solution.png

Once I changed those, the Event works perfectly fine as previously written with the Conditional Branches!

Thanks to everyone who took the time to look at this and to offer assistance.

I learned several new things from this thread so I am very appreciative!

Happy Holidays!
Oh, so you didn't even need to remove those from the array? Yes, referencing the values directly like that is a much neater solution. You're welcome! 👋

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-multiple-conditional-branches.174011/