[I6] Is the mechanism for invoking an action available for me to use?

I’m new to Inform6 but having a blast. This is my first question
here.

The Inform6 standard library call the appropriate section of a
before/after/life routine by setting the global variable action
before calling the routine:

Object  foo
 with   bar [ ;
         MyAction: "MyAction";
        ];

[ MyActionSub ;
    action = ##MyAction;
    foo.bar();
];

Tested like this:

>; myactionsub()
; myactionsub ( )
MyAction
; == 1

DM4 does not explain this mechanism. Should I consider it unspecified
behavior and out of bounds for a game that wishes to adhere to the
standard?

The standard library is not a standard to adhere to. It’s a bunch of code that you can poke into, if you want, or alter parts of, if you want.

I’m not sure if the manual explains this in so many words, but it’s one of the pillars of the language. If a routine contains a bunch of action constants as case labels (like your bar property, or any normal before/after/life property), then it’s implicitly a switch() on the action global variable. Except you leave off the ## part of the constants.

The action global is just a variable; you can change it if you like. However, you might not want to if you’re going to continue with normal action processing (e.g., after routines). If you don’t want to alter the global notion of what action is happening, you might want to rely on an explicit switch() statement and pass in an argument. There are other possibilities too.

1 Like

This is good to know. I don’t actually have a need to do this right now, but I like knowing all the little bits and pieces of a language so that I can use them if needed. Thank you for both of your helpful answers.

It’s useful to know how it works when you are not using the library; it’s the internal variable sw__var which is tested by the implicit switch():

Object foo
   with bar [ ;
      MyAction1: "MyAction1";
      MyAction2: "MyAction2";
      MyAction3: "MyAction3";
   ];

[ MyAction1Sub; ];
[ MyAction2Sub; ];
[ MyAction3Sub; ];

[ main   key;
   sw__var = random(##MyAction1, ##MyAction2, ##MyAction3);
   foo.bar();
   @read_char 1 ->key;
];

The same object foo, but with the switch() displayed:

Object foo
   with bar [;
      switch (sw__var) {
         ##MyAction1: "MyAction1";
         ##MyAction2: "MyAction2";
         ##MyAction3: "MyAction3";         
      }
   ];
3 Likes