Defined in Object.Def Mixes in with Object
Many classes have an Init method that the system calls when a new class member is created with New. The system doesn't call Init when it loads in static objects defined in an object definition file, however. If you need to initialize those objects, you will find that the HasLoad mixin comes in handy. The HasLoad mixin provides you with just one method: the Load method. The system calls Load when those pesky predefined objects are created. Override Load to do any intialization processing or checking.
Note: Class HasLoad has been declared obsolete except for cases of per instance initialization.
The HasLoad class mixes in with any subclass of the Object class.
Here is a quick list detailing the differences among various initialization methods:
Method | Description |
---|---|
Object_Install | Called on each class which overrides install, every time the device is turned on. |
Object_Reset | Called on each class which overrides reset, every time the device is reset. |
HasLoad_Load | Called on static instances (from object definition files) of a class descending from HasLoad, every time the device is turned on. |
Object_Init | Called when an object is created with new. |
GoTo methods have no effect when called during the boot process.
The system calls the Load method only on static objects, that is, objects defined in an Objects.Def file. The system will not call Load on objects created with the New or Copy methods.
Instantiate: never Subclass: always Call its methods: sometimes
The actor demo in the Miscellany sample in the MagicDeveloper kit contains an example of a class which inherits from HasLoad. Here is the class definition:
Define Class ActorDemo; inherits from Viewable, HasLoad; field actors: Object, getter, setter; field centerKey: Object, getter, setter; field centerBox: Box; operation GetCenterBox(center : Box); Overrides Draw; operation LaunchActors(), safe, common; Overrides Load; attribute CenterKey: Object; End Class;
And here is the definition of the Load method:
Method void ActorDemo_Load(ObjectID self) { ObjectID actors; Box aBox; ActorDemo_Fields rec; actors = NewTransient(ObjectList_, NULL); SetActors(self, actors); /* Calculate the center area. */ ContentBox(self, &aBox); InsetBox(&aBox, 100 * onePixel, 50 * onePixel); ReadFields(self, &rec); rec.centerBox = aBox; WriteFields(self, &rec); /* Create the semaphore that controls access to the center area */ SetCenterKey(self, NewTransient(Semaphore_, nil)); }
Whenever you create a subclass using the HasLoad mixin, you must override the following method:
Method | Description |
---|---|
Load | Process each object of this class from the objects.Def file |
The HasLoad mixin defines no fields.
operation Load(), noMethod Call: rarely Override: always
You will probably never call the Load method yourself. The system calls it instead. See the discussion above for more information on when the method is called.