(SOLVED) Lag-free depth system

Discussion in 'Programming' started by ZombieSquirrel, Sep 13, 2019.

  1. ZombieSquirrel

    ZombieSquirrel Member

    Joined:
    Oct 18, 2018
    Posts:
    80
    Hi everyone.
    I've been using a depth system that works quite nice.
    The way it works, is it stores all the objects in the current room in a grid,
    using each individual object's x and y value.
    The grid is sorted each frame according to these values. When the player moves,
    each frame, the entire grid gets sorted.
    In the depth object, the draw event of the object's are being controlled (using event_perform(ev_draw,0))
    and they only draw after being sorted.

    This works great, however, once I get a lot of objects in the room, i experience massive lag.
    The reason I think is two-fold:
    1) The many objects cause the depthgrid to be very large. Completely sorting this amount of rows in the grid each frame causes lag.
    2) And also considering the large amount of objects, each one having its draw event being controlled externally by a depth controller object, could also be laggy.

    The depth object I use is a variant of Friendlycosmonaut on YouTube. The code goes like this;


    //Grid Logic
    var instancenumber = instance_number(OBJ_DepthSystem_PAR);
    //(OBJ_DepthSystem_PAR is the parent object of every object that needs depth)

    var dsgrid = DepthGrid;
    if (ds_grid_height(DepthGrid) != instancenumber)
    {ds_grid_resize(dsgrid, 2, instancenumber);}
    var yy = 0;
    with(OBJ_DepthSystem_PAR)
    {
    dsgrid[# 0, yy] = id;
    dsgrid[# 1, yy] = ((y-id.depthspecific)*room_width) + x;
    yy += 1;
    }

    //The next line works, but causes massive lag:
    ds_grid_sort(dsgrid, 1, true);

    //Override draw events of each object and draw them in the correct order:
    var inst;
    var zz = 0;
    repeat(instancenumber)
    {
    //Pull out id
    inst = dsgrid[# 0, zz];
    //Draw yourself
    with(inst)
    {
    event_perform(ev_draw,0);
    }
    zz +=1;
    }

    A quick note: The "depthspecific" variable is just something I put in every object that needs specific depth. Don't pay too much attention to that, it just means that some sprites might have to "start their depth" at 2 pixels from the bottom, or 3, or 1,...


    So my questions are:
    How to solve the massive lag when the depthgrid gets really large?
    Also, depth = -y does not do what I want to do.
    Are there other depth systems that are much more friendly towards processing power?
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    954
    You could see if MirthCastle's depth sorting system is suitable for your purpose.

    https://forum.yoyogames.com/index.p...d-for-gms2-objects-sprites.42868/#post-398498

    My poor memory seems to tell me it relies essentially on a sufficient number of y coordinate layers. That is, there's a lookup table of layers for y, and when y changes for an instance you set the layer for the instance to the layer corresponding to the new y coordinate. Given that the system relies on the layer system there is no sorting taking place in your own code.
     
    ZombieSquirrel likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice