Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Need little help saving table over quit

Need little help saving table over quit

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Couto   (15 posts)  Bio
Date Mon 03 Dec 2012 10:17 PM (UTC)

Amended on Tue 04 Dec 2012 02:11 AM (UTC) by Nick Gammon

Message
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>
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 04 Dec 2012 02:13 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Couto   (15 posts)  Bio
Date Reply #2 on Sun 09 Dec 2012 09:20 PM (UTC)
Message
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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Mon 10 Dec 2012 03:10 AM (UTC)
Message
Can you post your code please? Seeing an error message and a couple of lines doesn't tell me much.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Couto   (15 posts)  Bio
Date Reply #4 on Mon 10 Dec 2012 03:57 PM (UTC)
Message
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)
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Mon 10 Dec 2012 11:31 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Couto   (15 posts)  Bio
Date Reply #6 on Thu 13 Dec 2012 06:48 PM (UTC)

Amended on Thu 13 Dec 2012 07:21 PM (UTC) by Couto

Message
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.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #7 on Thu 13 Dec 2012 08:34 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Couto   (15 posts)  Bio
Date Reply #8 on Sat 15 Dec 2012 09:03 AM (UTC)
Message
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?
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #9 on Sat 15 Dec 2012 02:17 PM (UTC)
Message
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

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


25,205 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.