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

Posted by Simimi on Thu 17 May 2007 05:09 AM — 5 posts, 21,129 views.

#0
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!





Australia Forum Administrator #1
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.

#2
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.
Amended on Thu 17 May 2007 10:33 PM by Simimi
Australia Forum Administrator #3
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.
#4
Many thanks for your explination Nick. I finally think I understand!