Using Variables

Posted by Rynok on Wed 21 Jan 2009 03:32 AM — 6 posts, 25,080 views.

USA #0
I've been looking over the website but for the life of me can't figure out how to use a variable.

All I've found is how to access one via "Expand Variables".
What I need to do is increment a variable (var + 1).
It keeps storing it as a string, and doing it via a script makes it fail like crazy (and seems like the long way around to me).

I think I'm just missing something that should be very simple.
Australia Forum Administrator #1
See http://mushclient.com/faq point 31.
USA #2
Isn't that still all handled inside a script though?
I was hoping there was a way to do the arithmetic inside MUSHclient itself.

Similar to the example: 'xp = xp + 1' except when I've done this inside the client itself, it would look like 'xp = @xp + 1' and when I do that, it treats it like a string so that 'xp' actually equals "@xp + 1" (instead of adding it like I'm wanting it to do).

Thanks for the help.
Australia Forum Administrator #3
I'm not too sure what you mean by "inside the client". The script engine is inside the client, particularly Lua, which is imbedded into it.

There is no specific provision for adding 1 to a variable in the GUI interface (after all, someone would want to add 2, or multiply by 3 or something). Thus ad-hoc arithmetic is done by scripting.

It isn't hard, you make a trigger like:


Match: something happens
Send: xp = xp + 1
Send to: script


However that is adding to a script variable. If you want to add to a MUSHclient variable (which would be saved with the world file), you change it to:


Match: something happens
Send: SetVariable ("xp", GetVariable ("xp") + 1)
Send to: script


USA #4
Hmm, so you don't need to make a function call? I figured scripts needed a function to be called to do everything. (I made a function, but it kept failing. I do Lua a lot, so I was doing it in that, but didn't see why it was failing. It might of been because I was giving a function name inside the "Script" field after having it "Send to script" as well)

On second thought, I need the default to always be 0, so having it saved with the World on exit wouldn't do me any good. I was hoping to make a quick one and not work with scripting, but if I can do the inline scripting like that then that'll accomplish the same task.

Anyways, I'll give it a go and see where I get.

Thanks again.
Australia Forum Administrator #5
If you do "send to script" whatever is in the send box is compiled into a Lua "chunk" and then executed. Therefore it needs (to be useful) to be Lua code (not just declarations). That code might declare a function, but ultimately you need to execute it.

If you want the default to be zero (per session) then as suggested earlier, this idiom works quite well in Lua:


xp = (xp or 0) + 1


That makes sure that xp (which is nil when the engine is started up) is replaced by zero the first time around.