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 ➜ Switching from local variables to client variables and back

Switching from local variables to client variables and back

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


Posted by Silencher   (55 posts)  Bio
Date Sat 01 Mar 2014 04:20 AM (UTC)
Message
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?
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 01 Mar 2014 06:32 AM (UTC)
Message
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.

- Nick Gammon

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

Posted by Silencher   (55 posts)  Bio
Date Reply #2 on Sat 01 Mar 2014 12:06 PM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 02 Mar 2014 05:59 AM (UTC)
Message
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.

- Nick Gammon

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

Posted by Silencher   (55 posts)  Bio
Date Reply #4 on Thu 06 Mar 2014 02:45 AM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #5 on Thu 06 Mar 2014 03:53 AM (UTC)
Message
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 between the lines below (to the Clipboard)
  2. Open a text editor (such as Notepad) and paste the plugin into it
  3. Save to disk on your PC, preferably in your plugins directory, as Saved_Variables_Demo.xml
  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"



<?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>

- Nick Gammon

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

Posted by Silencher   (55 posts)  Bio
Date Reply #6 on Thu 06 Mar 2014 07:49 AM (UTC)
Message
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.
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.


27,413 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.