Objects in class

Hello,
I want to have a class where each instance always contains certain objects. As if they were added with the + syntax to the instance.
I tried:

class test : Actor foo = new TestObject() foo.location = self ;

But the compiler throws lots of errors on line 3. It seems, that it cant parse the . which causes lots of other parsing errors.
Using the constructor in some way isn’t a solution since I will need to write and read properties of foo in tant class.

Try using constructor arguments:

class TestObject: <parent class>
{
    construct(initial_location)
    {
        location = initial_location;
    }
}

class Test: Actor
{
    foo = new TestObject(self);
}

Somehow I get a “[Runtime error: wrong number of arguments” error from the VM now.
Using the code you provided I can compile without warning, but the object is never initialized.
The constructor is never called(breakpoint in constructor).
So I overwrote the desc of the payer to call the working desc of the TestObject.

class TestObject: Possession, Thing, Component
{
    construct(initial_location)
    {
        location = initial_location;
    }
}

class Test: Actor
{
    foo = new TestObject(self);
    desc = foo.desc;
}

+ me: Test
;

The Line the error suposely happens in is the last line of the .t file that contains the TestObject class. It consists of one “;” and nothing else.

Using breakpoints I found the erorr in the " location = initial_location;" line, but that makes no sense to me. :confused:

Not sure if your Possession class messes something up :-/

A minimal game works just fine:

[code]#charset “UTF-8”
#include <adv3.h>
#include <en_us.h>

versionInfo: GameID;

gameMain: GameMainDef
{
initialPlayerChar = me;
}

class TestObject: Thing, Component
{
construct(initial_location)
{
location = initial_location;
}
}

class TestActor: Actor
{
foo = new TestObject(self);
}

startRoom: Room ‘Start Room’ "This is the starting room. ";

  • me: TestActor
    {
    name = ‘Random J.\ Actor’;
    desc = "<<foo.location.name>> ";
    }[/code]

In the ‘me’ object, ‘desc’ prints “Random J. Actor”, since ‘foo.location.name’ is ‘me.name’ (due to ‘foo.location’ refering to the ‘me’ object.)

You can also use one of the moveInto() methods instead of setting the property. Like:

construct(initial_location) { moveInto(initial_location); }

Also, if the code you posted really looks like that:

[code]class Test: Actor
{
foo = new TestObject(self);
desc = foo.desc;
}

  • me: Test
    ;[/code]

Then this will of course not work (but compile fine), since you’re trying to put the ‘me’ object (using the ‘+’ syntax) inside something that does not derive from a BasicLocation class (like Room.)

I seem to recall that T3 syntax allows classes to be created anywhere, without implications on the +/++/+++ structure. I could be mis-remembering.

@RealNC no I didn’t make that mistake, in my project nearly every paragraph of the code I posted is in its own .t file.

I finally found the error. :smiley: :smiley:
Custom constructors arn’t properly inherited!
In my code I din’t have an object of the TestObject class, but one that inherited only testobject and defined name (and in the future additional code). If you add:

construct( initial_location )
    {
        inherited TestObject(initial_location);
    }

to that class everything suddenly works.
The debugger “step within” stepped into the custom constructor of the parrent class, but the stack listed a call to a constructor without arguments. Thus “Runtime error: wrong number of arguments”.
It isn’t reproduceble in the simplyfied verson, so there are probably more conditions to triggering that error than I found, but it works now.

Anyway thanks for your help.

You are correct. I learned something new today :slight_smile: