Inventory help in an RPG game

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.
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Inventory help in an RPG game

Post by Pigeon »

Heyo, it's me again. I've decided to make an inventory system for my RPG, but I have no idea how to do it. I have no current code on the inventory and can't figure it out. Sorry for posting so much, I just need help with the advanced stuff :) (and tables) The .love is below

Thanks in Advance,

Pigeon.
RPG.love
RPG.love
(242.79 KiB) Downloaded 295 times
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Inventory help in an RPG game

Post by jjmafiae »

hint: use tables
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

jjmafiae wrote:hint: use tables

Any help with using those tables? I did say I was terrible with them? Please?
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Inventory help in an RPG game

Post by jjmafiae »

watch sock munkees tuts on how you make tables, you can't make good games without tables!

you can also read this: Lua First Edition
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

jjmafiae wrote:watch sock munkees tuts on how you make tables, you can't make good games without tables!

you can also read this: Lua First Edition
Yeah I understand tables, I honestly do. I'm just not too good. Please, I asked about an inventory, stay on topic maybe? Sorry,

Pigeon
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Inventory help in an RPG game

Post by Azhukar »

Pigeon wrote:Any help with using those tables? I did say I was terrible with them? Please?

Code: Select all

local function addItemToInventory(inventory,item,amount)
	if (inventory[item] ~= nil) then
		inventory[item] = inventory[item] + amount
	else
		inventory[item] = amount
	end
end

local function removeItemFromInventory(inventory,item,amount)
	if (inventory[item] == nil) then return end
	
	if (inventory[item] <= amount) then
		inventory[item] = nil
	else
		inventory[item] = inventory[item] - amount
	end
end

local function printItemsInInventory(inventory)
	for item,amount in pairs(inventory) do
		if (type(item)=="table") then
			print(item.name or "Unknown",amount)
		else
			print(item,amount)
		end
	end
end

local myInventory = {}

local myItem = "hammer"
local myUniqueItem = {name="superhammer",bonus=5}

addItemToInventory(myInventory,myItem,10)
addItemToInventory(myInventory,myUniqueItem,1)
removeItemFromInventory(myInventory,myItem,4)

printItemsInInventory(myInventory)
Pigeon wrote:Yeah I understand tables, I honestly do. I'm just not too good. Please, I asked about an inventory, stay on topic maybe?
You do not understand tables. He was on topic.
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

Azhukar wrote:
Pigeon wrote:Any help with using those tables? I did say I was terrible with them? Please?

Code: Select all

local function addItemToInventory(inventory,item,amount)
	if (inventory[item] ~= nil) then
		inventory[item] = inventory[item] + amount
	else
		inventory[item] = amount
	end
end

local function removeItemFromInventory(inventory,item,amount)
	if (inventory[item] == nil) then return end
	
	if (inventory[item] <= amount) then
		inventory[item] = nil
	else
		inventory[item] = inventory[item] - amount
	end
end

local function printItemsInInventory(inventory)
	for item,amount in pairs(inventory) do
		if (type(item)=="table") then
			print(item.name or "Unknown",amount)
		else
			print(item,amount)
		end
	end
end

local myInventory = {}

local myItem = "hammer"
local myUniqueItem = {name="superhammer",bonus=5}

addItemToInventory(myInventory,myItem,10)
addItemToInventory(myInventory,myUniqueItem,1)
removeItemFromInventory(myInventory,myItem,4)

printItemsInInventory(myInventory)
Pigeon wrote:Yeah I understand tables, I honestly do. I'm just not too good. Please, I asked about an inventory, stay on topic maybe?
You do not understand tables. He was on topic.


Thanks, that's all I needed. :) explanation would have been nice, but I think I understand. Sorry for being such a pain. Plus: I did understand tables, and I did watch SockMunkee's videos. I think I know what I know, if you know what I mean.
Last edited by Pigeon on Mon Oct 21, 2013 10:57 am, edited 1 time in total.
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Inventory help in an RPG game

Post by jjmafiae »

you should try before asking for help, just like i did back in my noob days.
User avatar
Pigeon
Prole
Posts: 29
Joined: Sat Oct 05, 2013 9:37 am

Re: Inventory help in an RPG game

Post by Pigeon »

jjmafiae wrote:you should try before asking for help, just like i did back in my noob days.
I've tried messing about with it, but to no avail. I've tried what I've tried, and I feel as though your judging me. :(
If I seem aggressive, It's because of my head. Not me. Please do the best you can to deal with it, it's not my fault I'm the way I am. :)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Inventory help in an RPG game

Post by Azhukar »

Pigeon wrote:I feel as though your judging me. :(
Of course, who doesn't judge others?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], MrFariator and 156 guests