Flexible offensive aliases

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Flexible offensive aliases

Post 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 :)

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: Flexible offensive aliases

Post 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] ) )

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Flexible offensive aliases

Post by Vadi »

Yep, that's better even.

derekperrault
Posts: 14
Joined: Sun May 31, 2009 7:40 am

Re: Flexible offensive aliases

Post by derekperrault »

Or just:

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

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Flexible offensive aliases

Post by Vadi »

:o thank you.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: Flexible offensive aliases

Post by Caled »

Would it not need to be:

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

derekperrault
Posts: 14
Joined: Sun May 31, 2009 7:40 am

Re: Flexible offensive aliases

Post 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.

juraj
Posts: 15
Joined: Wed Jan 13, 2010 2:47 pm

Re: Flexible offensive aliases

Post 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?

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Flexible offensive aliases

Post 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

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Flexible offensive aliases

Post 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.

Post Reply