Super Basic Math Issue

Posted by Magellan on Mon 26 Jun 2017 03:00 AM — 5 posts, 22,355 views.

#0
getting frustrated here. i'm new to mushclient, and have exclusively used zmud for close to (holy crap) 20 years. in zmud what i would do is #math @variable+1 and get the results i want.

Basically what i need is the likely super basic coding for how to add 1 to a variable.

EVENT 1: Variable+1
EVENT 2: Variable+1
EVENT 3: Variable-1

each event would be it's own separate trigger.

I've been searching the forum's and can't seem to find a topic that mirrors what i need enough for me to understand fully what i need to do to implement it. Thanks in advance.
#1
SetVariable ("Variable", "@Variable"+1)


i knew it was simple.
Australia Forum Administrator #2
An alternative is to get the variable, convert it to a number, and add 1, like this:


SetVariable ("foo", tonumber (GetVariable ("foo")) + 1)


Your method of expanding the variable like @foo won't work if you have changed that variable in the same trigger, because substitution of variables is done before the script starts running.
USA Global Moderator #3
Quote:
substitution of variables is done before the script starts running.

Pre-process substitution is something that some of us have kinda internalized over the years, but is there a document describing when different bits of a script happen (immediate send block vs script function call vs @variable expansion vs xml processing, etc)? If not I think I'll put one together.
Australia Forum Administrator #4
I did a video about using variables:

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

If you just want to add one to a variable in this session (rather than to be remembered forever) then it is simpler to use Lua variables. For example:


-- make sure variable exists initially
if mobsKilled == nil then
  mobsKilled = 0
end -- if

mobsKilled = mobsKilled + 1


You can do that in "send to script" in a trigger, with Lua as the scripting language.

Alternatively (although it is a bit more obscure):


mobsKilled = (mobsKilled or 0) + 1