Re: Inventory help in an RPG game
Posted: Mon Oct 21, 2013 11:02 am
:/ as in, in a bad way.Azhukar wrote:Of course, who doesn't judge others?Pigeon wrote:I feel as though your judging me.
:/ as in, in a bad way.Azhukar wrote:Of course, who doesn't judge others?Pigeon wrote:I feel as though your judging me.
You do keep saying sorry, as if you have done something bad. Maybe it's all in your head.Pigeon wrote::/ as in, in a bad way.
You were already given an example inventory code.Pigeon wrote:Any help now please?
One step further out of your bubble.Pigeon wrote:And now my feelings of reality are slipping, cheers Azhukar
Fine, sure. Thanks. I guess.Azhukar wrote:You were already given an example inventory code.Pigeon wrote:Any help now please?
One step further out of your bubble.Pigeon wrote:And now my feelings of reality are slipping, cheers Azhukar
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 '?'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
The question mark was for show, not for real code.Azhukar wrote:Syntax error: ?:1: <name> or '...' expected near '?'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
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: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
almost everything in the code... + sorry for being rudeAzhukar wrote:Is there something you do not understand about the inventory code? Like say, tables? D: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
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