I'm about to drive this bus over a ******* cliff!

I have a bus that shows up and then leaves (it's not really a transit system, just a really simple and random way of making it show up, which works for me).

I'm having a slight problem when the bus is leaving though.

The code:

if (bus.here = true) {
  SetTimeout (5) {
    MoveObject (bus, VOID)
    msg ("<small><font color=blue>The doors on the bus closes and it drives away.</font></small>")
    DisableTurnScript (busroute2)
    EnableTurnScript (busroute)
    MakeExitInvisible (busdoor)
    bus.here = false
  }
}

The problem:

IF I do nothing when the bus has arrived (ie not typing anything), then, after 5 secondsthe bus leaves and the msg is printed once.

If however I do something (anything!) while the bus is there, the 5 seconds is obviously prolonged and the message that's supposed to get printed once, get printed once for every turn I've made during the timer.

Example:
Bus shows up.
//I do nothing, after 5 sec the bus leave and
The doors on the bus closes and it drives away.

Example:
Bus shows up.
//thing go in the parser (i'm using 'wait' for the example):
wait
wait
wait
// Once I stop feeding the parser, this happens.
The doors on the bus closes and it drives away.
The doors on the bus closes and it drives away.
The doors on the bus closes and it drives away.
The doors on the bus closes and it drives away.

If I were to enter 11 commands, the "bus leaving"-msg would get printed 12 times.
The timer is set to 5 sec for me to try things out but it'll be longer, say about 20 sec.
The player will be able to enter a whole lot of stuff in 20 sec which will result in a flood of
The doors on the bus closes and it drives away.-messages.

That bus is freaking crazy!
And I can't figure it out.

(I've tried switching the timer for a turntimer but with the same result. If I set it to 5 turns, it gets printed once for every turn)


And, if needed, this is the turnscript used for making the bus show up called "busroute". The one in the last post is "busroute2"

if (RandomChance (25)) {
  MoveObject (bus, dirtroad)
  msg ("<small><font color=blue>A bus drives up to the bus stop, comes to a halt and the doors are opened.</font></small>")
  MakeExitVisible (busdoor)
  DisableTurnScript (busroute)
  bus.here = true
  EnableTurnScript (busroute2)
}
else {
}

K.V.

Let's look at them together:

  <turnscript name="busroute">
    <script><![CDATA[
      if (RandomChance (25)) {
        MoveObject (bus, dirtroad)
        msg ("<small><font color=blue>A bus drives up to the bus stop, comes to a halt and the doors are opened.</font></small>")
        MakeExitVisible (busdoor)
        DisableTurnScript (busroute)
        bus.here = true
        EnableTurnScript (busroute2)
      }
    ]]></script>
  </turnscript>
  <turnscript name="busroute2">
    <script><![CDATA[
      if (bus.here = true) {
        SetTimeout (5) {
          MoveObject (bus, VOID)
          msg ("<small><font color=blue>The doors on the bus closes and it drives away.</font></small>")
          DisableTurnScript (busroute2)
          EnableTurnScript (busroute)
          MakeExitInvisible (busdoor)
          bus.here = false
        }
      }
    ]]></script>
  </turnscript>

Let's say that the player is in dirtroad and the busroute turn script fires and the random chance is successful.

1. The bus shows up in the pane.

2. We read: A bus drives up to the bus stop, comes to a halt and the doors are opened.

3. A new exit appears on the compass (whatever the alias for busdoor is).

4. bus.here is set to true

5. The turn script busroute2 is enabled.

6. The turn script busroute is disabled.


Now, no matter what we do this turn, this will happen FIVE SECONDS afterwards:

1. The bus object is moved to the VOID object.

2. We read this: The doors on the bus closes and it drives away.

3. The busroute2 turn script is disabled.

4. The busroute turn script is enabled.

5. bus.here is set to false.


NOTE:

Every time the busroute turn script is enabled and the random chance succeeds, bus.here is set to true and the busroute2 turn script is enabled, and this happens over and over again:

1. The bus object is moved to the VOID object (whether it is already there or not).

2. We read this: The doors on the bus closes and it drives away.

3. The busroute2 turn script is disabled.

4. The busroute turn script is enabled (which makes all of this happen again after 5 seconds from the time the command is entered).

5. bus.here is set to false.

6. The busroute turn script is now enabled, so if the random chance succeeds, bus.here is set to true, and this loops.


EDITED

You probably need to put a SetTurnTimeout on the line that enables the turn scripts.

  <turnscript name="busroute">
    <script><![CDATA[
      if (RandomChance (25)) {
        MoveObject (bus, dirtroad)
        msg ("<small><font color=blue>A bus drives up to the bus stop, comes to a halt and the doors are opened.</font></small>")
        MakeExitVisible (busdoor)
        DisableTurnScript (busroute)
        bus.here = true
        SetTurnTimeout(1){
          EnableTurnScript (busroute2)
        }
      }
    ]]></script>
  </turnscript>
  <turnscript name="busroute2">
    <script><![CDATA[
      if (bus.here = true) {
        SetTimeout (5) {
          MoveObject (bus, VOID)
          if (bus.parent = game.pov.parent){
            msg ("<small><font color=blue>The doors on the bus closes and it drives away.</font></small>")
          }
          DisableTurnScript (busroute2)
          SetTurnTimeout(1){
            EnableTurnScript (busroute)
          }
          MakeExitInvisible (busdoor)
          bus.here = false
        }
      }
    ]]></script>
  </turnscript>

> z
Time passes.
A bus drives up to the bus stop, comes to a halt and the doors are opened.

> z
Time passes.

> z
Time passes.
The doors on the bus closes and it drives away.

> z
Time passes.

> z
Time passes.
A bus drives up to the bus stop, comes to a halt and the doors are opened.


K.V.

A Public Service Announcement Concerning SetTimeouts

If you're not pausing play, I advise against using a SetTimeout() to print a message. Chances are, the player is moving through the game quickly, especially if this is not the first time they've been in this location. If the player enters a command before your timeout fires, the message will print after the output of a different turn than you may have originally planned.


Looking at your code ... you want the bus to leave 5 seconds after every action the player performs while the bus is there?

If you only want it to happen once, then put the if (bus.here) { inside the SetTimeout (5) {. When the timeout fires, if the bus hasn't already left, the message will be printed.

I don't see why you have the busroute2 turnscript, though.

Why not have the busroute turnscript be:

if (RandomChance (25)) {
  MoveObject (bus, dirtroad)
  msg ("<small><font color=blue>A bus drives up to the bus stop, comes to a halt and the doors are opened.</font></small>")
  MakeExitVisible (busdoor)
  bus.here = true
  DisableTurnScript (busroute)
  SetTimeout (5) {
    if (bus.here = true) {
      MoveObject (bus, VOID)
      msg ("<small><font color=blue>The doors on the bus closes and it drives away.</font></small>")
      EnableTurnScript (busroute)
      MakeExitInvisible (busdoor)
      bus.here = false
    }
  }
}

Then the bus leaves 5 seconds after it arrives.

Or, if you specifically want it to be "1 turn plus 5 seconds":

if (not bus.here) {
  if (RandomChance (25)) {
    MoveObject (bus, dirtroad)
    msg ("<small><font color=blue>A bus drives up to the bus stop, comes to a halt and the doors are opened.</font></small>")
    MakeExitVisible (busdoor)
    bus.here = true
  }
}
else {
  DisableTurnScript (busroute)
  SetTimeout (5) {
    if (bus.here = true) {
      MoveObject (bus, VOID)
      msg ("<small><font color=blue>The doors on the bus closes and it drives away.</font></small>")
      EnableTurnScript (busroute)
      MakeExitInvisible (busdoor)
      bus.here = false
    }
  }
}

K.V.

That last block of code posted by mrangel seems like the best way to handle it.


The reason I had two turnscripts was only to get an overview of how I fudged up. Although; I didn't work it out without help.

Your code works very well MrAngel!

Thanks guys (for your input as well K.V.!)
As always; it's highly appreciated!

/cheese


Actually, I think the best way is more likely:

<changedparent type="script">
this.here = (this.parent = dirtroad)
busdoor.visible = this.here
if (player.parent = dirtroad) {
  msg ("<small style=\"color:blue\">{either bus.here:A bus drives up to the bus stop, comes to a halt and the doors are opened.:The doors on the bus close and it drives away.}</small>")
}
</changedparent>

<turnscript name="busroute"><script>
if (bus.here) {
  DisableTurnscript (this)
  SetTimeout(5) {
    bus.parent = VOID
    EnableTurnscript (busroute)
  }
}
else if (RandomChance(25)) {
  bus.parent = dirtroad
}
</script></turnscript>

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums