Questions on working with Variables...

Posted by Drake1132 on Thu 26 Mar 2009 06:09 PM — 5 posts, 21,200 views.

#0
OK, I'm trying to learn how to use MUSHCLient after years of using zMUD, and while there are a lot of similarities, there are alos a lot of differences, and I have some questions I would like answered regarding how to work with variables.

1. How can I set the value of multiple *world* variables from a single trigger? I want to fill variables such as currentHP, maxHP, and percentHP with a trigger that matches on my prompt. Example prompt text is (without quotes): "<HP: 3229/3229 | SP: 1235/1235 | AP: 983/1080>". Previous attempts I've made at making such a trigger seems to fail to create world variables, and instead is (I believe) creating local variables only. This isn't what I want, so how can I make it fill the value of existing world variables that I've created using the Variables dialog? This is also what I need to learn for question 3 (below).

2. In the same prompt trigger (above) I want to calculate and fill percentHP based on the values of currentHP and mxHP. I presume that I need to do this using the ToNumber() function to convert the untyped or string variable to a number, then make the calculation, then set the value of the variable equal to the result of the calculation, but I'm not totally sure how to go about that. Assuming that the values of the world variables for currentHP and mxHP have been correctly defined and saved before the calculation, could I use something like the following:

percentHP = ToNumber(@currentHP)/ToNumber(@maxHP)

...Naturally, I would need to change the syntax of the variable assignment to assign to the world variable "percentHP" rather than creating a local variable.

3. I want to be able to use a single alias to both set the value of a variable AND execute a command to the world. Example follows:

<aliases>
<alias
match="bs *"
enabled="y"
echo_alias="y"
expand_variables="y"
variable="target"
send_to="12"
keep_evaluating="y"
sequence="100"
>
<send>target = %1
Execute("tech backstab %1")</send>
</alias>
</aliases>

...In the above alias example, I want to set the variable named "target" to equal the string matched by the wildcard. The execution works, but the variable is not changed. I suspect this is because it is creating a local variable rather than changing the value of the variable that already exists in the world, so how can I direct it to change the existing world variable?

I did some forum searching in all of the MUSHClient sub-sections (like "General" and such), but didn't find any relevant answers, and the help files (documentation) only seems to provide the most basic information, and I didn't notice any information on how to handle the difference (if any) regarding proper syntax for a script file vs. proper syntax for an alias or trigger creation via the dialog window.

Thanks for any help you can give to help me fugure this out,
Drake

P.S. Sorry if this is in the wrong forum, by all means feel free to move it if it isn't.
Australia Forum Administrator #1
Quote:

How can I set the value of multiple *world* variables from a single trigger?


With scripting. You can use "send to script" for simplicity, but using a script file and putting a function in that is slightly more efficient.

See http://mushclient.com/pasting for how to copy the trigger below into the client.


<triggers>
  <trigger
   enabled="y"
   match="&lt;HP: */* | SP: */* | AP: */*&gt;*"
   send_to="12"
   sequence="100"
  >
  <send>
SetVariable ("curr_hp", "%1")
SetVariable ("max_hp",  "%2")
SetVariable ("curr_sp", "%3")
SetVariable ("max_sp",  "%4")
SetVariable ("curr_ap", "%5")
SetVariable ("max_ap",  "%6")

</send>
  </trigger>
</triggers>



Quote:

I want to calculate and fill percentHP based on the values of currentHP and mxHP ...


In the above trigger, a simple extra line will do it - if you are using Lua it is smart enough to know that you divide numbers, not strings, and convert for you. However by not putting them in quotes, they are treated as numbers anyway:


percentHP = %1 / %2 * 100  -- calculate percentage
SetVariable ("percentHP", percentHP)  -- save as variable


The more general answer is to use GetVariable, eg.


percentHP = tonumber (GetVariable ("curr_hp")) /
            tonumber (GetVariable ("max_hp")) * 100
SetVariable ("percentHP", percentHP)  -- save as variable


You see the extra line to convert from a script variable to a client variable. Of course you could do it in one line:


SetVariable ("percentHP", %1 / %2 * 100)  


Quote:

target = %1
Execute("tech backstab %1")

...

The execution works, but the variable is not changed


This one is in the FAQ. If you think about what happens after variable substitution you have written (if %1 is Nick):


target = Nick
Execute("tech backstab Nick")


You have assigned the variable Nick to the variable target. Thus target is probably nil as Nick is probably nil.

You want:


target = "%1"
Execute("tech backstab %1") 


Quote:

I didn't notice any information on how to handle the difference (if any) regarding proper syntax for a script file vs. proper syntax for an alias or trigger creation via the dialog window.


Try reading http://mushclient.com/scripting - that has quite a bit of detail in it.
Amended on Thu 26 Mar 2009 07:59 PM by Nick Gammon
#2
Yeah, I thought that SetVariable() and GetVariable() might work, but when I tried it, it sent those commands to the MUD and the MUD gave an error. I assume that I have to change the "Send To" field (if I'm creating them using the dialog rather than with a copy & paste method)... do I change that to "Script", is that what "send_to="12"" means in the trigger you posted?

As for the alias, I have other aliases that call the target variable, so I want to make sure that the target variable is correctly set to my target. thus, if I backstab a target using the bs * alias, such as "bs kobold", I want to also set the target variable at the same time so that other uses of the target variable will have the right information. I have similar aliases that I want to set up for kill * (when backstab is on cooldown and unavailable) and con * (for when I consider a target before deciding whether or not to kill them).

Other than that, your answers were very helpful, thank you... and you don't have to answer about the send to script thing, I'll just paste the trigger you posted and see what "send_to="12"" actually works out to, but feel free to answer it anyway for the benefit of others in the future.

Thanks,
Drake
#3
OK, I feel stupid... you DID answer the send to script question, I just missed it when I first read your post. Sorry about that.

Drake
Australia Forum Administrator #4
The sendto numbers are documented in various places, eg. http://www.gammon.com.au/scripts/doc.php?function=AddTriggerEx


A quick way of looking at them is to type this into the command window, if you have Lua scripting, and "/" is your script prefix:


/require "tprint"; tprint (sendto)


Output is:


"scriptafteromit"=14
"script"=12
"notepadappend"=5
"execute"=10
"notepad"=4
"immediate"=13
"output"=2
"speedwalk"=11
"command"=1
"notepadreplace"=7
"logfile"=6
"status"=3
"variable"=9
"commandqueue"=8
"world"=0


I find tprint so useful it is handy to put it into the default script space. To do this, open Global Preferences -> Lua, and add this to the end of the Lua sandbox:


require "tprint"


Then, all you have to do to see the sendto locations is:


/tprint (sendto)


This table is provided to make scripted use of those numbers more natural. eg.


DoAfterSpecial (5, 'EnableTriggerGroup ("mygroup", 1)', sendto.script)