GML [SOLVED] General question - Should I split code between step and draw event?

MartinK12

Member
Many times I have to change variables and sprites according to some other variable.
In example:
Code:
if clicked {
var1 = 1;
var2 = 2;
draw_sprite...
image_index...
image_speed..
}

Should I split this into step and draw event like below so check clicked both in step and draw event?
In step event:
Code:
if clicked {
var1 = 1;
var2 = 2;
}
In draw event:
Code:
if clicked {
draw_sprite...
image_index...
image_speed..
}
Or should I put all that code in the draw event? Or any other solution? The same goes with switch statement which are much bigger. What is the best practice?
Thank You for your answer.
 

chamaeleon

Member
Many times I have to change variables and sprites according to some other variable.
In example:
Code:
if clicked {
var1 = 1;
var2 = 2;
draw_sprite...
image_index...
image_speed..
}

Should I split this into step and draw event like below so check clicked both in step and draw event?
In step event:
Code:
if clicked {
var1 = 1;
var2 = 2;
}
In draw event:
Code:
if clicked {
draw_sprite...
image_index...
image_speed..
}
Or should I put all that code in the draw event? Or any other solution? The same goes with switch statement which are much bigger. What is the best practice?
Thank You for your answer.
My recommendation is to manage state information (including the setting of sprites and sprite indexes) in step events, and keep draw events for the purpose of doing drawing only. If things get "too big", consider creating scripts with meaningful names to reduce clutter.
 
I

icuurd12b42

Guest
the draw event is for drawing only... where you draw things according to various states. there is no issues using logic to figure out what to draw.
 

MartinK12

Member
Thank You for replies guys. Ok, so I understand that every time I’ll have to draw sth I’ll have to create same/similar logic both in Draw and Step event

One more question out of simple curiosity - why can’t I use only Draw event then? From my understanding Draw event is done by GPU (not CPU) and GPU is faster than CPU so wouldn’t it give better game performance?
 
I

icuurd12b42

Guest
the draw event is there for you to used code that will draw things. it's not "done by the gpu". the code submits draw instructions which game maker batches in a list of vertices and instructions that will be drawn by the gpu…

typically you only have draw related code in there because you may at one point decide to skip drawing for some frames, or decide to call the draw event yourself multiple time to draw the content to a surface or whatnot. if you have any code that would, say, perform movement, or check if the user clicked the mouse on a gui button... the movement would be affected, movement running slower than intended, or missing UI clicks, in the case of frame skipping...
 
Top