Switching from local variables to client variables and back

Posted by Silencher on Sat 01 Mar 2014 04:20 AM — 7 posts, 29,130 views.

#0
Hello. I'm having some trouble. What I'm trying to do is this basic idea:

1. Login: I pull the data saved in my client variables to local variables, effectively refreshing the local variables in case I had to close mushclient.

2. Playing: While playing, I may alter/compute/etc. the local variables, changing their data.

3. Logout: When I logout, I want to save those local variables back to client variables so they're retained in case I need to close mushclient.

---
To test this, I made two aliases:
==
'alias: setv *'
SetVariable("test", "%1")

--
'alias: getv'
v = GetVariable("test")
Note(v)
===

'getv' works fine. setv, however, is the trouble.

If I put:

vari = %1
SetVariable("test", "vari") |or if I do:|
SetVariable("test", vari)

Then when I do 'getv' it always displays 'vari' rathe than %1.

Pulling from client variables to local variables (as shown in my alias 'getv', works fine.

But I can't do the opposite, I can't seem to do:
SetVariable("test", v) So that client-variable 'test' has the contents of 'v'.

What am I doing wrong and how can I get around this?
Australia Forum Administrator #1
There's a lengthy post on that:

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

It should all be automatic if you make a plugin and use a "state file". Then your variables are serialized (with the code shown in that post) with minimal effort.
#2
I read that post and the post linked that describes the custom socials list, but I'm still confused about how to use this.

1) Where would I actually put the 'serializer'?
2) I would probably use 'simple' tables. So say. I had the table you have. To alter data would I use an alias/trigger with
koboldtreasure1 = cutlass
SetVariable("mobs.kobold.treasure[1]", serialize ('koboldtreasure1'))?

So that it would change the first treasure from 'sword' to 'cutlass'?

3)At the bottom of the post, functions for OnPluginSaveState and OnPluginInstall, would I put these in triggers?

Basically I'm very low-skilled with Lua/programming and I'm not sure how to make a plugin that would automate what I'm trying to do, or how to save/load/alter the data.

Could you tell me how to install this and essentially make it work?

Ex:
Table 'my vars'

Myvars = {
Money = 500
Catname = "Fluffy"
Exp = 10000
Class = "wizard"
}
So let's assume i just use the above local variables: money, catname, exp, class.

I know how to save stuff from my output to alter the local variable 'money' but I don't understand how to save it to a client/global variable and the reload the client variable.

I also haven't used tables in Lua before and while I did create a simple plugin once, I did it using the plugin wizard so I am very overwhelmed, a lot of the post i couldn't decipher due to lack of experience/ignorance.
Australia Forum Administrator #3
Silencher said:


2) I would probably use 'simple' tables. So say. I had the table you have. To alter data would I use an alias/trigger with
koboldtreasure1 = cutlass
SetVariable("mobs.kobold.treasure[1]", serialize ('koboldtreasure1'))?


No, that doesn't make sense. A variable in quotes like that is just a string of text, not a variable.


Quote:

3)At the bottom of the post, functions for OnPluginSaveState and OnPluginInstall, would I put these in triggers?


No, those go inside plugins.

There are multiple ways of doing this, the simplest is probably to make a plugin.
#4
I know this is probably annoying, but is there anyway you/someone could show me, step by step, how this is set up, as well as how it's used after installation?? I really don't understand how to do it, even after re-reading both of those posts.
Australia Forum Administrator #5
OK, here is an example. To allow for multiple variables you might want to save we have a table of variables:


my_variables = {}


The alias "setv" sets an entry inside this table:


my_variables.test = "%1"


Thus you could save other variables as well (use another word than "test").

The scripting near the bottom of the plugin saves and restores the variables when you close/open MUSHclient. After that it's all automatic. These are Lua variables, not MUSHclient variables.

Template:saveplugin=Saved_Variables_Demo
To save and install the Saved_Variables_Demo plugin do this:
  1. Copy the code below (in the code box) to the Clipboard
  2. Open a text editor (such as Notepad) and paste the plugin code into it
  3. Save to disk on your PC, preferably in your plugins directory, as Saved_Variables_Demo.xml
    • The "plugins" directory is usually under the "worlds" directory inside where you installed MUSHclient.
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Saved_Variables_Demo.xml (which you just saved in step 3) as a plugin
  7. Click "Close"
  8. Save your world file, so that the plugin loads next time you open it.



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, March 06, 2014, 2:46 PM -->
<!-- MuClient version 4.91 -->

<!-- Plugin "Saved_Variables_Demo" generated by Plugin Wizard -->

<!--
See: http://www.gammon.com.au/forum/?id=12388
-->

<muclient>
<plugin
   name="Saved_Variables_Demo"
   author="Nick Gammon"
   id="1780b441d6b07da9fe6aa3a8"
   language="Lua"
   purpose="Shows how to save variables in a state file"
   save_state="y"
   date_written="2014-03-06 14:44:11"
   requires="4.91"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Usage:

setv (something)
getv
]]>
</description>

</plugin>


<!--  Aliases  -->

<aliases>
  <alias
   match="setv *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

my_variables.test = "%1"
ColourNote ("cyan", "", "my_variables.test set to: %1")

</send>
  </alias>
  
  <alias
   match="getv"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

if my_variables.test then
  ColourNote ("cyan", "", "my_variables.test is now: " .. my_variables.test)
else
  ColourNote ("read", "", "my_variables.test is not defined")
end -- if

</send>
  </alias>
  
</aliases>

<!--  Script  -->


<script>
<![CDATA[

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

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

-- on plugin install, convert variable into Lua table

function OnPluginInstall ()
  assert (loadstring (GetVariable ("my_variables") 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 ("my_variables", 
               "my_variables = " .. serialize.save_simple (my_variables))
end -- function OnPluginSaveState

]]>
</script>


</muclient>
#6
Okay, do I need to do anything to create the table, or does that happen automatically because of the 'my_variables' part of 'my_variables.test'?

Sorry, I know this is likely very basic, but I have very little familiarity with Lua beyond basic, basic things.