7. Entity Management

7.1. Creating an Entity Programmatically

In its simplest form, the Create Entity Command to creates an entity at a specific position on the scene.

Create entity

7.2. Entity Parameters

Entity Parameters have a value(number, string, boolean) that can be set or returned. Here’s a list of the different entity parameters:

  • Name – Sets or returns the entity’s name
  • Image – Sets or returns the entity’s image
  • Top – Sets or returns the topmost point of the entity on the y axis
  • Left – Sets or returns the leftmost point of the entity on the x axis
  • Layer – Sets or returns the layer that contains the entity
  • Image Width – Sets or returns the width of the entity’s image in pixels
  • Image Height – Sets or returns the height of the entity’s image in pixels
  • Image left offset – Sets or returns the entity’s image position relative to the entity’s actual center.
  • Image top offset – Sets or returns the entity’s image position relative to the entity’s actual center.
  • Rotation – Sets or returns of the entity’s rotation
  • Has Physics? – Sets or returns whether the the entity has physics enabled
  • Type – Sets the entity to a Static or Dynamic physics object
  • Shape – Sets the shape of the entity to a Circle or Square for physics
  • Collision Width Scale – Scales the entity’s collision area proportional to its image width
  • Collision Height Scale – Scales the entity’s collision area proportional to its image height
  • Friction – Sets the entity’s friction for physics
  • Density – Sets the entity’s density for physics
  • Restitution – Sets the amount that an entity bounces during a physics collision
  • Fixed Rotation? – Sets or returns whether the the entity’s rotation is constant
  • Speed – Sets the speed of the entity
  • Speed Direction – Sets the direction that the speed will be applied in
  • Angular Speed – Applies torque to the entity

7.3. Getting This Entity’s Parameters

You can return the value of an entity parameter using the This Entity Command.

This entity

The returned value from the entity parameter can be stored in a variable, used in a mathematical operation, etc.

Get entity parameter

7.4. Setting This Entity’s Parameters

The values in entity parameters can be set or changed while the game is running. You use the same This Entity Command, you just set a value instead of returning it. Parameters can be set the same way as variables however, there are some restrictions.

  • Name, Image, Type, and Shape can only be set to a string value
  • Top, Left, Layer, Image Width, Image Height, Image Left Offset, Image Top Offset, Rotation, Collision Width Scale, Collision Height Scale, Friction, Density, Restitution, Speed, Speed Direction, and Angular Speed can only be set to a number
  • Has Physics? and Fixed Rotation can only be set to boolean, 1(true) or 0(false)

Here are some ways you could set an entity parameter:

Set entity parameter

7.5. Storing an Entity in a Variable

The Create Entity Command returns a random unique Name(in the form of a String) of the entity it creates. This Name can be stored in a variable and later be used to change the created entity’s parameters.

Entity name

7.6. Getting an Entity’s Parameter using its Name

Just like you can return the value in an entity parameter using the This Entity Command, you can return the value of any entity’s parameter using the Entity Property Command.

Get entity parameter b

You just need to type in the Name of the desired entity or add a variable that contains the name of the entity.

Get entity parameter with name

With the Name of the entity, you can return the value of any parameter.

7.7. Setting an Entity’s Parameter Using its Name

With an entity’s Name you can set the value of it’s parameters. You can even change it’s Name.

Set entity parameter with name

7.8. Testing for a Specific Actor

You may want different things to happen when an entity collides with entities of different actors.

The program below is triggered at the start of every collision. The If Statement uses the Collided Actor Command to check if the entity is colliding with any entities of the Danger Actor.

Collided actor

7.9. Destroying an Entity

When an entity is no longer needed, it can be destroyed. You use the Destroy Entity Command and specify the entity that needs to be destroyed. You can use a variable, the This Entity Command, or type the name of the entity.

Destroy entity