Capturing output

mrangel
19 Feb 2018, 16:20

I can think of a couple of cases where you might want to run a script you can't easily edit (one of the core functions; or part of a library); and you want to stop it from sending text to the player. Maybe you want to execute a series of commands, but change the output from those commands in some way.

Here's an idea (off the top of my head, maybe I'll test/improve it later.

<function name="StartOutputSpy">
  JS.eval("if(!realAddText){realAddText=addText;}addText=function(t){ASLEvent('os_capture',t);};")
  if (not HasAttribute(game, "outputspy_layers")) {
    game.outputspy_layers = NewList()
  }
  game.outputspy_current = NewStringList()
  list add (game.outputspy_layers, game.outputspy_current)
</function>

<function name="EndOutputSpy" parameters="callback">
  game.outputspy_callback = callback
  JS.eval("addText=realAddText;ASLEvent('os_terminate')")
</function>

<function name="os_capture" parameters="text">
  list add (game.outputspy_current, text)
</function>

<function "os_terminate">
  d = game.outputspy_current
  f = game.outputspy_callback
  p = NewDictionary()
  dictionary add (p, "text", d)
  invoke (f, p)
  game.outputspy_callback = null
  if (ListCount(game.outputspy_layers) = 1) {
    game.outputspy_current = null
    game.outputspy_layers = null
  }
  else {
    list remove (game.outputspy_layers, d)
    game.outputspy_current = ListItem(game.outputspy_layers, ListCount(game.outputspy_layers)-1)
  }
</function>

Then you can use it like…

redactor => {
  foreach (line, text) {
    outputline = ""
    foreach (word, Split(line, " ")) {
      if (GetObject(word) = null) {
        outputline = outputline + " " + word
      }
      else {
        outputline = outputline + " " + "[REDACTED]"
      }
    }
    msg (outputline)
  }
}
StartOutputSpy()
ShowRoomDescription()
EndOutputSpy(redactor)

... in this example, it shows the room description with all object names changed to "[REDACTED]".
Yes, it's a silly example. But it might work even if the room's description is a script, because it's overriding addText.

I'm sure there are more sensible uses for this. And the little obsessive bit of my brain made outputspy_layers because I want it to work correctly even if calls to these functions are nested.


K.V.
19 Feb 2018, 16:48

This looks pretty cool!