I wanted to make an inventory to my platforming game but I cannot get it to work. I can add stuff to an inventory using table.insert() but then I want the items removed when they are being used. Using table.remove() seems to cause some problems...
The inventory I make is for one-use items with slots and pictures that can be printed on the screen. For now I have only two stats for the items: The inventory placement and the image. Later I plan to add two types of descriptions for each of them.
I have no idea of how to upload a folder so here is a chunk of code directly in the post. It is the only code associated with the issue.
main.lua
Code: Select all
function love.load()
love.graphics.setBackgroundColor( .2, .4, 1 )
invText = love.graphics.newImage("images/inventorytext.png")
Frame = love.graphics.newImage("images/invframe.png")
Penny = love.graphics.newImage("images/coininv.png")
Wood = love.graphics.newImage("images/woodinv.png")
Axe = love.graphics.newImage("images/axeinv.png")
invY, selectinvx, selectinvy, selectinv, invnum = 600, 18, 75, 1, 0
inv = {}
end
function love.draw()
love.graphics.print("Press 'z', 'x' or 'c' to add items to the inventory.", 10, 10)
love.graphics.print("Press 'v' to open and close the inventory.", 10, 40)
love.graphics.setColor( .15, .15, .25, 1 )
love.graphics.rectangle( "fill", 0, invY+3, 800, 300 )
love.graphics.setColor( 1, 1, 1, 1 )
love.graphics.draw( invText, 10, invY+15, 0, 1, 1, 0, 0 )
love.graphics.setColor( 0, 0 , 0, 1 )
love.graphics.rectangle( "fill", 16, invY+73, 68, 68 )
love.graphics.rectangle( "fill", 16, invY+143, 68, 68 )
love.graphics.rectangle( "fill", 86, invY+73, 68, 68)
love.graphics.rectangle( "fill", 86, invY+143, 68, 68)
love.graphics.rectangle( "fill", 156, invY+73, 68, 68)
love.graphics.rectangle( "fill", 156, invY+143, 68, 68)
love.graphics.rectangle( "fill", 226, invY+73, 68, 68)
love.graphics.rectangle( "fill", 226, invY+143, 68, 68)
love.graphics.rectangle( "fill", 296, invY+73, 68, 68)
love.graphics.rectangle( "fill", 296, invY+143, 68, 68)
love.graphics.rectangle( "fill", 366, invY+73, 68, 68)
love.graphics.rectangle( "fill", 366, invY+143, 68, 68)
love.graphics.rectangle( "fill", 436, invY+73, 68, 68)
love.graphics.rectangle( "fill", 436, invY+143, 68, 68)
love.graphics.rectangle( "fill", 506, invY+73, 68, 68)
love.graphics.rectangle( "fill", 506, invY+143, 68, 68)
love.graphics.rectangle( "fill", 576, invY+73, 68, 68)
love.graphics.rectangle( "fill", 576, invY+143, 68, 68)
love.graphics.rectangle( "fill", 646, invY+73, 68, 68)
love.graphics.rectangle( "fill", 646, invY+143, 68, 68)
love.graphics.rectangle( "fill", 716, invY+73, 68, 68)
love.graphics.rectangle( "fill", 716, invY+143, 68, 68)
love.graphics.setColor( .3, .3 , .4, 1 )
love.graphics.rectangle( "fill", 18, invY+75, 64, 64 )
love.graphics.rectangle( "fill", 18, invY+145, 64, 64 )
love.graphics.rectangle( "fill", 88, invY+75, 64, 64)
love.graphics.rectangle( "fill", 88, invY+145, 64, 64)
love.graphics.rectangle( "fill", 158, invY+75, 64, 64)
love.graphics.rectangle( "fill", 158, invY+145, 64, 64)
love.graphics.rectangle( "fill", 228, invY+75, 64, 64)
love.graphics.rectangle( "fill", 228, invY+145, 64, 64)
love.graphics.rectangle( "fill", 298, invY+75, 64, 64)
love.graphics.rectangle( "fill", 298, invY+145, 64, 64)
love.graphics.rectangle( "fill", 368, invY+75, 64, 64)
love.graphics.rectangle( "fill", 368, invY+145, 64, 64)
love.graphics.rectangle( "fill", 438, invY+75, 64, 64)
love.graphics.rectangle( "fill", 438, invY+145, 64, 64)
love.graphics.rectangle( "fill", 508, invY+75, 64, 64)
love.graphics.rectangle( "fill", 508, invY+145, 64, 64)
love.graphics.rectangle( "fill", 578, invY+75, 64, 64)
love.graphics.rectangle( "fill", 578, invY+145, 64, 64)
love.graphics.rectangle( "fill", 648, invY+75, 64, 64)
love.graphics.rectangle( "fill", 648, invY+145, 64, 64)
love.graphics.rectangle( "fill", 718, invY+75, 64, 64)
love.graphics.rectangle( "fill", 718, invY+145, 64, 64)
love.graphics.rectangle( "fill", 330, invY+32, 450, 30 )
love.graphics.setColor( .7, .7, .8, 1 )
love.graphics.rectangle( "fill", selectinvx, invY+selectinvy, 64, 64 )
love.graphics.setColor( 1, 1, 1, 1 )
for k, v in ipairs(inv) do
if v.invpos < 12 then
love.graphics.draw(v.invImg, -52+(v.invpos*70), invy, 0, 1, 1, 0, 0)
invx = -52+(v.invpos*70)
else
love.graphics.draw(v.invImg, -822+(v.invpos*70), invy, 0, 1, 1, 0, 0)
invx = -822+(v.invpos*70)
end
if v.invpos < 12 then invy = invY+75 else invy = invY+145 end
end
love.graphics.print("Press 'x' to use an item", 340, invY+38)
love.graphics.draw( Frame, 0, invY, 0, 1, 1, 0, 0 )
end
function love.update(dt)
if INVENTORY and invY > 380 then
invY = invY - 500*dt
elseif not INVENTORY and invY < 600 then
invY = invY + 500*dt
end
end
function love.focus(bool)
end
function love.keypressed( key, scancode, isrepeat )
if key == 'z' then
invnum = invnum+1
table.insert(inv, invnum, {invImg = Wood, invpos = invnum})
end
if key == 'c' then
invnum = invnum+1
table.insert(inv, invnum, {invImg = Penny, invpos = invnum})
end
if key == 'x' then
invnum = invnum+1
table.insert(inv, invnum, {invImg = Axe, invpos = invnum})
end
if INVENTORY then
if key == 'left' then
if selectinvx < 20 then
selectinvx = 788
if selectinvy < 100 then selectinv = 12 else selectinv = 23 end
end
selectinvx, selectinv = selectinvx-70, selectinv-1
end
if key == 'up' then
if selectinvy > 100 then
selectinvy, selectinv = 75, selectinv-11
end
end
if key == 'right' then
if selectinvx > 710 then
selectinvx = -52
if selectinvy > 100 then selectinv = 11 else selectinv = 0 end
end
selectinvx, selectinv = selectinvx+70, selectinv+1
end
if key == 'down' then
if selectinvy < 100 then
selectinvy, selectinv = 145, selectinv+11
end
end
if key == 'a' then
for k, v in ipairs(inv) do
if v.invImg == Wood and v.invpos == selectinv then
table.remove(inv, selectinv)
invnum = invnum-1
end
end
end
end
if key == 'v' then
if INVENTORY then
INVENTORY = false
else
INVENTORY = true
end
end
end
function love.keyreleased( key, scancode )
end
function love.mousepressed( x, y, button, istouch, presses )
end
function love.mousereleased( x, y, button, istouch, presses )
end
function love.quit()
end