[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Variables in a script, and changing them on command, is there a good way to?
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Variables in a script, and changing them on command, is there a good way to?

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


Posted by Simimi   (40 posts)  [Biography] bio
Date Thu 17 May 2007 05:09 AM (UTC)
Message
I am having this problem, and I think it has to do with variable scope. Inside of my script I have a variable such that:
wings = false

And I have a trigger such that:

<trigger
   enabled="y"
   group="defs"
   match="^Angelic wings sprout out of your back\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>wings = true</send>
  </trigger>


Now, the trigger does not make the change to the script file and I was not sure why. A friend suggested it looks liek a variable scope issue, and suggested I convet all of my variables to mush-client variables, to be handled by mush, and not inside my script. I am ok with that idea, but it does not fix the issue at hand.

Is there a way to change my script variables from inside of mush client, that would be effecient over the long run? Someone suggested storing all of my variables in a Lua table, and useing triggers to call functions to change the table values to true or false. I am not sure how I would do that, or if it would indeed be speed effecient in the long run.

Also, if I need variables to have a third state, like true/false/foo or 0/1/2 ... when using the mush client variables with Lua as my language, can the variables be 0,1,2 instead of true/false?

Thanks for the help!





[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Thu 17 May 2007 05:45 AM (UTC)
Message
Quote:

Now, the trigger does not make the change to the script file and I was not sure why.


What do you mean "to the script file"? A statement like that will change the wings variable in the internal script state for the current world. You could test that by doing this in the command window, if "/" is your script prefix:


/ print (wings)


Changes won't ever be written to the script file as such, unless you manually type them in and save them.

If you want to save something like wings being true or false from one session to another, you either need to use MUSHclient variables (which are saved in the world file), or "serialize" the Lua variables. See this post:

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

Quote:

Is there a way to change my script variables from inside of mush client, that would be effecient over the long run?


Using Lua variables should be efficient. For neatness I would put them into a table, for one thing you can't really serialize the global table (_G).

Then when you start you "un-serialize" them (read back into Lua), and when you save the world you serialize them. In plugins this is easy, see this example:

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

Quote:

Also, if I need variables to have a third state, like true/false/foo or 0/1/2 ... when using the mush client variables with Lua as my language, can the variables be 0,1,2 instead of true/false?


Lua variables can contain virtually anything: numbers, strings, booleans, tables, functions. So you could have true/false/2 if you want to. However this might be confusing, as only false (and nil) is considered false in an "if" test.

Maybe use 0/1/2 instead. If you use MUSHclient variables everything is a string.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Simimi   (40 posts)  [Biography] bio
Date Reply #2 on Thu 17 May 2007 04:28 PM (UTC)

Amended on Thu 17 May 2007 10:33 PM (UTC) by Simimi

Message
My entire post was made irrelevent since most problems were solved by myself and others.

Nick, thank you so much for your time. I now understand how to use your var.lua module.
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Thu 17 May 2007 09:22 PM (UTC)
Message
I was about to reply when your original set of questions disappeared. :)

You are right, the "var" module will be helpful to you. For others who are wondering, this is described here:

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

Effectively this lets you treat MUSHclient variables as script variables.

I do want to respond to one of your (now deleted) questions, just to make things clear:

Quote:

So you're saying eventhough the script file does not visably change when I open the editor to look at it; There is an understood change that has taken place, internally understood by the script file?


No I am not saying that. Changes to script variables are lost when you close the world or reload the script file. Effectively script variables are "short term" for remembering things which are relevant during this session.

For example, your current hit points, which can easily be re-established, might be stored in script variables (plus converting them to a percentage, etc.).

However if you need to remember something from one day to the next (eg. total mobs killed, or time started at a certain level), then they must be saved somehow. Putting them into a MUSHclient variable is one way of doing that - although this relies upon you saving your "world" file.

Plugins have a separate mechanism for saving variables, called a "plugin state" file. If requested, the plugin mechanism will save all the variables in the plugin script space when it is being closed, and reload them next time the plugin is opened. These are still MUSHclient variables however.

The example plugin I gave a link for shows how you might convert those into script variables at load time, and back into MUSHclient variables at save time.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Simimi   (40 posts)  [Biography] bio
Date Reply #4 on Thu 17 May 2007 10:34 PM (UTC)
Message
Many thanks for your explination Nick. I finally think I understand!
[Go to top] 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.


3,253 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]