• 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!

"Simple" Calculator Help

S

Soren11112

Guest
Code:
var num1 = 1;
var num2 = num1*2;
var num3 = num2*2;
var num4 = num3*2;
var num5 = num4*2;
var num6 = num5*2;
var num7 = num6*2;
var num8 = num7*2;
var num9 = num8*2;
var random1 = random_range(1,9);
var random2 = random_range(1,9);
var random3 = random_range(1,9);
var random4 = random_range(1,9);
var random5 = random_range(1,9);
var random6 = random_range(1,9);
var random7 = random_range(1,9);
var random8 = random_range(1,9);
var random9 = random_range(1,9);
var num1row1 = num+random1; 
draw_text(0,0,num1row1);
What I'm trying to do above is make a calculator for a question that a friend posed. My problem is on the second from the bottom row, is there any way I can do something like
Code:
 "num"+random1
and if random1 was 2 it would appear as
Code:
 num2
?
 

jo-thijs

Member
The big question here is, do you want it to appear as num2 as string or as the value the variable num2 contains?
If you want it as string, you would use this line as second to last line:
Code:
var num1row1 = "num" + string(random1);
If you want the value of the variable num2, you should use arrays, it's exactly what they're for.
Your code would look like this:
Code:
var num;
num[0] = 1;
num[1] = num[0] * 2;
num[2] = num[1] * 2;
num[3] = num[2] * 2;
num[4] = num[3] * 2;
num[5] = num[4] * 2;
num[6] = num[5] * 2;
num[7] = num[6] * 2;
num[8] = num[7] * 2;
var random1 = irandom(8);
var random2 = irandom(8);
var random3 = irandom(8);
var random4 = irandom(8);
var random5 = irandom(8);
var random6 = irandom(8);
var random7 = irandom(8);
var random8 = irandom(8);
var random9 = irandom(8);
var num1row1 = num[random1];
draw_text(0, 0, num1row1);
I also went ahead and changed random_range to irandom_range (the former produces decimals, the latter does not)
and I then optimized irandom_range(0, ...) to irandom(...).

I should also note that using "var variableName = ...;" makes the variable local to the event or script.
Furthermore drawing functions such as draw_text have to be used in a draw event if you want them to have any effect (and you're not using surfaces).
If you're using this code in a draw event though, the random numbers will be different every fraction of a second and the drawn text will constantly change,
because the value of random1 constantly changes.
All of the variable code should be put in the create event and every "var" should be removed.
And as a last note, for loops can optimize readability here.
 
S

Soren11112

Guest
The big question here is, do you want it to appear as num2 as string or as the value the variable num2 contains?
If you want it as string, you would use this line as second to last line:
Code:
var num1row1 = "num" + string(random1);
If you want the value of the variable num2, you should use arrays, it's exactly what they're for.
Your code would look like this:
Code:
var num;
num[0] = 1;
num[1] = num[0] * 2;
num[2] = num[1] * 2;
num[3] = num[2] * 2;
num[4] = num[3] * 2;
num[5] = num[4] * 2;
num[6] = num[5] * 2;
num[7] = num[6] * 2;
num[8] = num[7] * 2;
var random1 = irandom(8);
var random2 = irandom(8);
var random3 = irandom(8);
var random4 = irandom(8);
var random5 = irandom(8);
var random6 = irandom(8);
var random7 = irandom(8);
var random8 = irandom(8);
var random9 = irandom(8);
var num1row1 = num[random1];
draw_text(0, 0, num1row1);
I also went ahead and changed random_range to irandom_range (the former produces decimals, the latter does not)
and I then optimized irandom_range(0, ...) to irandom(...).

I should also note that using "var variableName = ...;" makes the variable local to the event or script.
Furthermore drawing functions such as draw_text have to be used in a draw event if you want them to have any effect (and you're not using surfaces).
If you're using this code in a draw event though, the random numbers will be different every fraction of a second and the drawn text will constantly change,
because the value of random1 constantly changes.
All of the variable code should be put in the create event and every "var" should be removed.
And as a last note, for loops can optimize readability here.
Thanks for telling me about irandom but I am intentionally putting this in a step event and I am intentionally logging these variables locally, I am not new to event based scripting, I just didn't know GML supported arrays.
 
Top