why are objects clipping behind each other?

148 views Asked by At

I'm making a script that sorts the depth for my objects by prioritizing the y variable, but then afterwards checks to see if the objects that are touching each other have a higher depth the further to the right they are, but for some reason the last part isn't working.

Here's the code:

ds_grid_sort(_dg,1,true);

_yy = 0;

repeat _inst_num
{
    _inst = _dg[# 0, _yy];
    with _inst
    {
        with other
        {
            if (x > _inst.x and y = _inst.y)
            {
                _inst.depth = depth + building_space;
            }
        }
    
    }
    _yy++;
}

I've identified that the problem is that nothing comes out as true when the game checks the
y = _inst.y part of the _inst statement, but that doesn't make any sense seeing how they're all at the same y coordinate. Could someone please tell me what I'm doing wrong?

2

There are 2 answers

0
Steven On

I think it should be y == _inst.y. But I'm not sure as GML tends to accept such formatting.

It's a better practise to use == to check if they're equal when using conditions.

0
BflySamurai On

As Steven mentioned, it's good practice to use double equal signs for comparisons (y == _inst.y) and a single equals sign for assignments (_yy = 0;), but GML doesn't care if you use a single equals sign for comparison, so it won't be causing your issue. Though it does matter in pretty much every other language besides GML.

From what I understand, the issue seems to be your use of other. When you use the code with other, it doesn't iterate through all other objects, it only grabs one instance. You can test this by running this code and seeing how many debug messages it shows:

...
        with other
        {
        show_debug_message("X: "+string(x)+"; Y: "+string(y));
...

You could use with all. That will iterate through all objects or with object, where object is either an object or parent object. That will iterate through all instances of that object. However, neither of these functions check whether the objects overlap (it's just going to iterate over all of them), so you'll have to check for collisions. You could do something like this:

...
        with all
        {
            if place_meeting(x, y, other)
            {
                if (x > _inst.x and y = _inst.y)
                {
                    _inst.depth = depth + building_space;
                }
            }
...

I don't know what the rest of your code looks like, but there might be an easier way to achieve your goal. Is it possible to initially set the depth based on both the x and y variables? Something such as depth = -x-y;? For people not as familiar with GameMaker, objects with a smaller depth value are drawn above objects with higher depth values; that is why I propose setting the depth to be -x-y. Below is what a view of that grid would look like (first row and column are x and y variables; the other numbers would be the depth of an object at that position):

enter image description here

Having one equation that everything operates on will also make it so that if you have anything moving (such as a player), you can easily and efficiently update their depth to be able to display them correctly relative to all the other objects.