• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Checking two variables question

W

wkaudio

Guest
Hi! Novice programmer, sorry if this is a silly question.

I have two variables, power_1 and power_2, each of which can hold a value from -1 to 3.

In this example, power_1 = 2 and power_2 = 3. Instead of having two checks, saying if power_1 = 2 && power_2 = 3, and if power_1 = 3 && power_2 = 2, is there an easy way to do one check for a 2 and a 3, in any place? I'm currently slowly working on this problem by having a very large, multi-layered switch/case tree.

The goal is, as long as there is a 2 and a 3, no matter where, we will get the same result.

Sorry if this is not a very clear question.

Thanks for your input :)
 

Slyddar

Member
When you don't care if either one are the value, the product can come in handy.
In your example, you could do
Code:
if (power_1 * power_2 == 6)
but that might not suit what you are after elsewhere. Just happens to work for your example.
 
For this specific issue, you can also simply add the two and check if it equals 5. A more robust solution that doesn't rely on specific situations might be to transfer your values to a ds_list and use ds_list_find_value on both numbers
 

chamaeleon

Member
Hi! Novice programmer, sorry if this is a silly question.

I have two variables, power_1 and power_2, each of which can hold a value from -1 to 3.

In this example, power_1 = 2 and power_2 = 3. Instead of having two checks, saying if power_1 = 2 && power_2 = 3, and if power_1 = 3 && power_2 = 2, is there an easy way to do one check for a 2 and a 3, in any place? I'm currently slowly working on this problem by having a very large, multi-layered switch/case tree.

The goal is, as long as there is a 2 and a 3, no matter where, we will get the same result.

Sorry if this is not a very clear question.

Thanks for your input :)
Using (n+1) to get the valid numbers in the range 0 to 4 instead of -1 to 3.
Code:
var checker = 1<<(2+1) | 1<<(3+1);

for (var power_1 = -1; power_1 <= 3; power_1++) {
    for (var power_2 = -1; power_2 <= 3; power_2++) {
        var combo = 1<<(power_1+1) | 1<<(power_2+1);
        if (checker == combo) {
            show_debug_message("Matched " + string(power_1) + ", " + string(power_2));
        }
    }
}
Code:
Matched 2, 3
Matched 3, 2
 
W

wkaudio

Guest
When you don't care if either one are the value, the product can come in handy.
In your example, you could do
Code:
if (power_1 * power_2 == 6)
but that might not suit what you are after elsewhere. Just happens to work for your example.
For this specific issue, you can also simply add the two and check if it equals 5. A more robust solution that doesn't rely on specific situations might be to transfer your values to a ds_list and use ds_list_find_value on both numbers
Using (n+1) to get the valid numbers in the range 0 to 4 instead of -1 to 3.
Code:
var checker = 1<<(2+1) | 1<<(3+1);

for (var power_1 = -1; power_1 <= 3; power_1++) {
    for (var power_2 = -1; power_2 <= 3; power_2++) {
        var combo = 1<<(power_1+1) | 1<<(power_2+1);
        if (checker == combo) {
            show_debug_message("Matched " + string(power_1) + ", " + string(power_2));
        }
    }
}
Code:
Matched 2, 3
Matched 3, 2
Hi all, thanks for your replies. All of these work, but I'm not sure my problem is solved. I'm sorry for not being clear in my original post. I am trying to write a code that works for any combination of numbers, not just 2 & 3 (and its pair, 3 & 2). I am trying to check for all possible combinations of all integers -1 to 4. @chamaeleon and @stainedofmind, your codes could work. I don't quite understand how this would work with a ds_list (I'm not sure how they would work in the first place), and I will try implementing the n+1 code today.

Thanks again :)
 

chamaeleon

Member
Hi all, thanks for your replies. All of these work, but I'm not sure my problem is solved. I'm sorry for not being clear in my original post. I am trying to write a code that works for any combination of numbers, not just 2 & 3 (and its pair, 3 & 2). I am trying to check for all possible combinations of all integers -1 to 4. @chamaeleon and @stainedofmind, your codes could work. I don't quite understand how this would work with a ds_list (I'm not sure how they would work in the first place), and I will try implementing the n+1 code today.
My loop was just to verify that every combination was tried but only the matching combinations were displayed
Code:
var checker = 1<<(desired_1+1) | 1<<(desired_2+1);
var combo = 1<<(power_1+1) | 1<<(power_2+1);
if (checker == combo) { ... }
Where you get values of desired_1 and desired_2 from (containing the values 2 and 3 in your example), I don't know (and I don't know where the values for power_1 and power_2 comes from either..), but using bit patterns to set "flags", and comparing the resulting bit patterns, it doesn't matter which variable sets which bit, but you end up with the desired result of not needing to care which variable provided which bit to be set. You could presumably have a set of predefined checker variables (or an array of them, etc.) if you have several different patterns of interest and use whichever bit pattern is relevant for some particular piece of code.
 
Top