Variables in scripts

Posted by Errigour on Thu 17 Jul 2025 08:17 AM — 5 posts, 7,838 views.

USA #0
I really think it would be a good idea to allow you to use
expand variables when making aliases or triggers etc.. to
define and use variables. Idk if there is a way to do that
without using SetVariable and GetVariable. Like if you use say
@myvariable = 1 and it doesn't exist it should make that
variable. That would be really cool I believe mudlet does
something similar to that accept they just make all your lua
variables part of the worlds variables and then delete them on
close unless you check them with a check box. Thanks for
letting me waste your time.

Example:
@myvariable = 1

if @myvariable == 1 then
-- do something
end
Australia Forum Administrator #1
There is already something similar to that supplied with MUSHclient. See: https://www.gammon.com.au/forum/?id=4904&reply=6#reply6

You have to put "var." in front of variables that you want saved, eg.


var.gold = 666

print (var.hp)  --> might be nil


This basically uses a Lua table called "var" which is automatically serialized (you still have to save your world file to save the variables).

So, there isn't much difference between putting "@" at the start of the variable you want saved, and putting "var.". You could edit the plugin and make "var" be "v" if you want to save typing.


Quote:

That would be really cool I believe Mudlet does something similar to that accept they just make all your Lua variables part of the worlds variables and then delete them on close unless you check them with a check box.


Well, you could make it that you have a table of variables you want saved, and then automatically serialize those.

For serializing, see: https://www.gammon.com.au/forum/?id=4960
USA #2
This gives an error but I have a variable called cutter.


if var.cutter == "1" then
  Note("No Longer Boarding Cutter.")
  var.cutter = "0"
else
  Note("Boarding Cutter.")
  var.cutter = "1"
end



Run-time error
World: Ethius on Necromium
Immediate execution
[string "Alias: "]:1: attempt to index global 'var' (a nil value)
stack traceback:
        [string "Alias: "]:1: in main chunk
USA #3
Oh I see what I did wrong, next time I need to read the posts you suggest first. Ok thanks.
Amended on Fri 18 Jul 2025 11:49 AM by Errigour
USA Global Moderator #4
Nobody should ever use the feature to expand "@" variables inside a script body. It's just a bad idea because the expansion happens before the script is actually run and it inevitably leads to confusion and broken code. The only safe use is for dynamic match patterns.