
Thanks in Advance,
Pigeon.
jjmafiae wrote:hint: use tables
Yeah I understand tables, I honestly do. I'm just not too good. Please, I asked about an inventory, stay on topic maybe? Sorry,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
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)
You do not understand tables. He was on topic.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?
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)
You do not understand tables. He was on topic.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?
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.jjmafiae wrote:you should try before asking for help, just like i did back in my noob days.
Of course, who doesn't judge others?Pigeon wrote:I feel as though your judging me.
Users browsing this forum: Bing [Bot], dr.flimflam9 and 4 guests