I’m not even sure I understand the problem I’m having. In a nutshell, I’ve defined a subclass of room. It’s not actually a room for a randomized maze (trust me – there are no mazes in this game), but it’s designed to behave rather like one, in that several rooms will have identical routines for choosing exits at random.
To this end, the exits defined for the class all have TravelMessage objects attached to them. The destination property of each TravelMessage uses rand() to grab a random room from the list of rooms in the region. This works, except that once in a while rand() will choose the room the player is already in. In that case, in place of the travelDesc, you’ll get “Nothing obvious happens.”
This destroys the illusion. So I want to be able, on the fly, to construct a list of the OTHER rooms – the rooms in the region that are not the current room – and then feed that list to rand(). This is where matters go seriously screwy. I first tried to create a chooseDestination method in the class, like this:
chooseDestination () {
local destList = [VM1, VM2, VM3, VM4, VM5, VM6, VM7, VM8, VM9, VM10];
local x = destList.indexOf(self);
local otherList = destList.removeElementAt(x);
return rand([otherList]);
}
… the idea being that chooseDestination would be called by any TravelMessage.destination in the actual room, and would return some other room, which would result in the other room being returned by the destination method. However, this fails disastrously, and I don’t know why. The first time one of these TravelMessages is called, “Nothing obvious happens” and the room you’re in blows up. The status line goes blank, all of the exits are gone, the room description is gone – most likely, you’ve moved into nil.
The most likely explanation of this is that “self” isn’t in the list, which sets x to nil.
One possible diagnosis of the problem is that I’m having trouble getting the instance object of the room itself in order to subtract it from the initial destination list in chooseDestination. I can’t pass “self” to chooseDestination as an argument, because self is the TravelMessage. I’ve tried passing lexicalParent to it as an argument and then doing local x = destList.indexOf(callingRoom), but very possibly the lexical parent is the class, not the actual room.
This is no more than a guess. Probably wrong.
I can, in fact, copy and paste a bunch of TravelMessage code onto ten different rooms and edit each of them manually so as to eliminate the original problem, but I’d sure like to understand how to code it the RIGHT way. How can I get the current instance object when I’m writing code for the class and the code is in an anonymous embedded object? Are the anonymous embedded objects in a class definition separate objects for each instance of the class, or am I trying to access the same TravelMessage from various rooms? The latter possibility wouldn’t explain the error I’m getting, because the error happens the very first time the TravelMessage tries to access chooseDestination().
Suggestions cheerfully acknowledged and gratefully experimented with!