Need little help saving table over quit

Posted by Couto on Mon 03 Dec 2012 10:17 PM — 10 posts, 35,057 views.

#0
Hi guys,

i've a little problem saving a table over quit. I already read a few posts about this problem. But i didn't fully understand it.

In Avalon Mud, they don't provide exact numbers of xp for levelups. So my idea was to track the levelup and the xp with ATCP and save it to a table level[x]. Where x is the level and level[x] the value of xp needed for it.

What did i do?

Here is a part of my script:


function OnPluginInstall ()
  levelwerte = {}
  for i = 1, 120, 1 do
    levelwerte[i] = tonumber(GetPluginVariable("f4a727cb3b8ebdccd4f0af42", "level"..i))
    if levelwerte[i] == nil then
      levelwerte[i] = 0
    end
  end
end

function OnPluginSaveState ()
  for i = 1, 120, 1 do
    SetVariable("level"..i, tostring(levelwerte[i]))
  end
end


OnPluginSaveState it shall save all variables of the table (for x = 1 to 120) as variables called "level1", "level2", ... "level120" in addition with the values.

OnPluginInstall it shall read all variables and put it back in the table. But the save function doesnt work. I cant find the saved variables anywhere.

Edit: save_state is "y"


<plugin
   name="Levelup_Plugin"
   id="f4a727cb3b8ebdccd4f0af42"
   language="Lua"
   save_state="y"
   date_written="2012-12-03 20:17:00"
   requires="4.71"
   version="1.0"
   >
</plugin>
Amended on Tue 04 Dec 2012 02:11 AM by Nick Gammon
Australia Forum Administrator #1
Read this about serializing variables:

http://www.gammon.com.au/forum/?id=4960

You shouldn't need to copy each table item into a string like you did. Just save the whole table as shown in that thread.
#2
Hello Nick,

i tried a bit around. But i didn't got it working.

How can i use this 'serialize'? Do i need a extra plugin?

With just copying the code on top of my own script, it didn't work.


Run-time error
Plugin: Levelup_Plugin (called from world: Ava-Wombat)
Immediate execution
...\proggeln\MUSHCLIENT\Levelup.plugin\scripts\main.lua:32: attempt to call a table value
stack traceback:
        ...\proggeln\MUSHCLIENT\Levelup.plugin\scripts\main.lua:32: in main chunk
        [C]: in function 'require'
        [string "Plugin"]:41: in main chunk


main.lua:32 is the following:


for _, v in {
    "and", "break", "do", "else", "elseif", "end", "false", 
    "for", "function", "if", "in", "local", "nil", "not", "or", 
    "repeat", "return", "then", "true", "until", "while"
            } do lua_reserved_words [v] = true end


I think once i got the serialize-function included correctly, i shouldnt have anymore problems to get the tables serialized^^.

Thanks in advance
Couto
Australia Forum Administrator #3
Can you post your code please? Seeing an error message and a couple of lines doesn't tell me much.
#4
http://pastebin.com/gqDJ2zJs


as u can see, i copied the serializer-code on top of the file and the function OnWorldSave(), OnPluginSaveState() and OnPluginInstall at the bottom.

I'm using Twisols PPI for ATCP in this script.

Now that i looked up the ppi.lua i saw that there is already something like a serializer for tables, mayb i can use this?

--> http://pastebin.com/Z92dKSjf (Twisols PPI)
Australia Forum Administrator #5
That looks too complicated to me.


require "serialize"  -- needed to serialize table to string

levelwerte = {}  -- ensure table exists, if not loaded from variable

-- on plugin install, convert variable into Lua table

function OnPluginInstall ()
  assert (loadstring (GetVariable ("levelwerte") or "")) ()
end -- function OnPluginInstall

-- on saving state, convert Lua table back into string variable

-- save_simple is for simple tables that do not have cycles (self-reference)
-- or refer to other tables

function OnPluginSaveState ()
  SetVariable ("levelwerte", 
               "levelwerte = " .. serialize.save_simple (levelwerte))
end -- function OnPluginSaveState



That's all you need. It saves the table "levelwerte" at save time, and loads it back at load time.
#6
Thanks Nick,

the problem was an easy one. I thought that i would need another plugin called serialize with your code of the other thread and now, after your clear answer, i noticed that it is already in the mushclient folder ^^.

I guess it works now. Thanks alot for your help!

Edit:

Next Question: Where does it save the variables? I thought they were saved to the world file, but i cant find them there. But obviously they were saved.
Amended on Thu 13 Dec 2012 07:21 PM by Couto
Australia Forum Administrator #7
There is a "state" folder (beneath the plugins folder, I think, usually).

Inside are files named like this:


e1b6c6a9e51d8ab024a981df-f67c4339ed0591a5b010d05b-state.xml


The first "id" is the world ID, and the second "id" is the plugin ID. So in this case these are the variables for world e1b6c6a9e51d8ab024a981df, and plugin f67c4339ed0591a5b010d05b.

The hex IDs are used in case you have two plugins of the same name, or two worlds of the same name. Your variables will be inside that.
#8
Nick Gammon said:

There is a "state" folder (beneath the plugins folder, I think, usually).

Inside are files named like this:


e1b6c6a9e51d8ab024a981df-f67c4339ed0591a5b010d05b-state.xml


The first "id" is the world ID, and the second "id" is the plugin ID. So in this case these are the variables for world e1b6c6a9e51d8ab024a981df, and plugin f67c4339ed0591a5b010d05b.

The hex IDs are used in case you have two plugins of the same name, or two worlds of the same name. Your variables will be inside that.


found it, but sadly its not near the state folder of my plugin.

its something like:

C:\Users\couto\AppData\Local\VirtualStore\Program Files (x86)\MUSHclient\worlds\plugins\state

is it possible (with low efford) to save it to the state folder near the plugin or maybe centralized for more then 1 plugin?
USA Global Moderator #9
You appear to be using Windows Vista or later. You have a few options.

Option 1) Don't put it in your Program Files folder, because programs don't have the freedom to write files there while UAC is fully active.

Option 2) Disable UAC entirely (inadvisable).

Option 3) Disable the user Virtualization feature of UAC.

Option 4) Change specific permissions for the MUSHclient directory.

There is a forum thread that discusses these options for some other PC game here. Take a look.
http://www.twcenter.net/forums/showthread.php?t=397636