OK, let's make a simple Colossal-Cave-Adventure-style two-word parser.
Lil's parsing patterns can get a little bit cryptic, but essentially what we want to break up user input is:
- Skip any leading spaces.
- Grab any characters up to a space or newline and call them "verb".
- Skip any spaces.
- Grab any characters up to a newline and call them "obj".
Which can be expressed as the pattern:
pat:"%*r %[verb]-.2r \n%*r %[obj]s\n"
I did a few tests in the Listener to make sure it was working as intended:
pat parse "foo" {"verb":"foo","obj":""} pat parse " foo" {"verb":"foo","obj":""} pat parse "foo bar" {"verb":"foo","obj":"bar"} pat parse "foo bar\nbaz" {"verb":"foo","obj":"bar"} pat parse "foo bar\nbaz" {"verb":"foo","obj":"bar"}
Now we can rework the input field's change[] handler to bundle this up and send the verb/object to the card script:
on change do if "\n" in me.text c:"%*r %[verb]-.2r \n%*r %[obj]s\n" parse me.text me.text:"" typed[c.verb c.obj] end end
In the card script, we'll have our actual game logic, a twisty maze of if statements:
on println x do log.text:log.text,x,"\n" log.scroll:999999 end on typed verb obj do println["> %s %s" format verb,obj] if verb~"look" if obj~"flask" println["it is a flask."] elseif obj~"self" println["that's difficult unless your eyes are prehensile."] else println["ye see ye flask."] end elseif verb~"take" if obj~"flask" println["ye cannot take ye flask."] else println["i don't see a %s anywhere here." format obj] end else println["ye is speakin' nonsense."] end end
And our scintillating gameplay experience begins: