Reply
Thread Tools Display Modes
Unread 06-13-11, 06:14 AM   #1
Noax
Zombie
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 1
How to: save variables between gaming sessions.

Hello dear fellow addon developers / riftui community.

As per request a copy of my post @ forums.riftgame.com

Some of you might already know how this works but as i could not find any info relating this subject on the forums i thought i just bring it up! I'll explain it using examples as that makes it easier for me to explain and hopefully easier for you guys to understand.

If i missed out on any info or some of the info might be faulty then please add to / correct it. I'll try to keep the first post as up to date as posible!

Breakdown of the topics:
- What are saved variables for?
- What is the downside of saved variables?
- How do we make these saved variables?
- How do we use the saved variables that we created?
- Special subpart for people that want to use stored addon info for other purposes.
- Known bugs!

What are saved variables for?
Saved variables are used to store info related to your addon like regular variables. The difference however is that when you logout or exit the game and log back in the data stored in the regular variables will be lost where as the data stored in the saved variables will be still accessible.

What is the downside of saved variables?
Saved variables aren't saved instantly, a crash to desktop will cause a loss of data.

How do we make these saved variables?
Unlike the variables that you create in the .lua files the saved variables are created in the .toc files. Here is an example RiftAddon.toc file :
Code:
Identifier = "NoaxVariableTest"

Name = "Saved Variable Test"

Description = "An addon to show how to save variables between sessions."

Author = "Noax"
Email = "Private"

Environment = "1.3"

RunOnStartup = {
  "main.lua",
}

SavedVariables = {
     number  = 'character',
     number2 = 'character'
}
As you can see there is a bit in this toc file starting with `SavedVariables = {` this part is REQUIRED to save your variables!

The next part are the actual variables in this case `number = 'character'` and `number2 = 'character'.
`number` and `number2` are the variable names and `'character'` indicates that the variables are to be saved for each character sepperatly. The `number2` wont be used any further in the following example, its just to show how to create multiple saved variables.

Account wide variables can be set by:
Code:
SavedVariables = {
    myAddOn_AccountVariable = 'account'
}
If you understood this part then good job you now know how to create saved variables!

How do we use the saved variables that we created?
Now we created the saved variables in the previous part we can start using them in our .lua files.
Even though we can use the saved variables directly its my own preverence to load them into a local variable created from within the lua file and use that variable instead of addressing the saved variable directly.

I will first post the code of the lua file and go into detail in the part after it.
Code:
local testSave

local function noaxtest()
   number = testSave
end

local function noaxadd()
   testSave = testSave + 1
end

local function noaxdisplay()
   print("","",number)
   print("","",number2)
   print("","",testSave)
end

local function noaxsave()
   if testSave then
   	number = testSave
   end
end

local function noaxload()
   if number then
	testSave = number
   else
	testSave = 0
   end
end

table.insert(Event.Addon.SavedVariables.Save.Begin, {function () noaxsave() end, "NoaxVariableTest", "Save variables"})
table.insert(Event.Addon.SavedVariables.Load.Begin, {function () noaxload() end, "NoaxVariableTest", "Load variables"})

table.insert(Command.Slash.Register("noaxadd"), {function () noaxadd() end, "NoaxVariableTest", "Slash command"})
table.insert(Command.Slash.Register("noaxdisplay"), {function () noaxdisplay() end, "NoaxVariableTest", "Slash command"})
As you might notice, my personal preference is to put all these `table.insert` commands at the bottem of the lua file.
Quick description of what it does:
it adds 2 slash commands `/noaxadd` and `/noaxdisplay`. When you first run it and use /noaxdisplay you will most likely see `nil`, `nil`, `0` use /noaxadd and with /noaxdisplay you'll see `nil`, `nil`, `1`. Logout and log back in use /noaxdisplay and you should see `1`, `nil`, `1`

Lets first start with the loading of the variables when we start the game. The parts responsible for the loading of the saved variables are these:
Code:
local function noaxload()
   if number then
	testSave = number
   else
	testSave = 0
   end
end

table.insert(Event.Addon.SavedVariables.Load.Begin, {function () noaxload() end, "NoaxVariableTest", "Load variables"})
Because of the table.insert the saved variables will be loaded automaticly when you login to the game with your character.
When the noaxload function gets called it checks if the number actually has a value if not it will just fill the temporary variable testSave with a 0.

The part responsible for the saving of variables are these:
Code:
local function noaxsave()
   if testSave then
   	number = testSave
   end
end

table.insert(Event.Addon.SavedVariables.Save.Begin, {function () noaxsave() end, "NoaxVariableTest", "Save variables"})
The table.insert for the event to save the variables makes it so that whenever you logout the function in noaxsave will get called. The function itself checks if testSave exists if it does then it stores the value of it in the saved variable `number`.

Special subpart for people that want to use stored addon info for other purposes.
The data that is stored in the saved variables is stored in the `Interface\Saved\<Shard>\<Character>\<SavedVariables>\<Addonname>.lua`. In this case <Shard> will be `PTS`. These files are actually text based files which you can open and just read, good luck making something brilliant with it!

Known bugs!
Bug 1: Incorrect folder permissions prevents saving
Rift does this automatically as part of its install, but if you just create a "Rift PTS" folder and put the PTS patcher in there the permission doesn't get updated to add the modify control

You need to add machinename\Users permissions and check the "Modify" box, otherwise the game will fail to create the permissions required.
OR
Run rift patcher as administrator, which gives it full control and bypasses the issue.

Bug 2: Saving the SavedVariables only works on Character Logout, not Exit
If you use `/quit` or `esc` -> `exit game` and you use the option `exit now` you never go through the logout process that normally happens if you wait 20 seconds to exit. This will result in saved variables not being updated with the new values!

Download link to an example will be following shortly

Kind regards,
Noax
Noax is offline   Reply With Quote
Reply

Go BackRiftui » Developer Discussions » Tutorials & Other Helpful Info. » How to: save variables between gaming sessions.

Thread Tools
Display Modes

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