Contents of variable from within a script

Posted by Mazarin on Thu 29 Jul 2010 07:31 AM — 10 posts, 39,651 views.

#0
I want to access the contents of a variable from a script-text.

How is that done?

I can't write "Send ("wield @weapon")" - which would not, as desired, result in wield (dagger, sword or whip or whatever the contents of the variable "weapon" is).

Thank you.

// Mazarin
Amended on Thu 29 Jul 2010 09:21 AM by Mazarin
Australia Forum Administrator #1
Is this inside an alias "send to script"? Or inside a script file?

In either case, you can do this:


Send ("wield " .. GetVariable ("weapon"))


This is getting the MUSHclient variable "weapon", not the script variable "weapon".

Template:scripting
Scripting tutorial

There is extensive information about scripting in MUSHclient at http://mushclient.com/scripting. This includes screen shots and examples.

#2
But, however the command

Send ("wield " GetVariable("weapon"))

will not work but gives an error message (msg = [string "Trigger: "]:1: ')' expected near 'GetVariable'). I think that what is needed is some type of concatenation of the string "wield" with the contents of the variable "weapon", but how to do that is more than i know.

Anybody, help, please?

//Mazarin







Australia Forum Administrator #3
Better look up the Lua reference, huh?

But concatenation is easy enough:


Send ("wield " .. GetVariable("weapon"))


In fact, didn't I say exactly that earlier up?
#4
Hi, I'm still building my combat aliases.

Now, how can I address the contents of the string in the variable "%1" - is it a variable - from within a script alias. I would like to write to the mud the command "cast missile mule", "cast chill horse" or "cast heal Mazarin" etc. From within a command alias I would use "cast MySpell %1" (when the variable MySpell has the contents of "missile", "chill" and "heal" respectively) and %1 is entered from the keyboard? Howto do this from within a scriptalias?

The commandline - I'm trying to send the command "kick", which is the content of the variable "attackskill" - in

Send ("" .. GetVariable("attackskill"))

works (sends "kick or whatever is in "attackskill")to the MUD but the commandline

Send GetVariable("attackskill")

does not, but gives error message "[string "Alias: "]:1: '=' expected near 'GetVariable'"? Seems odd to me?

Any help. Thank you.

//Mazarin
Amended on Sat 11 Sep 2010 10:36 PM by Mazarin
USA #5
Mazarin said:
The commandline - I'm trying to send the command "kick", which is the content of the variable "attackskill" - in

Send ("" .. GetVariable("attackskill"))

works (sends "kick or whatever is in "attackskill")to the MUD but the commandline

Send GetVariable("attackskill")

does not, but gives error message "[string "Alias: "]:1: '=' expected near 'GetVariable'"? Seems odd to me?


The parentheses are required (minus a couple exceptions). In Lua, a variable (i.e. Send) followed by parentheses is interpreted as a function call. Your second example doesn't have this syntactic cue, so Lua doesn't understand what you're trying to do.

Send(GetVariable("attackskill"))
Amended on Sun 12 Sep 2010 12:47 AM by Twisol
#6
Twisol said:
The parentheses are required (minus a couple exceptions). In Lua, a variable (i.e. Send) followed by parentheses is interpreted as a function call. Your second example doesn't have this syntactic cue, so Lua doesn't understand what you're trying to do.

Send(GetVariable("attackskill"))





This is quite clear. The sentence to to be sent is to be imbedded in parenthecis while the quotation marks have a different meaning. This was not clear to me. Thank you for the advice.

I noticed i had made a mistake in my previous post when i wrote that I wish to give the same command as in a within-alias script of "cast MySpell %1". I would of course like to send the command "cast @MySpell %1", that is send to the mud a command like "cast kick mule" or "cast heal Mazarin" or any similar command, ie. the spellname followed by my input.

Now my question is: how do I accomplish this? How do i write a command containing as well the contents of a variable as the input from the keyboard to the MUD?

Thank you.

//Mazarin


Nevermind. I found the solution. Thanks anyway.

//M.
Amended on Sun 12 Sep 2010 08:17 PM by Mazarin
#7
So how would you do the same thing, but with the script variable "weapon"? Or what command is needed to use an array value? Would arrays be any different?

I tried to use the above on my variable, which contains an array, and I received a nil value. I guess this is expected since it only works for MushClient variables.

An example-- if I'm trying to (hypothetically):




useThisSpell = arrayOfDifferentSpells[math.random(15)]


Send ("cast " .. GetVariable("useThisSpell"))




Amended on Tue 07 Dec 2010 03:07 PM by Eloni
Australia Forum Administrator #8
Template:post=10794
Please see the forum thread: http://gammon.com.au/forum/?id=10794.


This would be a Lua variable - you can serialize Lua variables into MUSHclient variables for saving over sessions.

Template:post=4960
Please see the forum thread: http://gammon.com.au/forum/?id=4960.
Amended on Wed 08 Dec 2010 08:20 AM by Nick Gammon
#9
Thanks! I'm going to give it a shot.