Call last command in a trigger

Posted by Faedara on Thu 18 Nov 2010 08:54 PM — 9 posts, 35,381 views.

#0
Is there a viable Lua string for calling the last command sent to the world before a trigger was activated?

I want to create a few different triggers for varying functions in Achaea, including but not limited to unlocking/opening closed doors and entering through them ((Scriptable without this)) and drawing my weapon if I attack while it's sheathed ((Not so easy))

I use the word 'string' because I don't know the proper word if that isn't the word for a function or... bah, I just don't know the terminology yet. I need a personal tutor for this stuff, I'm supposed to be a certified genius and yet I can't figure out scripting =.w.=
Australia Forum Administrator #1
Take a look at this first:

Template:post=9108
Please see the forum thread: http://gammon.com.au/forum/?id=9108.



Also, try:

Template:function=GetCommandList
GetCommandList

The documentation for the GetCommandList script function is available online. It is also in the MUSHclient help file.



That lets you get recent commands you typed.
#2
I want to say I don't quite understand what it means by

"for k, v in pairs (GetCommandList (10)) do
Note (v)
end"

But to be honest I just don't understand how it turned the table into a variable usable in note form.

Not that I need to understand to use it, but I want to do more than just use the code, I want to understand just what makes the function tick. Or at least what k stands for and why v is calculated in pairs.
Australia Forum Administrator #3
GetCommandList returns a table of recent commands. So for example, if you did this:


emote laughs and sings
/print (GetCommandList (1) [1])  --> emote laughs and sings


So, GetCommandList (1) returns a table consisting of a single item, keyed by the key 1, which is the most recent command you typed.

Then accessing key 1 gives you that command.

In this case:


for k, v in ipairs (GetCommandList (10)) do 
  print (k, v) 
end


You get a table of 10 items, and ipairs accesses them in sequence (eg. command 1, command 2 etc.)

That was really a typo in the help, it should be ipairs not pairs, to get the commands in sequence.

k is the key (ie. 1, 2, 3, 4, 5) and v is the value (eg. "look", "sing", "kill")
Amended on Thu 18 Nov 2010 10:15 PM by Nick Gammon
#4
OH! So k assigns them a value according to when the reverse order they were sent in, so that the table is organized, while v is the actual command as it was sent?

If I got that at all correct I think I can understand it. Also, for anyone who wants it or wants to improve on it, this is the trigger I used it in:

<triggers>
<trigger
enabled="y"
match="You must be wielding a whip in order to garrote someone."
send_to="12"
sequence="100"
>
<send>for k, v in pairs (GetCommandList (1)) do
Send ("Draw whip strong")
Execute (v)
Send ("Sheathe whip")
end</send>
</trigger>
</triggers>


Right now it's more flashy than practical, but I'm sure I'll find more than a few uses for it given enough time and studying.
Australia Forum Administrator #5
Faedara said:

for k, v in pairs (GetCommandList (1)) do
Send ("Draw whip strong")
Execute (v)
Send ("Sheathe whip")
end


Yes, good. But you don't really need to loop over one item. Try:


Send ("Draw whip strong")
Execute (GetCommandList (1) [1])
Send ("Sheathe whip")

#6
I love Lua. I really do. I actually make things *more* complicated than they need to be.
Australia Forum Administrator #7
I've added GetInfo (87) to version 4.71 of MUSHclient. The usage of GetCommandList is potentially flawed, in that this is commands you actually type, not scripted sends or speedwalks. It might not matter in your case, but it might fail in other ones.

Just as one example:

/print "hello world"
/print (GetCommandList (1) [1])  --> print "hello world"


So this is printing the last thing you did, not the last thing you sent to the MUD.

Another way of working around that problem in a plugin is to use OnPluginSent. That records the last thing actually sent to the MUD (after alias expansions, speedwalks, scripts, etc.).

So for example:


function OnPluginSent (sText)
  last_command_sent = sText
end -- function


Effectively, GetInfo (87) will record that last thing for you, so you can access it without needing a plugin, or to write a small function like that.
Amended on Thu 18 Nov 2010 11:18 PM by Nick Gammon
#8
Which is exactly why I used send for the normal functions and Execute for the actual command. I use a group variables for attacking, so my arsenal is very flexible. I use K and L to set the action (which can be anything from an attack to a greeting) and E R T as targets.

When I first made this trigger I was sending KE to the world, hehehe