Custom methods of actions object of SimpleSituation is undefined [Undum 2.x]

I’m working with Undum 2.x and I’m trying to wire up methods of the config objects passed as params when creating a new SimpleSituation such that text descriptions change programmatically, but I keep getting undefined reference errors when calling the methods.
Here’s an example:

undum.game.situations = {
main: new undum.SimpleSituation(
    "",
    {
        enter: function(character, system, from) {
            ...
        },
        actions: {
            testMethod: function() {
                return "hello from test method";
            },
            'test-action': function(character, system, action) {
                console.log("testMethod for main situation says: "+this.testMethod());
                ...ideally system.write() contents would be determined by the method call.
            }
        }
    }
),
...other situation defs
}

When I click on the test-action link, the console shows “Uncaught TypeError: this.testMethod is not a function”. Any idea what’s going wrong?

1 Like

In the context where the action function is called, this refers to the window object. You’ll have to move the function outside the SimpleSituation object and call it directly:

function testMethod() {
    return "hello from test method";
}

undum.game.situations = {
main: new undum.SimpleSituation(
    "",
    {
        enter: function(character, system, from) {
            ...
        },
        actions: {
            'test-action': function(character, system, action) {
                console.log("testMethod for main situation says: "+testMethod());
                ...ideally system.write() contents would be determined by the method call.
            }
        }
    }
),
...other situation defs
}
1 Like

gotcha. The specific problem here appears to come from the following Undum framework code

SimpleSituation.prototype.act = function(character, system, action) {
    // my take-action action function is extracted and stored as response, and response's calling context is as a standalone function such that keyword this points to Window object
    var response = this.actions[action];
    try {
        response(character, system, action);
    } catch (err) {
        if (response) system.write(response);
    }
    if (this._act) this._act(character, system, action);
};

any idea why Mr. Millington extracts the method and calls it as a standalone function?