Stupid Question

Posted by Duma on Fri 06 Apr 2007 01:01 AM — 6 posts, 20,001 views.

USA #0
Ok, I know this is a stupid question and the answer might be buried in the help files, but here it goes.

How do I refer to a variable in the input screen? I don't want to have to make an alias for it, but I want to be able to just type sdk @t with t being my target.

I've tried to simply sdk @t but it takes the "@t" literally and inputs sdk @t instead of say, sdk rat. Any help would be appreciated.
Australia Forum Administrator #1
There is no provision for expanding generic @variable in the command line. It was intended for use in aliases.

However you could make a generic alias that does it for you. For example, if you are planning to type:

<some_command> @<some_variable>

... then this would work (Lua scripting):


<aliases>
  <alias
   match="^(\w+) @(\w+)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
do
  local target = GetVariable ("%2")
  Send ("%1 " .. (target or ("@%2")))
end -- do
  </send>
  </alias>
</aliases>


That looks up the variable and sends the replacement.
USA #2
Just be careful when using this script. The mud I play on uses @ for colour codes. So @g is dark green, @G is bright green, etc. You might want to have a way to disable or escape that if you're using a variable. If you're accessing variables with @foo like zmud, you might want to have it escape with a tilde as well. The alias would be:

<aliases>
  <alias
   match="^^(\w+) (?<!~)@(\w+)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
do
  local target = GetVariable ("%2")
  Send ("%1 " .. (target or ("@%2")))
end -- do
  </send>
  </alias>
</aliases>

This will protect you from having @crush match the variable "crush" when you meant to colour rush dark cyan. You would use ~@crush instead. Might never come up, but you wouldn't want to have your script do something wacky when you don't want it to.
Amended on Fri 06 Apr 2007 06:03 AM by Shaun Biggs
USA #3
I was thinking about this a bit, and I redid the alias. Here's the result.

<aliases>
  <alias
   match="^(.*) (?<!~)@(\w+)(\W.*)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
do
  local target = GetVariable ("%2")
  Send ("%1 " .. (target or ("@%2")) .. "%3")
end -- do
</send>
  </alias>
</aliases>

That will let you have the variable pretty much anywhere on the line that you want it. However, I can't find a way to get multiple variables on one line. Is there a repeat on same line function for aliases like there is for triggers? If not, would it be better to have a somewhat recursive function called? Or just have a Lua script parsing the whole line?
Amended on Fri 06 Apr 2007 09:37 PM by Shaun Biggs
Australia Forum Administrator #4
A more general solution, which you may or may not want, depending on how aggressively you want @variable to be replaced, would be to let a Lua string.gsub find and replace every occurrence of @name into its corresponding variable. This would do it:


<aliases>
  <alias
   match=".*@.*"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
Send ((string.gsub ("%0", "@([%a%d_]+)", 
      function (s)
        return GetVariable (s) or ("@" .. s) 
      end)))
</send>
  </alias>
</aliases>

USA #5
That's what I was referring to when I mentioned having Lua parse the whole line. I just wanted to know if there was a better way to do that.