GameMaker (Solved.) Simplify this code? Help.

Gasil

Member
Hello, I'm trying to figure out a dinamic way to accomplish the following.

maxVoli = 2; (This variable starts at 2 and will be added + 1 several times in the course of the game.)

Code:
    if(maxVoli >= 2) and (maxVoli <= 6)
        pag = 0;
    if(maxVoli >= 6) and (maxVoli <= 10)
        pag = 1;
    if(maxVoli >= 10) and (maxVoli <= 14)
        pag = 2;
    if(maxVoli >= 14) and (maxVoli <= 18)
        pag = 3;
etc

I guess I can amateurly make as much if statements as I need but I think maxVoli will end up with a very big number D':

How do you think I should approach this? With a loop or maybe a formula?

Thank you, please let me know if I didn't explain myself well enough.
 
Just offhand, it looks like:

pag = (maxVoli - 2) / 4;

Edit: Forgot to floor the result, like others mentioned.
 
Last edited:
L

Ludo Design

Guest
Simply use the mathematical expression y = mx + b. Since you want integer results, round down the result.

edit: updated to match later specified intended results
Code:
pag = floor( ( maxVoli - 2 ) / 5 );
 
Last edited by a moderator:
You could use a formula. It looks roughly (but not exactly as I will explain in a moment) to me like this is the pattern you are using (if the pattern is consistent over all values of maxVoli :

pag = floor((maxVoli - 2) / 5)

However, there is a small issue. You have here:

Code:
if(maxVoli >= 2) and (maxVoli <= 6)
       pag = 0;
   if(maxVoli >= 6) and (maxVoli <= 10)
       pag = 1;
So if maxVoli is equal to 6, do you want it pag to equal 0 or 1, because both conditions are true for maxVoli = 6.
 

Gasil

Member
You could use a formula. It looks roughly (but not exactly as I will explain in a moment) to me like this is the pattern you are using (if the pattern is consistent over all values of maxVoli :

pag = floor((maxVoli - 2) / 5)

However, there is a small issue. You have here:

Code:
if(maxVoli >= 2) and (maxVoli <= 6)
       pag = 0;
   if(maxVoli >= 6) and (maxVoli <= 10)
       pag = 1;
So if maxVoli is equal to 6, do you want it pag to equal 0 or 1, because both conditions are true for maxVoli = 6.
Holy crap you're right. If it is 6 I need it to make pag = 0 still. I guess I tried to achieve this I think?

if(maxVoli > 2) and (maxVoli < 7)
pag = 0;

In any case your formula is the one I need, I was trying to divide by 4 and the result was being short one number, I wanted this pattern:

maxVoli - 2, 7, 12, 17 ...
pag - 0, 1, 2, 3

I will run the game to see if it does what I'm looking for, thank you very much <3
 
L

Ludo Design

Guest
IndianaBones is also making a good point. Your use of <= indicates that you could use some familiarity with number ranges. I'll give a quick explanation:

Your original code effectively actually works like this:

Code:
    if(maxVoli >= 2) and (maxVoli <= 5)
        pag = 0;
    if(maxVoli >= 6) and (maxVoli <= 9)
        pag = 1;
    if(maxVoli >= 10) and (maxVoli <= 13)
        pag = 2;
    if(maxVoli >= 14) and (maxVoli <= 18)
        pag = 3;
Let's say that your maxVoli is 6. What happens is that the first comparison is matched, and pag is set to 0. But then, in the next line, maxVoli is also equal to 6, so that comparison matches in that if statement, too, so pag becomes overwritten with 1.

If I were to say "think of a number between 1 and 10," there are two interpretations for this.
  • I am including the numbers 1 and 10. In this case, I would say if( i >= 1 && i <= 10 ), so 1 2 3 4 5 6 7 8 9 10 will work.
  • I am not including the numbers 1 and 10. In this case, I would say if( i > 1 && i < 10 ), so 2 3 4 5 6 7 8 9 will work, but 1 and 10 will not.
 

Gasil

Member
IndianaBones is also making a good point. Your use of <= indicates that you could use some familiarity with number ranges. I'll give a quick explanation:

Your original code effectively actually works like this:

Code:
    if(maxVoli >= 2) and (maxVoli <= 5)
        pag = 0;
    if(maxVoli >= 6) and (maxVoli <= 9)
        pag = 1;
    if(maxVoli >= 10) and (maxVoli <= 13)
        pag = 2;
    if(maxVoli >= 14) and (maxVoli <= 18)
        pag = 3;
Let's say that your maxVoli is 6. What happens is that the first comparison is matched, and pag is set to 0. But then, in the next line, maxVoli is also equal to 6, so that comparison matches in that if statement, too, so pag becomes overwritten with 1.

If I were to say "think of a number between 1 and 10," there are two interpretations for this.
  • I am including the numbers 1 and 10. In this case, I would say if( i >= 1 && i <= 10 ), so 1 2 3 4 5 6 7 8 9 10 will work.
  • I am not including the numbers 1 and 10. In this case, I would say if( i > 1 && i < 10 ), so 2 3 4 5 6 7 8 9 will work, but 1 and 10 will not.
I see now, it makes so much sense now. Thank you so much for taking the time calrifying it for me.
 
Top