View Full Version : saved variables array
bnelsonjax
01-06-2006, 09:05 AM
for the love of god i cant get saved variables to work correctly. what im trying to do is an array of player names in the savedvariables file.
in my .toc file i have:
## SavedVariables: QStatus, QPlayer
in my lua file in teh addons folder i have:
QPlayer[name] = {}
this thows up an error that says: attempt to index global `QPlayer' (a string value)
what am i doing wrong to make an array of names? is there any documentation that would help me?
Sherkhan
01-06-2006, 04:29 PM
Try:
QPlayer = {} - Defines the array
Then
QPlayer[name] = {}
bnelsonjax
02-06-2006, 07:45 AM
thanks, that worked. Now do you know how i would get it to list this way?
Raid = {
[1] = {
[JohnDoe] = 1123,
[MikeJohns] = 1136,
},
[2] = {
[Brad] = 0116,
[Jim] = 0130,
the [1] = will be raid #1
the [2] will be raid #2
and those should be auto-incremented
[Johndoe] is obviously the Q_Player[name]
the =1123 is the current server time which im using:
timestamp = time();
any idea how to format it this way?
bnelsonjax
02-06-2006, 08:52 AM
thanks, that worked. ok Now I have my variables listed to look like this:
Q_Player = {
[1] = {
["Bakstabb"] = true,
},
[2] = {
["john doe"] = true,
},
}
the [1] = will be the 1st player that whipsered me
the [2] = the 2nd player that whispered me
[Johndoe] is obviously the Q_Player[name]
so my question is what code would i use so that i could print out this list in my chat window to show:
[1] bakstabb
[2] john doe
so that it shows the chronologicall order of people that whipsered me?
im using: table.foreach(Q_Player, QMeUp_List) but its now working
include
02-06-2006, 07:22 PM
hmm... why dont you just save it as
Q_Player = {
[1] = "bakstabb",
[2] = "jon doe",
}
bnelsonjax
03-06-2006, 12:34 AM
What the format to make it save like that?
and if i save it liek that how do i have it print back the results so it comes back:
1. bakstabb
2. john
bottom line is i need to record the order in which they whisper me which is why im saving the chronological numbers, and i need to be able to print the results with the correct order.
bnelsonjax
03-06-2006, 02:33 AM
ok i now have it formatting better. I have it looking like this:
Q_Player = {
[1] = "Doghunter",
[2] = "Bakstabb",
[3] = "brad",
}
however when i print it out, it only does this:
1
2
3
what should my code look like so when i send it to the DefaultChatFrame it would look like this:
1 Doghunter
2 Bakstabb
3 brad
thanks for the help.
BamED
03-06-2006, 08:28 PM
for i, name in ipairs(Q_Player) do
DEFAULT_CHAT_FRAME:AddMessage(i .. " " .. name)
end
bnelsonjax
04-06-2006, 04:17 AM
yep that worked! thanks BamEd, now for a tough one.
How do i remove a name in the Q_Player table?
example if i type /qmu remove bakstabb what code would i use to have it remove the user bakstabb?
Thanks again for the help guys
BamED
04-06-2006, 06:17 AM
-- variable 'name' contains value to remove
local i = table.foreachi(Q_Player, function(i, v) if v==name then return i end end)
if i then
table.remove(Q_Player, i)
end
bnelsonjax
04-06-2006, 06:29 AM
BamEd, do you have aim or something i could chat with you on? you seem to know your stuff and i have a few more questions that you may be able to help. I promise i would not take up much of your time.
BamED
04-06-2006, 07:03 AM
No, don't really use AIM or similar, sorry. I prefer using forums for two reasons. First, it allows me to answer when/if I have time. Second, other people may learn something from the answers. :smiley:
So go ahead and post your questions. I'll answer if I can and have time. Otherwise, I'm sure someone else will.
bnelsonjax
04-06-2006, 07:30 AM
my question is when i type /qmu remove bakstabb, how do i tell wow that name = bakstabb? heres my code for removing a player via name:
function QMeUp_Handler(msg)
if (msg == "remove") then
local i = table.foreachi(Q_Player, function(i, v) if v==name then return i end end)
if i then
table.remove(Q_Player, i)
DEFAULT_CHAT_FRAME:AddMessage(i.."removed");
end
BamED
04-06-2006, 08:47 AM
I think the msg variable will contain "remove backstabb". So you will have to parse it. I suggest making a general function for parsing the command string:
local function parseCommand(msg)
local arr = {}
for x in string.gfind(msg, "%S+") do
table.insert(arr, x)
end
return arr
end
Then you can use it like this:
function QMeUp_Handler(msg)
local args = parseCommand(msg)
if (args[1] == "remove") then
local name = args[2]
-- remove name
elseif (args[1] == "someOtherCommand") then
-- do something else
elseif (args[1] == "yetAnotherCommand") then
-- you get the picture :)
end
end
bnelsonjax
04-06-2006, 09:19 AM
ya that will work, thanks again bud.
one last thing im curious as how you would handle a search in the table to see if a name exists. Example. if someone whispers me it adds thier name to the Q_Player tables, if they whipser me again, i want it to say they are already added to the table.
so im using:
for var,i in Q_Player do
if(text == "q") then
if (i == name) then
DEFAULT_CHAT_FRAME:AddMessage(name.." is already in the list")
SendChatMessage("You are already in the Bloodlust Raid Queue.", "WHISPER", this.language, name)
else
table.insert(Q_Player, name)
SendChatMessage("you are now placed in the Bloodlust Raid Queue, Please wait for a spot.", "WHISPER", this.language, name)
DEFAULT_CHAT_FRAME:AddMessage(name.." has been added to the queue")
end
end
end
is this wrong?
BamED
04-06-2006, 09:42 AM
Look at the code I showed for removing a name. The first part searches for it and returns the index or nil if it's not there:
table.foreachi(Q_Player, function(i, v) if v==name then return i end end)
You can use a for-loop too, of course:
local function findValue(t, v)
for i, x in ipairs(t) do
if x == v then return i end
end
end
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.