Maybe useful - preventing infinite recursion


I've been playing around (again) with my random forest script; adding more realism. Now, I've got all the exits set up so that changing their parent or destination will adjust the exit in the other direction too; and will call a changedexits script on the rooms at either side.

Now, I've got a room whose changedexits script can move its exits around; calling the scripts on its neighbours. But if two of these rooms sit next to each other, they could end up calling each other recursively. But I found what I think is a fairly neat way to arrange it so that it doesn't call itself more than once.

In the initialisation script for a certain room:

this.realchangedexits => {
  this.changedexits => {
    debug (" * skipping "".changedexits - called recursively")

  // do some stuff
  // ...
  // move around my neighbours' exits
  // ...
  // do some more stuff
  // update my description based on my current exits
  // ...

  this.changedexits = this.realchangedexits
do (this, "realchangedexits")

I know I could do this by setting a boolean flag and nesting the whole function in an 'if' block; but this way seems a bit neater. And also reduces (slightly) the problem of code being unmanageable in the GUI view due to too many levels of indentation (and also seems to render slightly faster in the web editor, so less of my editing time is spent waiting for the thing to load)

Log in to post a reply.