Thursday, 9 December 2010

Back to conditionals...

With the jump tag working, the conditional test now passes. Unfortuneately, if does't actualy work, so I need to extend the test so that it fails again:

        rc = narrator:play(stage)
        assert_true(rc)

        assert_equal(2, stage:num_oq())
        assert_equal("text", stage:oq(1).type)
        assert_equal(
                "You don't like it in this room. What do you want to do?",
                stage:oq(1).value
        )

        local options = stage:oq(2).options
        assert_not_nil(options)
        assert_equal(2, #options)


If the condition worked, I'd only see two options until I'd visited the other two branches. Of course, it would help if I specified the condition in the first place...

          <Choice>
           <Option      Text = "Sulk"
                        Next = "sulking"
           />
           <Option      Text = "Complain"
                        Next = "moaning"
           />
           <Option      Text = "Try The Door"
                        Next = "thinking"
           >
            <Func>
                 userdata.test_ran = true
                 return userdata.sulk == true and userdata.moan == true
            </Func>
           </Option>
          </Choice>

So that opens up the Option tag to enclose a Func. I was going to have a Condition object and tag, ut why create an extra object when Func can do it already. William of Occam would approve, I feel :)

So the condition is a Func,. which is essentually a lua function, minus the  "function foo()" at the top, and  the "end" to finish up. I decided to set a flag as well, so I can test to see if it runs. Would simplify bug finding if I know for a fact whether the code ran or not.

And then

        rc = narrator:play(stage)
        assert_true(rc)

        assert_not_nil(narrator.userdata.test_ran)
        assert_true(narrator.userdata.test_ran)

And that fails because the test_ran flag never gets set, which is hardly a surprise. I need to change the Option class to incorporate the test, and to make use of it.

This is the code I'm adding to the constructor:

        while true do
                i = i + 1
                local raw_item = xml[i]
                if raw_item == nil then
                        break
                end
                self.func = Func:new(raw_item)
        end


That's assuming that anything between option tags is going to be a set of Func tags (reasonable) and that there will only be one Func (although you could make a case for multiple simple tests and only proceeding if all of them evaluated true. We'll make do with it as it is for the time being; if I ever get around to writing a SceneEditor app then it probably won't matter.

We need a func to tell if an option is valid or not... and this is getting complicated enough that it's worth writing a separate test to make sure the option behaves as expected. Deja Vu, anyone?

No comments: