The hard part at scripting is getting the state machine working. For those of you who have never programmed on a state-based system before (programming for Windows, the Macintosh, or KDE/Gnome works in the same way) and just have looked at the several scripts, it looks easy, doesn’t it? After all, everything you do puts the object in a “state” – either touched, or worn, or listening to voice commands on the chat line, or getting permissions from the user, etc. So it should be just an easy matter of defining the right responses to each state, right?
Wrong. I thought the same.
My first example was the Cigarette Vending Machine here. You can get cigarette packs from it, each one costs L$3, and gets you 20 cigarettes in a pack. The pack will happily give you those 20 cigarettes, and then will silently refuse to give you more (you got to buy a new pack then). Each cigarette lasts around 6 minutes or so.
All these things are fairly easy to do – you got events for dealing with retrieving payments, giving objects to users, tracking counters, and timer events.
The trick is handling the “correct state”. All objects in Second Life are “always running” from the moment they’re first created. This means that if you drop them on the floor and pick it up again, they will be in the same state (although probably they’ve called a few events in between). If you give them to someone else, the state won’t change. Alas, if you give permissions for an object to animate your avatar, and give it to someone else, this doesn’t mean he/she gets permission to animate him/herself, too!
Finding out which state you’re in and resetting things properly IS painful! For instance, with the cigarette pack, if someone smokes 5 cigarettes and gives the pack to a friend, it should properly still have 15 inside, so if by any chance the pack gets reset, the counter shouldn’t go up to 20! On the other hand, when building the cigarette pack, testing it, and dropping it into the cigarette vending machine in the first place – the state has changed already! When someone pays for the pack, and is given a copy from the vending machine’s own inventory, what state is it in?
Fortunately, making a COPY of an object resets it. It’s just a new object. But it’s not always easy to understand what is a “copy” and what is “the same object”…
Clearly finding out how things work properly in Second Life is NOT easy. I still have some quircky bugs creeping along my creations…
Scripting nightmares by Gwyneth Llewelyn is licensed under a Creative Commons Attribution 4.0 International License.