Page 1 of 1

(SOLVED) Z-Ordering graphical issues.

Posted: Tue Jul 21, 2015 8:35 pm
by sam
I'm attempting to Z-order my entities for my game, but the function that I supplied to table.sort seems to be shifting the entity table in some weird way, making the entities onscreen flash.

Just for clarification, my ents table holds every entity in the game (each one has their own z variable) and is then cycled through to check if the entities have draw functions. If they do, it draws them. The bug seems to be that their drawing function doesn't call properly due to table.sort.

Image
This gif shows my game without any z-ordering. Everything stays stable and never disappears. The bullets have a lifespan, so after a certain amount of frames, they're removed from the ents table, which ceases to draw and update it.

Image
This shows my game with the z-ordering function enabled, sorting the table in a way that makes the entities flash. It seems to do it mostly when the entities delete themselves off-screen.

Here's my draw function, complete with the function I also use for z-ordering.

Code: Select all

draw = function(self, cam)
    for k, v in pairs(ents) do
      cam:draw(function()
        if v.draw ~= nil then
          return v:draw()
        end
      end)
    end
    if debug.debugMode and debug.debugShow then
      love.graphics.setColor(255, 255, 255, 255)
      love.graphics.print("FPS: " .. love.timer.getFPS(), 16, 16)
      love.graphics.print("Number of entities: " .. entAmt, 16, 32)
      return love.graphics.print("Current room: " .. room, 16, 48)
    end
  end
  drawSort = function(a, b)
    if a and b then
      return a.z > b.z
    end
  end
If you want a better idea of what I have, I attached my .love file (if you're brave enough to go into moonscript-compiled lua code). What you want to do when you open the game is to press 0 to go to the debug room (where the gifs above take place), and then press 'p' to open the text menu that you see flashing in the gifs

The drawSort and draw functions are located in lib\mad\madlib.lua. This is the file that houses the library/engine thing I've been working on for the past few months in my spare time.

Thanks for the help!

Re: Z-Ordering graphical issues.

Posted: Tue Jul 21, 2015 10:55 pm
by BOT-Brad
I've taken a quick look at your code, and you seem to be sorting your entities (ents) every draw call.
Do your entities change 'z' coordinates often? If they never change, it would be much better to sort the entities by their z-coordinates whenever one is added, removed, or modified.

I actually don't get that flickering effect seen in your gif, so I am not sure what the issue is outside of that.

Nevermind, got it to happen. If you move to the top of the screen and fire, you never spawn bullets/bombs after that, but your little guy still recoils as he 'shoots'. Something is wrong, I'll take another look tomorrow!

Re: Z-Ordering graphical issues.

Posted: Tue Jul 21, 2015 11:10 pm
by sam
BOT-Brad wrote:I've taken a quick look at your code, and you seem to be sorting your entities (ents) every draw call.
Do your entities change 'z' coordinates often? If they never change, it would be much better to sort the entities by their z-coordinates whenever one is added, removed, or modified.

I actually don't get that flickering effect seen in your gif, so I am not sure what the issue is outside of that.

Nevermind, got it to happen. If you move to the top of the screen and fire, you never spawn bullets/bombs after that, but your little guy still recoils as he 'shoots'. Something is wrong, I'll take another look tomorrow!
The Z coordinate is set to a negative version of the player's Y coordinate every frame.
Now that I look at it, I think the bullets are still spawning, but not drawing because of the same bug that's affecting the menu (I don't have any code in place that deletes the bullets the farther they go up on the screen).
Hope this helps, thanks for taking a look!

Re: Z-Ordering graphical issues.

Posted: Fri Jul 24, 2015 12:25 am
by sam
Sorry for double-posting, but I'd really love to get this fixed.
I've attached an updated version of the game to this post, this time with better graphics/bullets and more importantly the ability to toggle Z-ordering on and off with the 0 key, showing the issues I'm having. If anyone knows a better way to fix this, I'd be super happy. (Again, press 0 in the main menu to go to the debug room, and press P to turn on the menu).
As you can see, the bullets are still spawning even when Z-ordering is off. I suspect the drawSort function is messing with the table in some way.

Re: Z-Ordering graphical issues.

Posted: Fri Jul 24, 2015 9:12 pm
by BOT-Brad
Did you write madlib yourself?
I think I might have solved it....

In madlib.lua, this is your remove function;

Code: Select all

for k, v in pairs(ents) do
    if v == ent then
        if v.destroy ~= nil then
            ent:destroy()
        end
        ents[k] = nil
        entAmt = entAmt - 1
        if debug.debugMode then
            print("Removed ent -> " .. ent.name, v)
        end
    end
end
I'm not entirely sure why the whole display flickers occasionally, but it's certainly something to do with making the array have gaps, e.g. [ent, ent, ent, NIL, ent, ent], which is throwing off your drawing functions and making the array appear 'empty' and hence not drawing anything.

Code: Select all

for i = entAmt, 1, -1 do
    local v = ents[i]
        if v == ent then
            if v.destroy then v:destroy() end
            table.remove(ents, i)
            entAmt = entAmt - 1
            if debug.debugMode then
                print("Removed ent -> " .. ent.name, v)
            end
        end
    end
end
This function above works fine (for me at least) and I didn't notice any bugs/problems with it.

Also, I would suggest changing your frequent usage of pairs(ents) in madlib to ipairs(ents) instead, or even a straight for-loop (like in my example above). You get the key value when using pairs, which will be the array index. However, I believe pairs can sometimes return values in an unspecified order, which can be quite annoying and lead to minor bugs like this.

Re: Z-Ordering graphical issues.

Posted: Sat Jul 25, 2015 5:03 am
by sam
BOT-Brad wrote:Did you write madlib yourself?
I think I might have solved it....

In madlib.lua, this is your remove function;

Code: Select all

for k, v in pairs(ents) do
    if v == ent then
        if v.destroy ~= nil then
            ent:destroy()
        end
        ents[k] = nil
        entAmt = entAmt - 1
        if debug.debugMode then
            print("Removed ent -> " .. ent.name, v)
        end
    end
end
I'm not entirely sure why the whole display flickers occasionally, but it's certainly something to do with making the array have gaps, e.g. [ent, ent, ent, NIL, ent, ent], which is throwing off your drawing functions and making the array appear 'empty' and hence not drawing anything.

Code: Select all

for i = entAmt, 1, -1 do
    local v = ents[i]
        if v == ent then
            if v.destroy then v:destroy() end
            table.remove(ents, i)
            entAmt = entAmt - 1
            if debug.debugMode then
                print("Removed ent -> " .. ent.name, v)
            end
        end
    end
end
This function above works fine (for me at least) and I didn't notice any bugs/problems with it.

Also, I would suggest changing your frequent usage of pairs(ents) in madlib to ipairs(ents) instead, or even a straight for-loop (like in my example above). You get the key value when using pairs, which will be the array index. However, I believe pairs can sometimes return values in an unspecified order, which can be quite annoying and lead to minor bugs like this.

Thanks, man! This worked great. I'll be sure to take your comment on loops into consideration when I run into errors like this.
Also yeah, Madlib was my personal summer project. It was to make a framework or library on top of Love that would handle things like entity control and a room system. with Game Maker as my inspiration (and where I came from before Love). It's pretty messy in it's current state and still lacks some things, but I like the way it's turned out so far.