Monday, April 16, 2012

Saving variables?

Forgive me if this is a stupid question, I'm somewhat new to this.

I'm the loot master for my guild and I hate alt-tabbing to update people's point values mid-raid, but if I don't update immediately, I'd probably forget. So I decided to make an addon which would store the points for me.

So far I have a list of raider's names, each with a point value next to them, and next to that, a button that, when clicked, brings up a new frame with an editbox so I can type in the new point value. I click accept and it updates the point value next to the player. Pretty simple.

My question is, how do I get it to save those point values for next time I log in? I know it is done with saved variables, but how? I guess that I would have to have clicking the accept button update the variables, but how would I go about doing that?

Any assistance would be greatly appreciated by (and my guild, who are well and truly sick of hearing "Wait, don't pull yet, I'm still updating points on the website, I can't heal you.")

Thanks,

Morgain|||No point retyping it:

http://www.wowwiki.com/HOWTO:_Save_V..._Game_Sessions

If you have any specific questions please ask :) .|||Well, that helps, but I'm not sure I totally understand.

What would be the best way to get it to take the value I enter into an EditBox, and replace the default value of the variable, so it saves the new value instead?|||Difficult to answer these 'general' questions about specific cases, without having specific details; But here goes...

Presumably, you are storing your points in a table such as :

PlayerPoints = {};



Edit boxes have an "OnEnterPressed" function so that after you have entered a player's points value and press 'Return', you would presumably execute code similar to the following :

(where playerName is a variable holding the player's name, and playerBox is the Edit box to be displayed)


Code:

local points = tonumber( playerBox:GetText() );

-- if the edit box contained a genuine number
if ( points ) then
PlayerPoints[playerName] = points;
end



Now as long as the table PlayerPoints is declared in your toc file as a SavedVariable then WoW will save the values in this table when you exit the game - REMEMBER that saved variables are only saved when you log out; So looking inside the saved variable file while you are still in the game will not show any of your changes; Exit the game, and THEN look in the saved variable file to see your changes. (Or execute a ReloadUI() inside the game to force an update).



Next time you log in, the PlayerPoints table will hold the same values as it had when you last logged out, and will allow you to use code like this :


Code:

DEFAULT_POINTS_VALUE = 10;

-- I'm explicitly testing for not nil, in order to allow for zero values
-- which can return 'false' negatives for the unwary
if ( PlayerPoints[playerName] ~= nil ) then
playerBox:SetText( PlayerPoints[playerName] );

else
playerBox:SetText( DEFAULT_POINTS_VALUE );
end

playerBox:Show();

No comments:

Post a Comment