+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 19

  Click here to go to the first Rift Team post in this thread.   Thread: How to: save variables between gaming sessions.

  1. #1
    Soulwalker
    Join Date
    Jun 2011
    Posts
    11

    Default How to: save variables between gaming sessions.

    Hello dear fellow addon developers / rift community.

    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 = 'account'
    }
    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. Keep in mind that the other addons will be able to adjust your saved variables as well, as long as they know the name of the variable!
    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 noaxadd()
       testSave = testSave + 1
    end
    
    local function noaxdisplay()
       print("","",number)
       print("","",number2)
       print("","",testSave)
    end
    
    local function noaxsave(addon)
       if addon == "NoaxVariableTest" then
         if testSave then
         	number = testSave
         end
       end
    end
    
    local function noaxload(addon)
       if addon == "NoaxVariableTest" then
         if number then
      	testSave = number
         else
      	testSave = 0
         end
      end
    end
    
    table.insert(Event.Addon.SavedVariables.Save.Begin, {noaxsave, "NoaxVariableTest", "Save variables"})
    table.insert(Event.Addon.SavedVariables.Load.End, {noaxload, "NoaxVariableTest", "Load variables"})
    
    table.insert(Command.Slash.Register("noaxadd"), {noaxadd, "NoaxVariableTest", "Slash command"})
    table.insert(Command.Slash.Register("noaxdisplay"), {noaxdisplay, "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(addon)
       if addon == "NoaxVariableTest" then
         if number then
      	testSave = number
         else
      	testSave = 0
         end
       end
    end
    
    table.insert(Event.Addon.SavedVariables.Load.End, {noaxload, "NoaxVariableTest", "Load variables"})
    Because of the table.insert the saved variables will be loaded automaticly when you login to the game with your character. As ZorbaTHut mentioned in the documentation topic: In the case of SavedVariables you'll want to use the End event, as that will be the first opportunity after things have been loaded
    When the noaxload function gets called it checks if the addon thats in the load function is the right addon and 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(addon)
       if addon == "NoaxVariableTest" then
         if testSave then
       	number = testSave
         end
       end
    end
    
    table.insert(Event.Addon.SavedVariables.Save.Begin, {noaxsave, "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>\<SavedVariabl es>\<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!

    I know its gotten into quite a post, I just hope some people find this useful and understandable enough to read. Any comments would be apreciated.

    Kind regards,
    Noax also known ingame as Amantha@Quarrystone

    PS. For some odd reason i couldnt upload the zip file of the example addon kept getting invalid file.
    Last edited by Noax; 06-15-2011 at 06:50 AM. Reason: Updates

  2. #2
    RIFT Fan Site Operator Aieny's Avatar
    Join Date
    Feb 2011
    Location
    Earth
    Posts
    472

    Default

    One correction, you aren't actually required to have SavedVariables section in the TOC just to get the AddOn to load; you do need it for saving variables, though.

    Also, the downside to SavedVariables is that they aren't saved instantly, so you wouldn't want to use saved variables for critical data (if Trion provides an alternative), as a crash to desktop will cause a loss of data.
    a community-oriented site for all things Rift

  3. #3
    Soulwalker
    Join Date
    Jun 2011
    Posts
    11

    Default

    Thanks Aieny, changed the first post to update the info you gave about saved variables.

    Havent (yet) experienced a crash while using saved variables so i had no idea yet that it would cause a loss.

    Any more input reguarding this subject is much apreciated,
    Noax

  4. #4
    RIFT Fan Site Operator Aieny's Avatar
    Join Date
    Feb 2011
    Location
    Earth
    Posts
    472

    Default

    You can test crashes (heh) by assigning one of your saved variables, then logging out to make sure the value is stored. Using a /slash command, run a function that does an extremely long for loop (it'll freeze your screen for a while, then crash out to desktop) and check the value of the saved variable when you return to the game.

    Code:
    -- Initial variable assignment
    mySavedVar = -1
    Code:
     -- using a /slash command to crash the game
    mySavedVar = 0
    for i=1,10000000 do
        mySavedVar = i
    end
    Account wide variables can be set by:

    Code:
    SavedVariables = {
        myAddOn_AccountVariable = 'account'
    }
    Last edited by Aieny; 06-12-2011 at 01:18 PM.
    a community-oriented site for all things Rift

  5. #5
    Soulwalker
    Join Date
    Jun 2011
    Posts
    11

    Default

    Once again my thanks for the additional info Aieny.

    For testing crashes wouldnt it be quicker to go to the task manager and kill the process instead? I can imagine that the loop might take some time before it crashes.

    Adding / added the acount wide saved variables in the first post.

  6. #6
    RIFT Fan Site Operator Aieny's Avatar
    Join Date
    Feb 2011
    Location
    Earth
    Posts
    472

    Default

    The loop (which I found by accident) runs about 20 sec before crashing the client. I did test it, using the loop to change the Saved variable directly, and it kept its previous loaded value.
    a community-oriented site for all things Rift

  7. #7
    RIFT Fan Site Operator Cairenn's Avatar
    Join Date
    Jun 2011
    Posts
    154

    Default

    Hi Noax,

    Could I ask you to perhaps consider uploading/posting this on RiftUI as well? You could post it in the Developer Discussions sections of the forums and have a link to a sample download. Or perhaps include it in our wiki? Thanks.
    "My friends, love is better than anger. Hope is better than fear. Optimism is better than despair. So let us
    be loving, hopeful and optimistic. And we’ll change the world."
    Cairenn, Co-Founder
    and Administrator, RiftUI

  8. #8
    Rift Disciple
    Join Date
    Feb 2011
    Posts
    132

    Default

    I found two bugs with SavedVariables


    Bug 1: Incorrect folder permissions prevents saving

    I found a permission issue with SavedVariables that prevents it from working correctly depending on how you run the PTS and how its folders were created on install.

    A little context...I tried your example several times and it didn't work. I saw the directories were never created under Interface\Saved... Then quit out and launched rift PTS as admin and then the directories and files were correctly created. Then I went poking about the system checking the ACLs and saw a difference between Rift Game folder and my Rift PTS folder. For RIFT Game (the one installed for the normal servers) there was a users permission added for "Modify" permissions, such as to write logs, etc. This permission was not present on my Rift PTS folder that I created and dropped the rift PTS patcher in when I installed the test build.

    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.

    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.

    A more robust solution would be for the rift patcher/installer to verify the ACLs are correct on the install directory & subdirectories.

    This issue won't affect the normal servers because the permission is created, but still I think it would be good for the patcher to verify the ACLs and set them appropriately.



    Bug 2: Saving the SavedVariables only works on Character Logout, not Exit

    If you exit the game and do not logout it will not save your data. I think this will lead to quite a few dataloss issues with addons that use savedvariables and players configuring data. The fastest way to leave the game is to just exit, if people dont' wait for the logout or logout first they will lose all saved variables for the session.
    "The only constant in all your failed warfronts is you."
    Malorn, Cleric of The Enclave

  9.   This is the last Rift Team post in this thread.   #9
    Rift Team
    Join Date
    Oct 2010
    Posts
    927

    Default

    Quote Originally Posted by Malvir View Post
    I found two bugs with SavedVariables
    Good reports, we'll take a look at them. Thanks!

  10. #10
    Telaran Mabs's Avatar
    Join Date
    Apr 2010
    Location
    France, Gex
    Posts
    63

    Default

    any way to trigger the loading of a variable from the code?
    i want some variable to be modified by some other software, and then show the new value without doing a /reloadui

  11. #11
    Plane Touched
    Join Date
    Feb 2011
    Posts
    283

    Default

    About this SaveVariable thing.

    I am still experiencing Rift Client crashing everyday, 3 per day in average.
    This happens to a few guildies as well, and I believe it happens to many other players as well.
    Most frequently it happens after a map load or after killing a rift boss while there are many players around.

    I have got used to it but if this is going to cause SaveVariables losing my data so frequently, this will become much more serious than it is now.

    Why don't just add a Save Variables API so the addon can save their settings or whatever important data needed to be updated instantly without waiting for log off, which can result in losing all of them at a crash?

  12. #12
    Soulwalker
    Join Date
    Jun 2011
    Posts
    11

    Default

    @Malvir
    Thanks for the additional info about the bugs you found! I updated the first post and added a new part 'Known bugs'.

    @ZorbaTHut
    Nice to see that the bugs are getting looked into! Any chance of looking into a saver way of storing data in saved variables as well to prevent data loss on crashes?

    @Cairenn
    Will do when i have the time for it on both riftui and the wiki.

    @Mabs
    I am unsure if any future for this is already included or if this is just a problem at the moment related to only saving the variables on logout. I'll have a look into it to see if there is a posible way to do this already.

    @comet361
    I know the issue you have and its something i am pretty sure about that most addon developers find a pain as well. All we can do at the moment is hope that the developers see this and give us a more secure way for the saved variables.

    Regards,
    Noax

  13. #13
    RIFT Fan Site Operator Cairenn's Avatar
    Join Date
    Jun 2011
    Posts
    154

    Default

    Quote Originally Posted by Noax View Post
    @Cairenn
    Will do when i have the time for it on both riftui and the wiki.
    Thanks Noax. May I ask what wiki? (Did I somehow miss a link somewhere? )
    "My friends, love is better than anger. Hope is better than fear. Optimism is better than despair. So let us
    be loving, hopeful and optimistic. And we’ll change the world."
    Cairenn, Co-Founder
    and Administrator, RiftUI

  14. #14
    Soulwalker
    Join Date
    Jun 2011
    Posts
    11

    Default

    Hi Cairenn,

    You might want to reread your own post (the first one )

    Quote Originally Posted by Cairenn View Post
    Hi Noax,

    Could I ask you to perhaps consider uploading/posting this on RiftUI as well? You could post it in the Developer Discussions sections of the forums and have a link to a sample download. Or perhaps include it in our wiki? Thanks.
    Do pay attention to your last question

    Regards,
    Noax

  15. #15
    RIFT Fan Site Operator Cairenn's Avatar
    Join Date
    Jun 2011
    Posts
    154

    Default

    I thought you meant our site and some other wiki.
    "My friends, love is better than anger. Hope is better than fear. Optimism is better than despair. So let us
    be loving, hopeful and optimistic. And we’ll change the world."
    Cairenn, Co-Founder
    and Administrator, RiftUI

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts