GMS 2 How can I draw an object only on the surface?

duran can

Member
I am a former game maker user, so I forgot too many techniques in gml. I want to draw an object only in the surface. But that object should be drawn with the entire draw event. Because if the object has draw_text in the draw event, I want it to be drawn.
When I remove the draw event of the object, I cannot draw it. If the object has a draw event, it draws 2 times, so that it is also drawn outside the surface.

How can I solve this? thanks.


1.png

GML:
var r1=c_silver;
var r2=c_gray;
draw_text(15,15,string(global.sc_w[sc_id])+"/"+string(global.sc_h[sc_id]))
draw_rectangle_color(x,y,x+global.sc_w[sc_id],y+global.sc_h[sc_id],r1,r1,r2,r2,1);//surface border
 
 
 surf = surface_create(global.sc_w[sc_id], global.sc_h[sc_id]);
    surface_set_target(surf);
    draw_set_alpha(0.9)
    draw_clear_alpha(c_black, 0);
    draw_circle_color(-x+mouse_x,-y+mouse_y,25,c_red,c_red,0)
    var t=1;
    draw_surface_part_ext(application_surface,x,y,global.sc_w[sc_id],global.sc_h[sc_id],
    0-global.sc_w[sc_id]*t,0-global.sc_h[sc_id]*t,t,t,c_white,1);
    
    //draw all attached objects
    for(var i=0;i<global.sc_num[sc_id];i++){
    event_perform_object(global.sc[sc_id,i], ev_draw, 0);
    }
    
    draw_set_alpha(1)
 surface_reset_target();
draw_surface(surf,x,y);
*note: I can draw it this way, but if the object has a draw circle drawing, I can't draw it. Actually, I want to draw all of them.

GML:
//draw all attached objects
    for(var i=0;i<global.sc_num[sc_id];i++){
    //event_perform_object(global.sc[sc_id,i], ev_draw, 0);
    with(global.sc[sc_id,i]){draw_self();}
    }
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, by calling the event_perform function, the instances are running the draw event and then being forced to run it again, hence the issue with duplicate drawing. You need to simply remove that code, and instead swap it with the code that is in the draw event instead, removing it from the instance and adding a simply comment into the instance draw event to prevent it default drawing.

Also, how you have things there means you are creating a new surface every step, which is a MASSIVE memory leak. Instead your code should be structured like this:

GML:
// CREATE EVENT
surf = -1;

// DRAW EVENT
var r1 = c_silver;
var r2 = c_gray;
draw_text(15,15,string(global.sc_w[sc_id])+"/"+string(global.sc_h[sc_id]))
draw_rectangle_color(x,y,x+global.sc_w[sc_id],y+global.sc_h[sc_id],r1,r1,r2,r2,1);//surface border

if !surface_exists(surf)
{
surf = surface_create(global.sc_w[sc_id], global.sc_h[sc_id]);
}
else
{
surface_set_target(surf);
draw_set_alpha(0.9)
draw_clear_alpha(c_black, 0);
draw_circle_color(-x+mouse_x,-y+mouse_y,25,c_red,c_red,0)
var t=1;
draw_surface_part_ext(application_surface,x,y,global.sc_w[sc_id],global.sc_h[sc_id],0-global.sc_w[sc_id]*t,0-global.sc_h[sc_id]*t,t,t,c_white,1);
//draw all attached objects
for(var i=0;i<global.sc_num[sc_id];i++)
    {
    //add the code for drawing attached objects here.
    //Use a Switch if there are multiple object types
    }
draw_set_alpha(1)
surface_reset_target();
draw_surface(surf,x,y);
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
To answer your original question with some more trivia (which could help others finding this question later idk): setting an object's visible to false will make it not run its draw event automatically. You can still have it draw things yourself whenever you want to (e.g. only after setting the render target to a surface), by having something else that is visible use event_perform() on it, or run a completely unrelated script from its perspective with a with loop during the draw event.
 
Top