Page 1 of 5

Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:01 am
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 313 times

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:15 am
by jjmafiae
hint: use tables

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:23 am
by Pigeon
jjmafiae wrote:hint: use tables

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

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:38 am
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

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:40 am
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

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:50 am
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.

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:52 am
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.

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:57 am
by jjmafiae
you should try before asking for help, just like i did back in my noob days.

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 10:59 am
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. :(

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:01 am
by Azhukar
Pigeon wrote:I feel as though your judging me. :(
Of course, who doesn't judge others?