Making a simple platformer, ran into some issues

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
retrotails
Party member
Posts: 212
Joined: Wed Apr 18, 2012 12:37 am

Making a simple platformer, ran into some issues

Post by retrotails »

I'm quite new to programming, this is my first time working with Lua, I just have a bit of experience with VB - by which I mean no more then the spam bot type stuff you'd expect. So don't expect anything clean or efficient, much less both. Anyway, my issue isn't so much of an issue as it is an optimization that doesn't work for some reason - love.graphics.newQuad shouldn't be called every single frame. I tried that on my netbook... just no. But I can't figure out how to make it only update on block changes. I have a boolean, newQuads, that is set to true for the first frame. It gets set to true if there's a block change and false at the end of love.draw(). If it's true, it sets up new quads with love.graphics.newQuad and renders them with love.graphics.drawq. If it's false, it just does love.graphics.drawq. I have a 3D table named quad_tiles with the tile layer, X axis, and Y axis respectively. I did this in a giant loop

Code: Select all

quad_tiles[k][i][j] = love.graphics.newQuad((string.byte(world_blocks[k][i][j]))*16,0,16,16,1024,16)
But for some reason, that strip of code has to be done every single frame, or else it crashes. Change line 47 of world_draw.lua to newQuads = false to reset it to false every frame to see what I mean. If you do that and hold left click in the top left corner somewhere immediately after opening the program, it'll constantly be placing a block and thus set newQuads to true every frame, so it won't crash until you let go of the left click.

A few things about the program:
It's not programmed well at all.
Up and down change layers, left and right pick blocks. Left click places, right removes. All blocks are placeable on all layers, and the collision layer will later have it's own unique gfx, with possibilities such as solid, swimmable, hurts, kills, and so on, which obviously won't get rendered in the game itself.
The level format is kinda weird, it's converted from 1-1.rtf to a table, but that's not always what's used. Here are some tips about world_data[x]:

Code: Select all

world_data[x]:  ¦ Description
----------------------------------
1               ¦   Background tile data
2               ¦   Mainground tile data
3               ¦   Foreground tile data
4               ¦   Collision tile data
5               ¦   Map size X, in blocks
6               ¦   Map size Y, in blocks
7               ¦   Player starting block X
8               ¦   Player starting block Y
9               ¦   Background image file (as in, the full-size background, not the tiles)
10              ¦   Tile image file (as in, the tiles)
11              ¦   Width of tile set, in blocks (so the image size X / 16)
12              ¦   If 1 then background is static with blocks, if 2 it moves a bit (might have to be tilable)
I'm not 100% sure if that's accurate, (and not all values do anything yet, and I need to add more, like the music location which is hard coded atm) I have changed the format a bit several times. Also, string.byte(255) is the separator. The map was made in a hex editor, which explains the insanely small 4x4 placement grid, but it can be made into any size - I just haven't made a level creator just yet. You can change the BG tile data in a hex editor to 256 letters each and set the map size X and Y from 4 to 16 (I haven't tested different map sizes yet though.)
Finally, a few random things -
The IT module doesn't loop, I don't know how to get it to, I don't think B00 works.
esc quits, but now in 0.8.0 it throws an error, how do I make it quit now?
It used to have a frame limiter of 30 FPS, frame skipping is way beyond me, how do I implement that in 0.8.0 since the example on the wiki doesn't work?
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: Making a simple platformer, ran into some issues

Post by iemfi »

The world_load function was all weird, the structure of the table it was setting up was completely different!

Changed it to the following and it won't crash anymore. As for updating tiles just set the tile to something else on mouse click, no need to loop or touch any other tiles.

The world_data[x] system is also weird, using something like world_data.mapY instead of world_data[5] would help readability a lot.

And you can just set the graphics mode to wait for vsync to limit it to 60 fps.

Code: Select all

   for k = 1,3,1 do
    quad_tiles[k] = {}
        for i = 1,world_data[5],1 do
                        quad_tiles[k][i] = {}
            for j = 1,world_data[6],1 do
                quad_tiles[k][i][j] = love.graphics.newQuad((string.byte(world_blocks[k][i][j]))*16,0,16,16,1024,16)
                love.graphics.drawq(gfx_tiles,quad_tiles[k][i][j],i*16,j*16,0,1,1,16,16)
            end
        end
    end

world_draw()
        for k = 1,3,1 do
            for i = 1,world_data[5],1 do
                for j = 1,world_data[6],1 do
                    love.graphics.drawq(gfx_tiles,quad_tiles[k][i][j],i*16,j*16,0,1,1,16,16)
                end
            end
        end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Making a simple platformer, ran into some issues

Post by Nixola »

Vsync won't work on every computer
The example in the wiki is for 0.7.2, I think, you'd have to divide by 1000 the 'sleep' amount to make it work 'cause the example uses milliseconds while 0.8.0 uses seconds.
Quitting in love 0.8.8 is either love.event.push('quit') or love.event.quit()
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
retrotails
Party member
Posts: 212
Joined: Wed Apr 18, 2012 12:37 am

Re: Making a simple platformer, ran into some issues

Post by retrotails »

iemfi wrote:The world_load function was all weird, the structure of the table it was setting up was completely different!

Changed it to the following and it won't crash anymore. As for updating tiles just set the tile to something else on mouse click, no need to loop or touch any other tiles.

The world_data[x] system is also weird, using something like world_data.mapY instead of world_data[5] would help readability a lot.
How is world_load weird exactly?
When you say "just set the tile to something else on mouse click" what do you mean?
I'd like to use something like "world_data.mapY", but coming straight from my little experience with VB.NET, I just know arrays, not tables, and then I think the loop would be weird since it'd have to check for the number and put it in a different variable. I don't know Lua very well...
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests