Page 1 of 3

Flexible offensive aliases

Posted: Thu May 14, 2009 11:58 pm
by Vadi
Short aliases that function on a pre-set target variable are great, except when you don't want to keep changing your target. So I tend to use flexible ones - you can either use the alias itself only to act on the pre-set target, or supply a different name and it'll use that instead of the target.

For example, our alias "dd" will either kick the target when you just do "dd", or kick a rat when you "dd rat".

So, now for the alias code itself.

Regex:

Code: Select all

^dd(?:\s(\w+))?$
Code:

Code: Select all

send("kick " .. (matches[2] or target))
Feel free to use this template for your own aliases. I found this quite useful :)

Re: Flexible offensive aliases

Posted: Fri May 15, 2009 2:18 am
by Caled
To make it a little easier each time:

Code: Select all

function tar( t )
	if t == nil then
		return target
	else
		return t
	end
end
Same alias pattern as yours, but for the command just call the function like so:

send( "kick " .. tar( matches[3] ) )

Re: Flexible offensive aliases

Posted: Fri May 15, 2009 5:50 am
by Vadi
Yep, that's better even.

Re: Flexible offensive aliases

Posted: Sun May 31, 2009 7:43 am
by derekperrault
Or just:

send("kick " .. (matches[3] or ""))

Re: Flexible offensive aliases

Posted: Sun May 31, 2009 10:56 am
by Vadi
:o thank you.

Re: Flexible offensive aliases

Posted: Sun May 31, 2009 2:23 pm
by Caled
Would it not need to be:

send("kick " .. (matches[3] or t))

Re: Flexible offensive aliases

Posted: Sun May 31, 2009 7:11 pm
by derekperrault
Caled, yes, you are correct. I don't have a target variable, so for my uses, that empty string ensures the alias targets the mob I'm currently fighting.

Re: Flexible offensive aliases

Posted: Wed Jan 13, 2010 3:01 pm
by juraj
Could I please get an explanation of the "(matches[3] or t)" part? Does that choose whether or not to use matches[3] based on whether or not it has a value of nil?

Re: Flexible offensive aliases

Posted: Wed Jan 13, 2010 4:40 pm
by Vadi
Pretty much! Lua statements are special in the fact that they can return the value of one of the variables matched in a certain condition, not just true/false: http://lua-users.org/wiki/ExpressionsTutorial

Re: Flexible offensive aliases

Posted: Wed Jan 13, 2010 5:05 pm
by Heiko
a = b or c means: if b is undefined i. e. b = nil, then a = c

At the time the original posting was written matches[3] would have been undefined if capture group #2 was empty.
Today, this behaviour has been changed. All capture groups in the regex will be defined and empty capture groups will contain an empty string i.e. "". When the original post was written a script like send( matches[3] ) would have caused a Lua error whenever capture group #2 was empty.

Code: Select all

t = "hello"
send( matches[3] or t )
sent the default value "hello" in case matches[3] was empty. That was the whole point of the expression. Today, it's meaningless in this respect. Today you can make good use for this sort of expression when initializing variables e.g.

Code: Select all

b = c or 0
-> if c is undefined at the time this script is run, initialize it to 0 instead of causing a Lua runtime error.