ThePix / QuestJS

A major re-write of Quest that is written in JavaScript and will run in the browser.
MIT License
66 stars 13 forks source link

default.js doEvent: EventIsActive() does not work #20

Closed Kln95130 closed 3 years ago

Kln95130 commented 3 years ago

I encountered the following issue, when trying to insert a turnscript event. The event is not recognized as active, despite having eventActive: true. After investigating, I found that it was caused by this check:

  eventIsActive:() => this.eventActive,
  doEvent:function(turn) {
    debugmsg("this=" + this.name);
    // Not active, so stop
    if (!this.eventIsActive()) return;
// ...

If I replace if (!this.eventIsActive()) by if (!this.eventActive), the event triggers correctly. The function is not used in the rest of doEvent, so I think it is safe to do the switch.

ThePix commented 3 years ago

Thanks for spotting that. The issue is that the => operator does not allow the use of this. I have posted an updated.

The point of "eventIsActive" is to allow authors to override it in their games. If you look at the example game in the "game-eg" folder there is a flashlight that has a limited battery charge. The battery usage is tracked with a turnscript, which only runs when the flashlight is turned on. Rather than have the turn on/off script change an additional attribute "eventActive", the flashlight has its own "eventIsActive" script that tests whether the flashlight is turned on. I think this is a better approach because there is no chance of the two attributes being in mismatching states if there is only one! This is a general approach I have taken (it is not possible in Quest 5, as scripts cannot return a value).