Thread Tools Display Modes
09-19-06, 05:13 PM   #1
damptraktor
A Kobold Labourer
Join Date: Sep 2006
Posts: 1
Tables

I'm trying to write a consumable counter/tracker addon and I'm having trouble figuring out how to store different players and their consumables.

Assuming I want to track a limited amount of potion, how would I set this up?

My idea was something like:

Using a function to check if a player is already present in the table, if not create a new table and tinsert it, the playername would be the key and another table would contain the potion info.

Servers are down atm and I'm quite new to lua so I just wanted to ask if this is the right approach.
  Reply With Quote
10-18-06, 07:26 AM   #2
Zeksie
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 24
Originally Posted by damptraktor
I'm trying to write a consumable counter/tracker addon and I'm having trouble figuring out how to store different players and their consumables.

Assuming I want to track a limited amount of potion, how would I set this up?

My idea was something like:

Using a function to check if a player is already present in the table, if not create a new table and tinsert it, the playername would be the key and another table would contain the potion info.

Servers are down atm and I'm quite new to lua so I just wanted to ask if this is the right approach.
Sure.

If you had some function to add an item to a player it could go something like this:

Code:
local myTable = {}
function AddPlayerItem(playerName, itemName, itemCount)

   if (not myTable[playerName]) then
      -- New player, so create an empty table for them under their name
      myTable[playerName] = {}
   end

   -- Assign is as the item count. If you don't care about the count, you can just use 'true' instead
   myTable[playerName][itemName] = itemCount

end

function PlayerHasItem(playerName, itemName)
   if (myTable[playerName]) then
      return myTable[playerName][itemName]
   end
end
For the return value, LUA doesn't care if the index doesn't exist. It'll just return nil if the item is not found. You do have to check that the player exists though, otherwise you might be trying to index a nil value, which is a no-no.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Tables


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off