form of Execute that returns a string?

Posted by Brian on Tue 16 Nov 2010 05:23 PM — 3 posts, 12,179 views.

#0
Is there a function similar to Execute that returns a concatenatable string, rather than sending directly to world?

I am trying to make an alias that is capable of executing other aliases, and appending the output to another string, for ordering charmed followers. i.e., if I have an alias:
fs *

which sends:
cast 'flame shroud' %1

Then I would like to be able to have a second alias that lets me do something like:
oc fs *
and have it send:
order charmie cast 'flame shroud' %1
Australia Forum Administrator #1
There is no such function right now, nor is it particularly easy to do. The reason is, aliases don't just substitute one string for another. For example, aliases can call scripts, the scripts can send stuff to the MUD, open files, read databases, put things on the screen, play sounds, etc. There isn't any easy way of "capturing" all that into a string that can be concatenated later with something else.

If this is inside a plugin you could work around it a bit. If you read up on the OnPluginSend plugin callback, you will see that this can be used to find what would be sent to the MUD, and decline to send it. So your general technique could be:

  • In your second alias:

    • Set a flag (eg. deferred_send = true)
    • Save what you want to prepend somewhere (eg. extra_stuff = "order charmie")
    • Do your "fs %1" - this will attempt to send "cast 'flame shroud' mobname" to the MUD
    • Clear the flag (eg. deferred_send = false)

  • In OnPluginSend, test if the flag (deferred_send) is true. If not, just return true, so the message is sent to the MUD.
  • But if the flag is set, grab the saved extra stuff ("order charmie"), concatenate with what was going to be sent ("cast 'flame shroud') and do a Send to send the combined command.
  • Now return false so the original command is not sent


So OnPluginSend might look like this (I haven't tested this):


function OnPluginSend (sText)
  if deferred_send then
    Send (extra_stuff .. " " .. sText)
    return false -- don't send original
  end -- if

  return true  -- process it
end -- function



And your alias might do this (in a script):


deferred_send = true
extra_stuff = "order charmie"
Execute ("fs %1")  -- evaluate "fs" alias
deferred_send = false

Amended on Tue 16 Nov 2010 08:44 PM by Nick Gammon
#2
Awesome, that works great -- thanks a ton!