Friday 10 December 2010

Option Funcs 3

We need to change Option:test so that it runs the Func and returns wahtever value the Func returns. Easy enough:

function Option:test(userdata)
        return self.func:exec(userdata)
end


The only complication was the userdata table. We're expecting to test this, which means it needs to be passed to the func. Which means that Option:test method needs to take userdata as a parameter.

Hmm... this was unexpected:

  2) Error! (narrator_tests.test_option_func1):
../Scripts/Option.lua:30: attempt to index field 'func' (a nil value)


Let's add an explicit test for a nil func

        --
        -- just to make sure
        --
        assert_equal("foo", option.text)
        assert_equal("bar", option.next)
        assert_not_nil(option.func)


... and that bit passes. It turns out the problem is here:

        rc = option.test(ud)
Which should read

        rc = option:test(ud)


It's a Lua thing. calling foo:bar() is shorthand for foo.bar(foo) in lua, and is how object invocations get the "self" parameter. But it means that if you forget and use "." then you're missing a parameter, and all your object data. I fixed that, and made the test access "ud" rather than "userdata" which didn't exist in the calling scope.

And after that, it worked fine.

So.. time to pop the stack again. Back to conditionals in choices

No comments: