Area events
Posted: 25 Nov 2012, 23:14
While working on porting the cam1a mission, I was happy to see that the existing API almost completely covered my needs.
Things that I've added in this process: .stattype field for features, hackAddMessage() and hackRemoveMessage() similar to wzscript addMessage and removeMessage (the 'hack' is to flag my disgust for this code, and that this interface may change suddenly), non-droids may now be group members, eventGroupEmpty is now eventGroupLoss, and addLabel() got added.
However, one task that I felt was much harder with the javascript API than it was with wzscript was doing something when a droid enters an area for the first time. An implementation with a function on a timer that continually checks the contents of the area is possible, but not somewhat ugly, slow and a bit much code for something that should probably be a very basic operation, given how frequently this is done in campaign code.
So I was thinking of adding a new event that is triggered the first time that a droid enters an area defined in labels.ini. We know in the c++ code when a droid changes tiles, and iterating over the area labels will not cause performance problems there. My first thought was 'eventArea(label, droid)', but this quickly becomes a huge catch-all function for this kind of stuff. Then I thought, why not call 'eventArea<label>(droid)' if it exists? This should make it slightly faster, since you don't need to be invoking the javascript engine on every area transition, only those with implemented event handlers. And it should make the scripting code neater. But making up events names dynamically is a new API mechanic, so I figured I'd air it here first.
In any case, the idea was that each area has a flag associated with it, so once a droid has entered it, that flag has to be cleared before it will trigger again. So we'd need a new function 'resetArea(label)' to tell the code that we want more events triggered on this area, eg if the wrong droid entered.
Opinions?
Things that I've added in this process: .stattype field for features, hackAddMessage() and hackRemoveMessage() similar to wzscript addMessage and removeMessage (the 'hack' is to flag my disgust for this code, and that this interface may change suddenly), non-droids may now be group members, eventGroupEmpty is now eventGroupLoss, and addLabel() got added.
However, one task that I felt was much harder with the javascript API than it was with wzscript was doing something when a droid enters an area for the first time. An implementation with a function on a timer that continually checks the contents of the area is possible, but not somewhat ugly, slow and a bit much code for something that should probably be a very basic operation, given how frequently this is done in campaign code.
So I was thinking of adding a new event that is triggered the first time that a droid enters an area defined in labels.ini. We know in the c++ code when a droid changes tiles, and iterating over the area labels will not cause performance problems there. My first thought was 'eventArea(label, droid)', but this quickly becomes a huge catch-all function for this kind of stuff. Then I thought, why not call 'eventArea<label>(droid)' if it exists? This should make it slightly faster, since you don't need to be invoking the javascript engine on every area transition, only those with implemented event handlers. And it should make the scripting code neater. But making up events names dynamically is a new API mechanic, so I figured I'd air it here first.
In any case, the idea was that each area has a flag associated with it, so once a droid has entered it, that flag has to be cleared before it will trigger again. So we'd need a new function 'resetArea(label)' to tell the code that we want more events triggered on this area, eg if the wrong droid entered.
Opinions?