A TADS3 module for loading the contents of a file and assigning it to a property on an object, during preinit

Don’t know how much use anyone else has for this, but here’s a little module that lets you load a file during preinit and assign the contents to an object property: fileToProp github repo.

I use this for things like in-game documentation. For example, including a copy of the licence the game is distributed under.

Syntax is straighforward:

        someObject: object
                someProp = nil
        +FileToProp 'fileName.txt' ->(&someProp);

This will load the contents of fileName.txt and assign them to someObject.someProp. The module assumes strings, but it could be easily extended to other datatypes if needed for some reason.

The file path is the default path for the compiler, which for FrobTADS under linux means the directory the compiled story file ends up in.

The file needs to be present during preinit, which means compile time for normal builds and runtime for debug builds.

This is pretty trivial code—it’s only like a hundred lines including comments—but I like being able to load things like the licence text, credits, and so on from flat files instead of having to format them in the game’s code.


Just pushed a minor feature update to the module.

First, there’s a FileToString, which is just an alias for the FileToProp base class. Included just to provide a class name that makes the datatype explicit.

Also there’s now a FileToListInt subclass of FileToProp that treats the file contents as an list of integers. So given a file list.txt containing (note the lack of brackets):

1, 2, 3

…and a file text.txt containing:

[This space intentionally left blank]

…and the code…

someObject: object
        textString = nil
        loadedList = nil
+FileToString 'text.txt' ->(&textString);
+FileToListInt 'list.txt' ->(&loadedList);

…then after preinit someObject.textString will be the string [This space intentionally left blank] and someObject.loadedList will be the list [ 1, 2, 3 ].

There’s also a FileToListString that treats the contents of the file as an array of strings instead of integers.

Neither FileToListInt nor FileToListString do any particular data validation, so it’s up to the implementor to insure the files are property formatted.

Flat text files (like the license file) and large integer arrays (to use as lookup tables) are the two usage cases I’m personally worried about. So I probably won’t be adding additional data types unless someone asks for them.

It should be easy enough to add support for new data types: you just have to define a new FileToProp subclass and supply a setProperty() method that takes care of it. For example, the FileToListInt class is just:

FileToListInt: FileToProp
        setProperty(data) {
                local l, v;

                l = data.split(',');
                v = new Vector(l.length);
                l.forEach(function(o) { v.append(toInteger(o)); });
                (location).(prop) = v.toList();

The arg to setProperty() is a string containing the contents of the file, and when it is called location and prop are known to be set, where location.prop is where we want the result to go.