-1
\$\begingroup\$

I make a project. I want to make randomize number & draw the number result. So, I make obj_BossText, which has a draw event & executes a code inside the event. This is the code:

randomize();

var ratio = irandom_range(1, 6);
var u7;

u7 = power(ratio, 6);
draw_text(obj_Boss.x, obj_Boss.y,u7);

But, the result is, the text won't stop randomizing & keep changing from one number to another. What is wrong? Do I have to stop the random? But, how? Please explain your code. Thanks

\$\endgroup\$
2
\$\begingroup\$

First of all, the Draw Event is suppose to contain only graphical rendering functions and statements, you are strongly recommended to separate logic from rendering.

Being the Draw Event executed room_speed times per second, it keeps generating random numbers continuously over the time, and that's another reason to move the variable assignment outside from the Draw Event.

I suggest you to edit your obj_Boss having a Create Event and a Draw Event as follow:

// Create Event
randomize();
ratio = irandom_range(1, 6);
u7 = power(ratio, 6);

and:

// Draw Event
draw_text(obj_Boss.x, obj_Boss.y,u7);

This way, your random number ratio and its power u7 will be assigned only once, and you will be displaying the value assigned to them. Also, ratio and u7 aren't local variables anymore but instance variables, thus they are accessible by other instances.

You can even save memory space by removing your variable ratio and perform the computation on one line only:

// Create Event
u7 = power(irandom_range(1, 6),6);

That's a legal statement as first irandom_range() is executed, and its return value is used as argument for function power().

\$\endgroup\$
2
  • \$\begingroup\$ It works! Thanks. But, I have another question. It's about draw_text too. So, maybe you want to check it out here \$\endgroup\$ – Grace Michelle May 27 '16 at 23:15
  • \$\begingroup\$ I will. At least', I'll try :) \$\endgroup\$ – liggiorgio May 29 '16 at 16:18
1
\$\begingroup\$

only run the irandom_range once when you start showing the text and store the result in obj_BossText

\$\endgroup\$
3
  • \$\begingroup\$ I just call it once, do I? \$\endgroup\$ – Grace Michelle May 27 '16 at 12:40
  • \$\begingroup\$ I don't know game maker but I would presume that "draw event" is run every time it has to be drawn. \$\endgroup\$ – ratchet freak May 27 '16 at 12:47
  • \$\begingroup\$ No, I think it isn't \$\endgroup\$ – Grace Michelle May 27 '16 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.