Run on load

Hello. I am trying to pick a random TopicEntry from a Shuffled List, but I always end up with interested = []. Nothing is added to it, and I’ve tried execute(), construct(), even beforeAction() as a last ditch effort. Can anyone help?

possibleInterests : ShuffledList [cheese, honey, money, trees, patrick, sun]; // Each item is a AskTopic, 
...
// This is our Generic character, otherwise, Basic Tenant.
class GenericCharacter : Person
    properName = nil
    makeProper() // This proper definition helps with proper output. (Turning 'he/she...' into 'Bob/Bobette...')
    { 
        if(properName != nil) // Make sure we have a name.
        { 
            name = properName; // Make our name the proper name.
            initializeVocabWith(properName); // We need to be able to reference this name.
            isProperName = true; // to make sure {the bob/he} works as expected.
        } 
        return name; // For compactness.
    }
    curState = nil // Initialize with no current state. 
    //No need to initalize names and such -- Those are defined per-object.
    
    maxInterests = 3 // The maximum amount of interests a person can have. 
                      // Must be less than the # of interests you have,
    getInterests() // Our random interests. Using initObject or preInit or execute or others doesn't work, so this name is a placeholder.
    { 
        for(local a = 0; a++; a <= rand(maxInterests) + 1) {
            interests.append(possibleInterests.getNextValue());
        }
    }
    
    interests = []
    
    randAskState : ActorState {} // State in which we respond to a random dummy-list of interests
;

bob : GenericCharacter
    vocabWords = 'bob'
    location = plazaWestSide
    properName = 'Bob'
    desc = "My interests are: <<toString(interests[0].desc)>>"
;

hey there,
you’ve got the parts of the for loop in the wrong order. it should be

for(local a = 0; a <= rand(maxInterests) + 1; a++) {...}

oh, and welcome to the forum :slight_smile:

Thanks. Why doesn’t the compiler tell me that, then? I wasn’t looking for that :laughing:.

Alright, I did that and then went down a different route, but I either get a ‘nil object reference’ or, if I use += instead of .append(), ‘invalid datatypes for addition operator’. Which makes no sense, if one is a list, which interests should be…

possibleInterests : PreinitObject
    intr = static new LookupTable
    execute() {
        intr['0'] = cheese; 
        intr['1'] = honey; 
        intr['2'] = money; 
        intr['3'] = trees;
        intr['4'] = patrick; 
        intr['5'] = sun;
    }
;

// This is our Generic character, otherwise, Basic Tenant.
class GenericCharacter : Person
    properName = nil
    makeProper() // This proper definition helps with proper output. (Turning 'he/she...' into 'Bob/Bobette...')
    { 
        if(properName != nil) // Make sure we have a name.
        { 
            name = properName; // Make our name the proper name.
            initializeVocabWith(properName); // We need to be able to reference this name.
            isProperName = true; // to make sure {the bob/he} works as expected.
        } 
        return name; // For compactness.
    }
    curState = nil // Initialize with no current state. 
    //No need to initalize names and such -- Those are defined per-object.
    
    maxInterests = 3 // The maximum amount of interests a person can have. 
                     // Must be less than the # of interests you have,
    interests = []

    preinit : InitObject { 
        execute() // Our random interests
        {
            local psi = possibleInterests.intr; // Cache is yummy bread.
            for(local a = 0; a <= (toNumber(rand(self.maxInterests)) + 1); a++) {
                self.interests.append(psi[toString(rand(psi))]);
            }
        }
    }
    
    
    
    randAskState : ActorState {} // State in which we respond to a random dummy-list of interests
;

bob : GenericCharacter
    vocabWords = 'guy'
    location = plazaWestSide
    name = 'guy'
    properName = 'Bob'
    desc = "My interests are: <<toString(interests.length)>><<makeProper()>>"
;

so for some reason i’ve never had to do this before, otherwise i would already have known that the list operation append always returns a new list, and doesn’t modify the existing one.
i got your original code (which you probably want to revert to) working by changing that one line to

interests = interests.append(possibleInterests.getNextValue());

same goes for the + operator (though i wouldn’t have expected a different behaviour here), meaning you could also write

interests = interests + [possibleInterests.getNextValue()];

OOHHHH. Well, that makes sense, and I got rid of the nil object reference by making the direct class an InitObject (instead of a sub-class). Combining this and your code, it works! I wish the runtime error would be more helpful, though. I remember reading through the library about .append() but they never gave a snippet of adding a same list, so I was confused.