A Quick I6 Tip: Local Routine Arrays

I know, I know — most IF authors could care less about Inform 6. But this is kind of cool, so I thought I’d share it with anyone who actually does still use I6. You can create local arrays of routines, like this:

routine_array [; "Fred!"; ]
   [; "Wilma!"; ]
   [; "Barney!"; ]
   [; "Betty!"; ],

You’ll note that there are no commas after the routines, until the end (when presumably you’re about to declare another property), and they have no individual names. Having set it up this way, you can access the elements of the array like this:

   y = some_object.&routine_array-->x;

This is potentially useful because it lets you encapsulate different kinds of behavior and then switch among them at run-time. You could do the same thing with a switch statement, but this way of doing it is likely to be easier to read and debug than a massive block of code in a switch. Fewer indents, if nothing else.

Since I6 doesn’t force the elements of arrays to be of a single type, you can also build property lists like this that contain routines, double-quoted strings, and object IDs. You then use:

   if (y ofclass Routine) y();
   else if (y ofclass String) print_ret (string) y;

…and so on, in order to use the elements in the array.

This entry was posted in Interactive Fiction. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s