CustomInitialize is called once per entity instance when it is initialized. Initialization happens in any of the following situations:
When an instance is created either in code by using the "new" operator, or if an instance is added through the FRB Editor. See below for more information.
When an instance is created through tiles in a Tiled map (tmx)
When an Entity is recycled through a factory
By default CustomInitialize is an empty function.
Any code can be added to CustomInitialize to prepare an entity for use. Simple entities may not require any code in CustomInitialize, but code can be added for the following:
Preparing animations (using AnimationController )
Dynamically creating FRB objects such as populating a List of Sprites
Adding conditional debug code such as turning on the visibility of collision shapes
CustomInitialize should not include any code that depends on the current screen. For example, if an Enemy entity needs to initialize its logic according to the current PlayerList in GameScreen, CustomInitialize should not access GameScreen. Instead, initialization which requires external context (such as a list in a Screen) should do so in a function which is called by the Screen. This keeps your entity portable, and reduces the chances of errors occurring due to improper casting or unexpected access of lists before they are available.
Using the example of initializing enemy logic, an Enemy entity may have the following code for initialization:
In this case, InitializeAi would be called by the GameScreen, as shown in the following example code:
The CustomInitialize method will only be called when an Entity instance is added to managers.
The most common method of creating entities is to use a factory. For example, creating an Enemy entity with the EnemyFactory will result in the newly-created instance being added to managers and having CustomInitialize called:
The additon of managers can be controlled by calling an Entity's constructor. Of course, doing so may result in the entity being created but not being added to the proper lists. If you are manually creating entities, make sure that you add them to the appropriate lists (such as the GameScreen lists).
Entities and Screens which have base types (inheritance) have two or more CustomInitialize functions depending on the inheritance depth. For example, if an entity Skeleton inherits from Enemy, each class (Skeleton and Enemy) has its own CustomInitialize method. Both are called by generated code - an explicit base.CustomInitialize();
call is not needed. CustomInitialize is called on the base class first, then to more-derived. Using the example above, Enemy.CustomInitialize is called first, then Skeleton.CustomInitialize.