Help with inventory

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
JME
Prole
Posts: 7
Joined: Mon Mar 04, 2013 7:49 pm
Location: Gothenburg, Sweden

Help with inventory

Post by JME »

Hi
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
If you understand me or not, that is not my problem.
Ross
Party member
Posts: 100
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Help with inventory

Post by Ross »

What is the problem? You haven't actually said what you want or don't want to happen.

After seeing your code, there's one general point about tables that might be causing you some trouble.

Using table.insert and table.remove with an index in the middle of a list will shift all the subsequent elements in the list. You're storing the "invpos" in each inventory item, but then you may be using table.remove to remove an inventory item from the middle, which will cause the "invpos" of every item after that to be incorrect.

If you want the inventory items to stay in the same place in the list for display purposes, when you remove items you should replace them with some non-nil value that signifies an empty slot in your code. If you replace them with nil that will semi-randomly break iteration with ipairs.
If you want them to stay in a list with no empty slots, then just use table.remove and table.insert as you are, but either don't store the "invpos", or make sure you update each "invpos" every time you add or remove items.

Sorry in advance if this is unnecessary info.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Help with inventory

Post by milon »

JME, it's not possible to (accurately) run your code - it relies on images that you haven't included. I'm not sure what I'm supposed to be seeing or how it's supposed to react. Can you upload a .love, or create a code sample that doesn't require external images?
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help with inventory

Post by darkfrei »

Here is my inventory implementation:
Attachments
2021-03-12T20_54_42-Untitled.png
2021-03-12T20_54_42-Untitled.png (17.66 KiB) Viewed 3033 times
inventory-01.love
(1.01 KiB) Downloaded 152 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest