Flags and variables in TADS3

OK! It is still missing the cannotPutInMsg bit but it should look better now :smiley:

power_plant_generator_battery_holder: OpenableContainer, SingleContainer, RestrictedContainer, Hidden, Fixture
    vocabWords = '(battery) (generator) holder/receptacle/snap/enclosure* holders receptacles snaps enclosures' 
    name = 'battery holder'
    location = Power_plant
    initiallyOpen = true
    validContents = [nuclear_Battery]
    desc()
    {
        if(nuclear_Battery.isIn(self))
        {
            "The battery rests inside. ";
        }
        else
        {
            "The battery holder is open and empty. These generators are plug and play, inserting the right battery on it should power it on. ";
        }
    }
    
    notifyInsert(obj, newCont)
    {
        if(obj == nuclear_Battery)
        {
            "When I insert the nuclear battery in the holder I hear a click and the compartment closes. ";
            self.makeOpen(nil);
            un_nuclear_Battery.makePresent();
        } else inherited (obj, newCont);
    }
    
    dobjFor(Open)
    {
        check()
        {
            if(nuclear_Battery.isIn(self))
            {
                failCheck('The generator is already on, I better don\'t mess with the battery holder anymore. ');
            } else inherited;
        }
    } 
    
    dobjFor(Close)
    {
        check()
        {
            if(!nuclear_Battery.isIn(self))
            {
                failCheck('I should not attempt to close it when there is no battery inside. ');
            } else inherited;
        }
    } 
; 

and I defined the unBattery as:

un_nuclear_Battery: PresentLater, Unthing //Nuclear battery
     vocabWords = '(nuclear) (atomic) battery*batteries'
     name = 'nuclear battery'
     location = Power_plant 
     notHereMsg = 'The battery is locked inside the generator, I don\´t need to mess with it anymore. '  
;

Everything seems to work well! I need also to take care of make the command “insert battery in generator” the same as “insert battery in holder” once the holder has been discovered, but that can wait a bit :).

3 Likes

Good!
As for the generator, within its definition I would suggest

iobjFor(PutIn) remapTo(PutIn, DirectObject, batteryHolder)

Perhaps I wasn’t clear, but when an object calls its own methods you don’t even need to use “self”! So the whole code line can just be
makeOpen(nil);
You will definitely appreciate this if you write a lot of TADS code! I learned TADS first and was a bit annoyed with Python when I found out it was mandatory to use “self” everywhere! :slightly_smiling_face:

This may be a preference thing, but it’s also acceptable to omit parentheses in method calls if there are no arguments. (This is not true for top-level functions.) Thus,
unBattery.makePresent;
is possible.

If you wish:

cannotPutInMsg(obj) {
   // if(obj==specialCase) return ‘msg’;
   //else
   return ‘Nothing goes in the holder except for a battery. ‘; 
   }
2 Likes