Page 2 of 5

Re: Inventory help in an RPG game

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

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:03 am
by Azhukar
Pigeon wrote::/ as in, in a bad way.
You do keep saying sorry, as if you have done something bad. Maybe it's all in your head.

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:05 am
by Pigeon
So I read through the Programming in Lua (the tables bit) and watch SockMunkee's tutorials. I have attempted at making an Inventory and it kinda failed. I've done all you asked. Any help now please? ;) And now my feelings of reality are slipping, cheers Azhukar

Cheers,
Pigeon

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:12 am
by Azhukar
Pigeon wrote:Any help now please? ;)
You were already given an example inventory code.
Pigeon wrote:And now my feelings of reality are slipping, cheers Azhukar
One step further out of your bubble. :)

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:15 am
by Pigeon
Azhukar wrote:
Pigeon wrote:Any help now please? ;)
You were already given an example inventory code.
Pigeon wrote:And now my feelings of reality are slipping, cheers Azhukar
One step further out of your bubble. :)
Fine, sure. Thanks. I guess.

Code: Select all

function time:wasted(?)
    if time == "wasted" then
    	log_off_lua = true
    	curse_timewasters_name = true
    	pissed = true
    end
end

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:17 am
by Azhukar
Pigeon wrote:Fine, sure. Thanks. I guess.

Code: Select all

function time:wasted(?)
    if time == "wasted" then
    	log_off_lua = true
    	curse_timewasters_name = true
    	pissed = true
    end
end
Syntax error: ?:1: <name> or '...' expected near '?'

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:20 am
by Pigeon
Azhukar wrote:
Pigeon wrote:Fine, sure. Thanks. I guess.

Code: Select all

function time:wasted(?)
    if time == "wasted" then
    	log_off_lua = true
    	curse_timewasters_name = true
    	pissed = true
    end
end
Syntax error: ?:1: <name> or '...' expected near '?'
The question mark was for show, not for real code.

Code: Select all

function time:wastedevenmore()
	if time == "wastedevenmore" then
		thoughts = "f*ck off, all I asked for was a bit of help"
	end
end

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:24 am
by Azhukar
Pigeon wrote:The question mark was for show, not for real code.

Code: Select all

function time:wastedevenmore()
	if time == "wastedevenmore" then
		thoughts = "f*ck off, all I asked for was a bit of help"
	end
end
Is there something you do not understand about the inventory code? Like say, tables? D:

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:26 am
by Pigeon
Azhukar wrote:
Pigeon wrote:The question mark was for show, not for real code.

Code: Select all

function time:wastedevenmore()
	if time == "wastedevenmore" then
		thoughts = "f*ck off, all I asked for was a bit of help"
	end
end
Is there something you do not understand about the inventory code? Like say, tables? D:
almost everything in the code... + sorry for being rude

Re: Inventory help in an RPG game

Posted: Mon Oct 21, 2013 11:37 am
by Azhukar
Pigeon wrote:almost everything in the code... + sorry for being rude

Code: Select all

local function addItemToInventory(inventory,item,amount)
	if (inventory[item] ~= nil) then  --there are items like this in the inventory already
		inventory[item] = inventory[item] + amount --so we add the amount to the ones already there
	else --there is no item like this in the inventory
		inventory[item] = amount --so the new amount of that item is the one we're adding
	end
end

local function removeItemFromInventory(inventory,item,amount)
	if (inventory[item] == nil) then return end --there are no such items in the inventory, we do nothing
	
	if (inventory[item] <= amount) then --we're removing more of such items than there are in the inventory
		inventory[item] = nil --so we will have no items like it afterwards
	else --there is more of such items than we are removing
		inventory[item] = inventory[item] - amount --so we subtract the amount we're removing
	end
end

local function printItemsInInventory(inventory)
	for item,amount in pairs(inventory) do --we're traversing the table, table keys are items, table values are amounts of items
		if (type(item)=="table") then --complicated items are represented by a table
			print(item.name or "Unknown",amount) --if item has a name we use that, otherwise we use "Unknown" for printing
		else --it's a simple item represented by a string
			print(item,amount) --we print the item and how much there is of it
		end
	end
end

local myInventory = {} --our new inventory is a simple table

local myItem = "hammer" --simple item will be a string
local myUniqueItem = {name="superhammer",bonus=5} --special item will be a table with anything we like in it

addItemToInventory(myInventory,myItem,10) --we add 10 hammers to inventory
addItemToInventory(myInventory,myUniqueItem,1) --we add 1 superhammer to inventory
removeItemFromInventory(myInventory,myItem,4) --we remove 4 hammers from inventory

printItemsInInventory(myInventory) --we print contents of the inventory