Saturday, April 14, 2012

Global table losing data entered in local function scope.... help!

[:1]Hey fellas,

I'll keep this short n sweet. New to addon creation but Ive been tinkering with whatever I look at next in the WOW API list. Trouble is atm Im starting to fiddle with storing lists of Unit GUID (Global Unit IDs) so I can effectively remember anyone Ive had a discrepancy with in the past, and avoid them <3 however...... I declare a global variable at the top of my Lua file, but when I later add to the list, I have debug messages that show its been added, but the minute I leave function scope .. this one being Button1_OnClick(), the minute I click something else, for example another button, I lose everything I did in that previous function, even tho I added the entries into a table thats declared globally.

Can anyone spot what Im doing wrong. BTW I will mention, I have not declared this table as a saved variable so all data is lost everytime I shutdown wow, but I was planning to add that functionality later, could it be this thats messing me up?

Can anyone give me a quick example of writing to a table, declaring a globally saved variable and adding to that table during local function scope like a click event without losing the data? Is there something stupid Im missing here?

Many Thanks Guys!

<3

Gamesguru|||a quick glimpse of some code I was messing with:

--declared globally at top of file before any functions.

t_Gankers = { "Phil", "Dave", "Adam" } -- Container for the IDs of gankers. (temp values debug)



function Button1_OnClick()

local count = #(t_Gankers); -- returns the length of the table (number of entries)

for i = 1, count, 1 do

SendChatMessage( t_Gankers[i] );

end

end



RETURNS:

Phil

Dave

Adam



Later comes:

function Button5_OnClick()

-- No Unit Targetted -- ERROR!

if (UnitName("target") == nil) then

SendChatMessage("No target currently selected.");



else

-- Get num of entries currently in table

local count = #(t_Gankers);



-- Add Global Unit ID to end of gank table

t_Gankers[count + 1] = UnitGUID("target");

count = count +1; --update the count



FontString3:SetText("Saved: " .. count .. " ! " .. "Last: " .. t_Gankers[count] );



-- Send Message to chat window to inform user

SendChatMessage( "Player: " .. UnitName("target") .. " || ID: " .. UnitGUID("target") .. " Tagged!" )



end

end

This all updates and reports fine, but the minute I step out of the function and click say Button1 again..... this goes back to only the 3 originally declared gankers Phil, Dave, and Adam .... even if Ive clicked Button5 1,000 times, thich should in turn give me 1003 gankers on the list. But it doesnt save =(

Help please guys? <3

|||Guys seriously, Ive spent 9 bloody hours looking for a way to import the saved global table back into my addon after relaunch of wow, but nothings happening... always NIL, and I really cant see how the hell this works.... Im so stressed .... please .. someone assist me!!! I BEG YOU!

I have this right at the top before anything else

GNS_T_GANKERS = {}; -- thats my global table

--One thing Im worried about here tho, is that everytime my addon loads, it automatically wipes the damn global??? Am I right in thinking this? If so, how the hell can you EVER declare a global, without it being wiped after every wow launch?

Now next, Ive been delving and I'm trying something like this:

-- Makes a local copy to access the global stored

local g_tGankers = getglobal("GNS_T_GANKERS");

-- A little debugging message of mine to tell me how many *saved* entries I had from last time .... but EVERYTIME it returns NIL, ZERO, NOUGHTA!!!

message("Global Gank returns " .. #(GNS_T_GANKERS));



.... after all my chopping n changing, trying to find ANYWAY at all to get this pile of crap to work, Im slowly but surely falling apart. Its terrifying me knowing I have 6 weeks to get a fully working addon submitted with a 12,000 word dissertation.....

Also, how the hell does setglobal() work? Cos everytime I google the lua function setglobal I get some overly complicated jibberish by some almighty professor who couldnt have made it any harder for a newcomer to understand.

I tell you, truthfully, Ive programmed particle effects simulators in OpenGL and DirectX.... released my own work on GP2X, Gameboy and PS2 platforms ... but never have I been so stressed as I am with this.....

My apologies for my rant ..... but this is breaking my spirit.

I could really use some help..... anyone, ANYTHING .... some respond!

Many Thanks

gamesguru|||Firstly, check out wowwiki.com, old.wowace.com or a couple of other sites that have tutorials on creating basic addons. There is a basic XML tutorial sticky in this forum also.

Secondly, you need to post or pastey a lot more of your code, including the toc and xml files so I can see if you are defining your saved variables properly.

Assuming that your saved variables are defined correctly:


Code:
GNS_T_GANKERS = {};

This is clearing your table every time you log in - not good

What you want is something along the lines of


Code:
if not GNS_T_GANKERS then GNS_T_GANKERS = { }; end;



This is the proper way to use tables, you can (and I sometimes do) use the method you were trying to use, however it does not easily allow for multiple peices of data to be stored per item. Note that you can refer to each entry by its index or contents.

Insert/remove data to/from the table


Code:
table.insert(SavedVariablesName_TableName, "BadGuysName");
table.remove(SavedVariablesName_TableName, index/"BadGuysName")



Get the size of the table


Code:
table.getn(SavedVariablesName_TableName)



Scan table and effect a response


Code:
for index, StoredName in ipairs(SavedVariablesName_TableName) do
if StoredName ~= nil then -- prevents compare w nil error
if StoredName == UnitName("target")
-- Do whatever it is you want to do
end
end
end

If you are still having trouble you could use Bang! AntiSpam 2 as a reference, it uses different types of tables in half a dozen ways.

On another note, why are you using the GUID? You could simply use the player name, or player name and server name.|||Here's a page that every new author should know about:

http://www.wowwiki.com/HOWTOs

Using saved variables is in that list.

No comments:

Post a Comment