Super simple question about performing arithmetic in aliases

Posted by Tetnao on Wed 13 Oct 2010 10:53 AM — 7 posts, 27,016 views.

#0
I have a variable called 'level', a trigger which triggers on my level message to update my own level into 'level' variable.

I would like to make an alias that checks who is within 20 levels of me, and my mud provides the command as

who level <lower level> <higher level>

I have seen one of nick's youtube videos on how to expand a variable in an alias to get it's content, however, i cant figure out how to perform an arithmetic operation with the value.

Here's what I did:

who level @level - 20 @level + 20


Lets say my level is 46, it simply sends "who level 46 - 20 46 + 20"

How can I perform arithmetic operations on the variable then send them?

Thanks!
USA #1
If it were me, I'd use a scripting language to do this, I'm not familiar with manipulating an @variable in MUSH (though I know you can do it with z/CMUD, because of their built-in and generally automatic scripting language) so I can't answer that question, but maybe I can help somewhat.

A simple example in JScript -- my preferred language -- would look something like this:

var level = Number(world.GetVariable("level")) ;

world.Send("who level " + (level-20) + " " + (level + 20) ) ;


If you're not using a scripting language at the moment, you can switch to JScript and use that -- be sure to enable "Send to Script" in the alias menu.
Amended on Wed 13 Oct 2010 03:18 PM by Bobo
#2
Make an Alias for whatever you want it to be (ie: wholevel)

In the alias, make it send to script, and under Lua, it'd be something like:

Send("who level " .. (GetVariable("level") - 20) .. " " .. (GetVariable("level") + 20))

I don't think merely using "level" in the Javascript example will retrieve a MUSHclient variable...only a variable set in the Javascript. At least that's the way I understand it.

You may be able to get away with using @level, but by using the GetVariable script function, you're retrieving it at that moment (in case it just got modified in another alias/trigger/whatever that may call your wholevel alias in the future).
Amended on Wed 13 Oct 2010 07:12 PM by LezChap
USA #3
LezChap said:

I don't think merely using "level" in the Javascript example will retrieve a MUSHclient variable...only a variable set in the Javascript. At least that's the way I understand it.


Yeah, hence the var level = ... declaration. If you wanted to make it one line in JScript it'd be more like:

 world.Send("who level " + (Number(world.GetVariable("level"))-20) + " " + (Number(world.GetVariable("level"))+20)) ; 


I always use the Number() function in JScript when dealing with numeric global variables because, I think, either MUSHClient or JScript has them set as "string" variables.
#4
Thanks for help guys, this is solved :)
USA #5
Bobo said:
I always use the Number() function in JScript when dealing with numeric global variables because, I think, either MUSHClient or JScript has them set as "string" variables.

MUSHclient GetVariable/SetVariable variables are strings, yes. If something like SetVariable("foo", 4) works, it's because the 4 is coerced into "4" by JScript.
#6
Bobo said:

LezChap said:

I don't think merely using "level" in the Javascript example will retrieve a MUSHclient variable...only a variable set in the Javascript. At least that's the way I understand it.


Yeah, hence the var level = ... declaration. If you wanted to make it one line in JScript it'd be more like:

 world.Send("who level " + (Number(world.GetVariable("level"))-20) + " " + (Number(world.GetVariable("level"))+20)) ; 


I always use the Number() function in JScript when dealing with numeric global variables because, I think, either MUSHClient or JScript has them set as "string" variables.


I missed that first line the first time I read the code... And the Number() function (or tonumber() in Lua) is a very good point...