Targeting alias

Posted by Nick Gammon on Tue 30 May 2006 06:41 AM — 11 posts, 50,446 views.

Australia Forum Administrator #0
The alias here (written in Lua) demonstrates how to write a targetting function that lets you select a word in the output window, and make that your target.

For instance, if you have a kobold attacking you, double-click the word kobold in the output window (to select it) and then type "target". You can speed things up by making a macro like Alt+T to be the word "target".

The alias finds the line, and start and end column of the selection (if any). If found, it applies a regular expression to the found text to make sure it passes its rules (ie. is a single word consisting of A-Z or 0-9).

If no word is found, it uses utils.inputbox to request the name of the target to be typed in (which you can cancel if you don't want to type it).



<aliases>
  <alias
   match="target"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
do
  local target -- Lua variable to hold our target

  -- find the selection range

  local startline, endline, startcol, endcol = 
    GetSelectionStartLine (),
    GetSelectionEndLine (),
    GetSelectionStartColumn (),
    GetSelectionEndColumn ()

  -- if a single line selected, take that word  

  if startline &gt; 0 and 
     endline == startline and
     startcol &gt; 0 and
     endcol &gt; 0 then
    local word = string.sub (GetLineInfo (startline, 1), startcol, endcol - 1)

  -- sanity check on word (in case whole line selected)

    _, _, target = string.find (word, "^([A-Za-z0-9]+)$")
  end

  -- otherwise prompt for one

  if not target then

    target = utils.inputbox ("Enter the name of the desired target", 
                             "Select target")
  end -- if no target selected

  -- echo new target name
  
  if target then
    ColourNote ("white", "blue", "Target now " .. target)
    SetVariable ("target", target)  -- copy into MUSHclient variable
  end -- have a target
end -- do
</send>
  </alias>
</aliases>
Amended on Tue 30 May 2006 07:43 AM by Nick Gammon
#1
Is it possible to use this as a plug in?

I copied the code, launched the plug in wizard, pasted into the scipt section, and saved it. I then attempted to load the plug in (under file > plugin) but I recieved the following error:

Scipting Error

Error Number: 0

Event: Compile Error

Raised by: Plugin: target (called from world: test) (I loaded an empty world and called it test.)

[string "Plugin"]:140: unexpected symbol near '<'

Called By: Immediate execution

The system I use is written in a language other then Lua and I'd like to try this method of targetting if possible.
USA #2
Quote:
The alias here (written in Lua) demonstrates how to write a targetting function that lets you select a word in the output window, and make that your target.

From the quote above, this is written in Lua, so it won't work on other script engines. Which script language do you use? This shouldn't be terribly hard to convert over to other scripts. Aside from the util box, perhaps.
Australia Forum Administrator #3
Quote:

I copied the code, launched the plug in wizard, pasted into the scipt section, and saved it.


What you needed to do, and what I did to make the plugin below is:

  • Copy the alias
  • Paste into your current world (see http://mushclient.com/pasting) - this adds one alias
  • Launch the plugin wizard and make a plugin consisting of just that alias. Make Lua the script language.



This is the result (you can copy between the lines to make a plugin file):


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Tuesday, May 15, 2007, 10:22 AM -->
<!-- MuClient version 4.06 -->

<!-- Plugin "Targetting_system" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Targetting_system"
   author="Nick Gammon"
   id="eb5860ad79f9193c181f8834"
   language="Lua"
   purpose="Sets 'target' variable"
   date_written="2007-05-15 10:21:52"
   requires="4.00"
   version="1.0"
   >

</plugin>


<!--  Aliases  -->

<aliases>
  <alias
   match="target"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
do
  local target -- Lua variable to hold our target

  -- find the selection range

  local startline, endline, startcol, endcol = 
    GetSelectionStartLine (),
    GetSelectionEndLine (),
    GetSelectionStartColumn (),
    GetSelectionEndColumn ()

  -- if a single line selected, take that word  

  if startline &gt; 0 and 
     endline == startline and
     startcol &gt; 0 and
     endcol &gt; 0 then
    local word = string.sub (GetLineInfo (startline, 1), startcol, endcol - 1)

  -- sanity check on word (in case whole line selected)

    _, _, target = string.find (word, "^([A-Za-z0-9]+)$")
  end

  -- otherwise prompt for one

  if not target then

    target = utils.inputbox ("Enter the name of the desired target", 
                             "Select target")
  end -- if no target selected

  -- echo new target name
  
  if target then
    ColourNote ("white", "blue", "Target now " .. target)
    SetVariable ("target", target)  -- copy into MUSHclient variable
  end -- have a target
end -- do
</send>
  </alias>
</aliases>

</muclient>



(The added stuff, compared to the original post, is in bold).

However your problem is that the 'target' variable is local to the plugin. To use it you either need to add extra aliases which use that variable (eg. see http://mushclient.com/faq point 22) into that plugin, or grab that variable from other plugins by using GetPluginVariable:

http://www.gammon.com.au/scripts/doc.php?function=GetPluginVariable
Amended on Tue 15 May 2007 02:38 AM by Nick Gammon
#4
How may I convert this into a function so I can put what the alias does into my script file?

I'm fine with calling a function from my alias, just couldn't write the correct lua script for this:
function clicktarget(name,output,wildcs)
[Then copy and pasted everything from Do to End here]
end

This was the error I got:
[string "Script file"]:34: 'then' expected near '&'

=================================================
Also, I'd like to save the target variable in my table, for example:
target = {}

And the function will save it into target.t1
=================================================
So my two questions are how can I modify the function to
-put it in my script file
-and save the variable into a table

Thanks
USA #5
To get rid of the error, change the instances of &gt; to > since you aren't using xml in the script file. As for the target.t1, just do exactly what you said with target.t1 = foo
#6
Thank you, I got it to work in a script now!

I'm still not sure where to put the
target.t1 = foo

And what does " = foo" mean?
USA #7
Sorry, "foo" is a programming term meaning "something goes here, but it will be different in each application, so I'll just fill in the word foo." I'm not sure exactly what you're doing with the table, so I left a "foo" in there so you can fill in whatever you would need to complete your script.
#8
It's alright, I've solved it. Thanks for your help!
#9
hi i added the lau version of double click to target something which worked great. So all i have to do is double click some ie rat, but what i dont understand to do is make an alias so that i staffcast scintilla at (???), what do i type in the parenthesis to get to the target. I tried %t and @t and i tried making a variable so name is t and the contents is target, but i get this error
Compile error
World: Achaea
Immediate execution
[string "Alias: "]:1: '=' expected near 'scintilla'
Amended on Sun 02 Dec 2007 06:03 AM by Benjamin
Australia Forum Administrator #10
You have to post more than that. You have given the error message but not what is causing the error.