CreateNew
Last updated
Was this helpful?
Last updated
Was this helpful?
The CreateNew function is used to create a new instance of the Entity corresponding to the calling factory. In simple cases, the only requirement is simply calling this method an a new instance will be created and available for use in your code. For information on CreateNew in the context of a tutorial, see .
This code assumes that your project has an Entity called "Enemy" and that it has a corresponding Factory:
Usually entities which are created with factories are created on some condition. Examples include:
Reacting to input. For example, the player presses a button to shoot a bullet.
Reacting to a collision event. For example, the player collides with a trigger to spawn an item.
Reacting to time condition. For example, an enemy spawner creates an enemy once every 10 seconds.
The following code shows how to create a bullet whenever the user presses the space bar.
The CreateNew method also takes a Layer argument. This layer specifies which Layer the created object should be a part of. Entities are usually created in Screen custom code or in the custom code of other Entities.
If you are creating an Entity through a Factory in a Screen, then you can pass the Layer which you would like the newly-created Entity to be a part of in the CreateNew method. This will usually be a Layer which is a part of the Screen (usually created through Glue). The following code assumes a Layer called HudLayer:
If this is not the case, then the creating Entity must have reference to the Layer which is to be used. You may need to set this reference up purely in custom code.
If the Entity that is being instantiated through the Factory is to be on the same Layer as the Entity calling the code, then the can be used:
If you are calling CreateNew you may see an exception indicating that you must call Initialize before using the factory. . If you are seeing this exception it means that the factory that you are trying to use when calling CreateNew has not yet been initialized. If you've used factories before then you may be thinking "I never had to call Initialize before, why do I have to now?" Initialize will automatically be called for you if you are using a Screen which contains a list of the given Entity type, and if that list was created through Glue. In other words, if you have an Entity called Bullet, and a factory called BulletFactory, then BulletFactory will automatically be initialized if your game is currently in a Screen which has a list of Bullets added to it in Glue. If you do not have a list of the given object type, but you want to instantiate that object at runtime, then you will manually need to call Initialize on the factory. For more information about Initialize, see .