Flexible offensive aliases

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

Re: Flexible offensive aliases

Post by juraj »

Thanks!

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: Flexible offensive aliases

Post by naftali »

Heiko wrote: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.
This being the case, how would you remake the original alias? Is there a better way then using the following?

Code: Select all

if not matches[3]=="" then blah end

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

Re: Flexible offensive aliases

Post by Heiko »

Today you can simply write

Code: Select all

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

naftali
Posts: 138
Joined: Wed Jan 20, 2010 8:42 pm

Re: Flexible offensive aliases

Post by naftali »

What if it is not a choice between variables? For instance if I have a script that sets a variable (x) where if matches[5] exists then x=matches[5] and if it does not x=0.

User avatar
Omni
Posts: 131
Joined: Fri Feb 12, 2010 10:26 am

Re: Flexible offensive aliases

Post by Omni »

I like, but don't understand. Could I get a quick recap for those that don't understand all this. And the exact code for this. I want to make use of it. I seriously need to take the time to learn to understand all this.

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

Re: Flexible offensive aliases

Post by Vadi »

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

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: Flexible offensive aliases

Post by Denarii »

naftali wrote:What if it is not a choice between variables? For instance if I have a script that sets a variable (x) where if matches[5] exists then x=matches[5] and if it does not x=0.
Code: [show] | [select all] lua
x = matches[5] or 0
Now if you wanted to make it so that it set the variable to matches[5] if it exists AND is not an empty string without resorting to an if statement, it's slightly more complicated.
Code: [show] | [select all] lua
x = ((matches[5] ~= "") and matches[5]) or 0

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

Re: Flexible offensive aliases

Post by Caled »

Code: Select all

function pChk(wildcard, default)
    local result = default
    if wildcard ~= "" then result = wildcard end
    return result
end
In your trigger, put:
x = pChk(matches[5], 0)

Erudite
Posts: 1
Joined: Thu May 06, 2010 8:51 pm

Re: Flexible offensive aliases

Post by Erudite »

This might be a sad question to ask, but as a horrid programmer, and rather new to Lua, I cannot figure this out. Say I wanted to create an Alias for "strike <target> Neck" While I have issues with just using target already, is there a way to script it to be flexible?

Knute
Posts: 87
Joined: Fri Mar 05, 2010 12:08 am

Re: Flexible offensive aliases

Post by Knute »

You can easily script that, depending upon how flexible you want it to be.

So let's say that you have 2 variables: target and bodypart
target is the one you want to attack.
bodypart is say neck or head or whatever.

You can set up an alias to set either of them.

The code would look something like:

Code: Select all

send("strike " .. target .. " " .. bodypart)
Notice that there is a space after strike, and another space in between target and bodypart in the quotes. That is necessary to prevent the output to be sent as "strike<target>Neck" rather than "strike <target> Neck" as the concat function (the two dots) doesn't add spaces so we have to add them manually in the script.

HTH

Post Reply