This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
rkketchup's profile picture

(question) Need help with tabels

Started by rkketchup, 13 January 2013 - 01:43 AM
rkketchup #1
Posted 13 January 2013 - 02:43 AM
I'm trying to make a text-based game, which would use an inventory system where you can pick up and drop items. I would use tables for this but I have no idea how I can make it so when the inventory is opened up, I can type the name of an item and the program will find it in the table and then give me a few options (like "delete" or something). Please help?
remiX #2
Posted 13 January 2013 - 02:48 AM
You would probably need to do something like this


tableOfStuff = {
["Sword"] = {"upgrade", "delete"}, -- Sword is the object name
["Boots"] = {"upgrade", "unequip", "delete"}, -- separate each index between the table with a comma
}

-- To print the options of one:
-- Lets say we have a 'sword' equipped
equippedItem = "Sword"

for i = 1, #tableOfStuff do -- loops through everything inside the tableOfStuff table
	if tableOfStuff[equippedItem] then -- if it finds the item that is equipped
	   for z = 1, #tableOfStuff[equippedItem] do -- loops through the options
		  print(tableOfStuff[equippedItem][z]) -- prints the options... just need to make a menu out of this now.
	   end
	   break
	 end
end

edit: nvm, that way doesn't work, this can:

Spoiler
tableOfStuff = {
[1] = {name = "Sword", options = {"upgrade", "delete"}}, -- Sword is the object name
[2] = {name = "Boots", options = {"upgrade", "delete"}} -- separate each index between the table with a comma
}

-- To print the options of one:
-- Lets say we have a 'sword' equipped
equippedItem = "Sword"

for i = 1, #tableOfStuff do -- loops through everything inside the tableOfStuff table
if tableOfStuff[i].name == equippedItem then -- if it finds the item that is equipped
for z = 1, #tableOfStuff[i].options do -- loops through the options
print(tableOfStuff[i].options[z]) -- prints the options... just need to make a menu out of this now.
end
break
end
end
theoriginalbit #3
Posted 13 January 2013 - 02:52 AM
tables can be used like dictionaries in other languages, that means that you can set a key instead of an index. an example with your use case would be


local inventory = {}
local maxInvSize = 64

local function addToInv( item, ... )
  local options = { ... }
  if #inventory == maxInvSize then
	return false
  end
  inventory[ item ] = options
  return true
end

local function removeFromInv( item  )
  for key, value in pairs( inventory ) do
	if key == item and value >= count then
	  inventory[ key ] = nil
	  return true
	end
  end

  return false
end

local function getActions( item )
  return inventory[ item ]
end

-- usage
addToInv( "Sword", "Throw Away", "Upgrade", "Repair", "Inspect" )
addToInv( "Rock", "Throw Away", "Inspect" )

removeFromInv( "Rock" )

using … in the add means we can have as many actions as we want and it will be added to a table for us :)/>

EDIT: getActions would be used to make the menu
Edited on 13 January 2013 - 01:56 AM