Friday, 10 December 2010

Conditionals Once Again

When last we looked at conditional options in menus, we had this:

  1) Failure (narrator_tests.test_condition1):
test_narrator.lua:655: nil not expected but was one


Which is this assertion:

        assert_not_nil(narrator.userdata.test_ran)

That's still failing. Since we're now fairly confident that the Func and Option
code is working as we expect, the chances are that the func isn't being called.
So that means we need to look at Narrator.

Currently the "choice_show" handler looks like this:

        elseif item.type == "choice_show" then
                interface:choice_show(item.options)


I don't want to push the conditional evaluation into the interface code
so we need to expand this a bit, making a opy of the options array containing
only options that pass the test.

Let's start by moving the code into its own method

        elseif item.type == "choice_show" then
                self:do_choice_show(interface, item)


And then

function Narrator:do_choice_show(interface, item)
        interface:choice_show(item.options)
end


Now: about that loop:

function Narrator:do_choice_show(interface, item)
        local i, v
        local valid_opts = {}
       
        for i,v in ipairs(item.options) do
                --
                -- if the test is absent, that means it's always valid
                --
                if v.func == nil then
                        table.insert(valid_opts, v)
               
                --
                -- if we get to this one, we have a test
                -- so we run it to see if it should be included
                --
                elseif v:test(self.userdata) then
                        table.insert(valid_opts, v)
                end
        end

        interface:choice_show(valid_opts)
end


And, slight to mmy surprise, that passes the test. We're not done yet, however.

No comments: