Dynamic actor creation

I keep getting the “nil object reference” error when I try to dynamically create actors and then move them places.

Here are the relevant bits from my code:

modify Actor
    construct() {
        name = 'testman';
        vocabWords = 'testman test man';
        isHim = true;
        initSpecialDesc = 'look a guy';
        location = startRoom;
        moveIntoForTravel(startRoom);
    }

DefineIAction(Test)
    execAction()
    {
        local testman = Actor.createInstance();
	location = startRoom;
        moveIntoForTravel(startRoom);
    } 
;

It only crashes when I’m in the room the actor is supposed to be in.

I’m having a hard time understanding TADS/figuring out what I’m doing wrong, so any help would be appreciated.

Have you tried googling the error? I see it associated with using vocabulary that is not defined.

I don’t know a lot about T3, but in TADS2 you would have to look like this:

DefineIAction(Test)
    execAction()
    {
        local testman = Actor.createInstance();
	testman.location = startRoom;
        testman.moveIntoForTravel(startRoom);
    } 

Also, unless you need to dynamically create people/objects programmatically, I’d create all the objects and give the ones that are “offstage” a location of nil. Then move them into the room at the appropriate time.

Hi… Alan’s right, for the most part you should create all of your actors ahead of time, locating the ones that will appear later in nil. However, there could be a scenario where you want to randomly generate new people (although that may not be what you intended here).
First off, if you want to work with TADS, you really should read all the way through Learning TADS 3 by Eric Eve. You should get a feel for the best way to code in TADS if perhaps you’ve come from a different programming background.
I’ll try to get back to you soon with a little snippet of how you could dynamically create actors, but once again, that may not be what you’re actually trying to do…

Try this:

 VerbRule(testman) 'testman' : IAction
   execAction { local f = new Actor(rand('Aa{3,6}'));  //construct with random name
        f.initializeActor;      //lacking this is prob why you got nil obj ref
        Actor.dynamicActors.append(f);  // in case you need a ref to it later
        f.moveIntoForTravel(startRoom); 
        gActor.lookAround(true);   //assuming you're in startroom you should see the new Actor
     } 
   ;

modify Actor construct(nam?) { inherited; name = nam; } 
   dynamicActors = static new Vector(10)
   ;

Also as Alan noted in the last two lines of your execAction method,
location = startRoom and moveIntoForTravel(startRoom) are called without being attached to anything meaningful. It is also conventional in TADS3 never to assign a new location to an object in procedural code (of course you can assign a location to statically defined objects using + and @…). If an object changes location during the program (or is dynamically created subsequent to starting the program), you should use one of the “moving” methods: moveInto, moveIntoForTravel, baseMoveInto, etc. These methods keep an object’s location in sync with the location’s list of contents (which is important for avoiding crashes and bugs).