Solarus quests  1.2
Quest maker's reference
Map entities

Objects placed on the map are called map entities (or just entities).

There exists many types of entities. They can be either declared in the map data file, or created dynamically by using the map:create_* methods of the map API.

Overview

All entities have a position on the map (X, Y and layer) and a size. Depending on their type, they can be visible or not. When they are visible, they are usually represented by one or several sprites. Some entities are fixed, other move according to a movement object.

Entities can also have a name that uniquely identifies them on the map. This is useful to access them from the map API. The name is optional, but if an entity has a name, it must be unique on the map.

Here are the existing types of entities.

Remarks
Note that sprites and movements are not map entities, but they can be attached to map entities (to display them and to move them, respectively). Sprites and movements can also be used outside a map, for example in your title screen or in other menus.

Methods of all entity types

These methods exist in all entity types.

entity:get_type()

Returns the type of entity.

  • Return value (string): The type of this entity. Can be one of: "hero", "dynamic_tile", "teletransporter", "destination", "pickable", "destructible", "carried_object", "chest", "shop_treasure", "enemy", "npc", "block", "jumper", "switch", "sensor", "separator", "wall", "crystal", "crystal_block", "stream", "door", "stairs", "bomb", "explosion", "fire", "arrow", "hookshot", "boomerang" or "custom_entity",
Remarks
The type "tile" is not is this list because tiles don't exist at runtime for optimization reasons.

entity:get_map()

Returns the map this entity belongs to.

  • Return value (map): The map that contains this entity.

entity:get_game()

Returns the game that is running the map this entity belongs to.

  • Return value (game): The current game.

entity:get_name()

Returns the name of this map entity.

The name uniquely identifies the entity on the map.

  • Return value (string): The name of this entity, or nil if the entity has no name (because the name is optional).

entity:exists()

Returns whether this entity still exists on the map.

An entity gets destroyed when you call entity:remove() or when the engine removes it (for example an enemy that gets killed or a pickable treasure that gets picked up). If you refer from Lua to an entity that no longer exists in the C++ side, this method returns false.

  • Return value (boolean): true if the entity exists, false if it was destroyed.

entity:remove()

Removes this entity from the map and destroys it.

After the entity is destroyed, entity:exists() returns false and there is no reason to keep a reference to it in the Lua side (though it is harmless).

entity:is_enabled()

Returns whether this entity is enabled.

When an entity is disabled, it is not displayed on the map, it does not move and does not detect collisions. But it still exists, it still has a position and it can be enabled again later.

  • Return value (boolean): true if this entity is enabled.

entity:set_enabled([enabled])

Enables or disables this entity.

When an entity is disabled, it is not displayed on the map, it does not move and does not detect collisions. Its movement, its sprites and its timers if any are suspended and will be resumed when the entity gets enabled again. While the entity is disabled, it still exists, it still has a position and it can be enabled again later.

  • enabled (boolean, optional): true to enable the entity, false to disable it. No value means true.

entity:get_size()

Returns the size of the bounding box of this entity.

The bounding box determines the position of the entity on the map. It is a rectangle whose width and height are multiples of 8 pixels. The bounding box is used to detect whether the entity overlaps obstacles or other entities.

  • Return value 1 (number): Width of the entity in pixels.
  • Return value 2 (number): Height of the entity in pixels.
Remarks
Note that the sprites of an entity may have a different size than the entity itself.

entity:get_origin()

Returns the origin point of this entity, relative to the upper left corner of its bounding box.

When an entity is located at some coordinates on the map, the origin points determines what exact point of the entity's bounding box is at those coordinates. It is not necessarily the upper left corner of the entity's bounding box.

The origin point is usually be the central point of contact between the entity and the ground. For example, the origin point of the hero, enemies, non-playing characters and most entities is the center of their shadow. Thus, for entities of size (16,16), the origin point is often (8,13).

This origin point property allows entities of different sizes to have comparable reference points that can be used by the engine. Indeed, when two enemies overlap, the engine needs to determine which one has to be displayed first (it is always the one with the lowest Y coordinate). Sometimes, the engine also needs to compute an angle between two entities, for example to push away an enemy that was just hit. Using the upper left corner of their bounding box would not give the correct angle (unless both entities had the same size).

The origin point is also the point of synchronization of an entity with its sprites (because again, an entity that has a given size may have sprites with different sizes).

  • Return value 1 (number): X coordinate of the origin point in pixels, relative to the upper left corner of the entity's bounding box.
  • Return value 2 (number): Y coordinate of the origin point in pixels, relative to the upper left corner of the entity's bounding box.

entity:get_position()

Returns the position of this entity on the map (coordinates and layer).

  • Return value 1 (number): X coordinate of the origin point of the entity, relative to the upper left corner of the map.
  • Return value 2 (number): Y coordinate of the origin point of the entity, relative to the upper left corner of the map.
  • Return value 3 (number): Layer where the entity is on the map (0: low layer, 1: intermediate layer, 2: high layer).

entity:set_position(x, y, [layer]):

Changes instantly the position of this entity on the map (coordinates and layer). The origin point of the entity gets placed at these coordinates, relative to the map's upper left corner. Any previous movement or other action performed by the entity continues normally.

  • x (number): X coordinate to set.
  • y (number): Y coordinate to set.
  • layer (number, optional): Layer to set (0: low layer, 1: intermediate layer, 2: high layer). By default, the layer is unchanged.
Remarks
Be careful: this function does not check collisions with obstacles.

entity:get_center_position()

Returns the coordinates of the center point of this entity on the map.

  • Return value 1 (number): X coordinate of the center of this entity's bounding box, relative to the upper left corner of the map.
  • Return value 2 (number): Y coordinate of the center of this entity's bounding box, relative to the upper left corner of the map.

entity:get_bounding_box()

Returns the rectangle representing the coordinates and size of this entity on the map.

The bounding box determines the position of the entity on the map. It is a rectangle whose width and height are multiples of 8 pixels. The bounding box is used to detect whether the entity overlaps obstacles or other entities.

  • Return value 1 (number): X coordinate of the upper left corner of the bounding box.
  • Return value 2 (number): Y coordinate of the upper left corner of the bounding box.
  • Return value 3 (number): Width of the bounding box.
  • Return value 4 (number): Height of the bounding box.
Remarks
The sprites of this entity (if any) may exceed the bounding box.

entity:overlaps(other_entity)

Returns whether the bounding box of this entity overlaps the specified rectangle or point.

  • x (number): X coordinate of the upper left corner of the rectangle to check.
  • y (number): Y coordinate of the upper left corner of the rectangle to check.
  • width (number, optional): Width of the rectangle (default 1).
  • height (number, optional): Height of the rectangle (default 1).
  • Return value (boolean): true if the bounding box of this entity overlaps the rectangle.

entity:overlaps(other_entity)

Returns whether the bounding box of this entity overlaps the bounding box of another one.

  • other_entity (entity): An entity to check.
  • Return value (boolean): true if both bounding boxes overlap.

entity:get_distance(x, y), entity:get_distance(other_entity)

Returns the distance in pixels between this map entity and a point or another map entity.

To compute the distance to a specified point:

  • x (number): X coordinate of the point.
  • y (number): Y coordinate of the point.
  • Return value (number): The distance in pixels between the origin point of this entity and the point.

To compute the distance to another map entity:

  • other_entity (entity): The entity to compute the distance to.
  • Return value (number): The distance in pixels between the origin point of this entity and the origin point of the other entity.

entity:get_angle(x, y), entity:get_angle(other_entity)

Returns the angle between the X axis and the vector that joins this entity to a point.

To compute the angle to a specified point:

  • x (number): X coordinate of the point.
  • y (number): Y coordinate of the point.
  • Return value (number): The angle in radians between the origin point of this entity and the specified point. The angle is between 0 and 2 * math.pi.

To compute the angle to another map entity:

  • other_entity (entity): The entity to compute the angle to.
  • Return value (number): The angle in radians between the origin point of this entity and the origin point of the other entity. The angle is between 0 and 2 * math.pi.

entity:get_direction4_to(x, y), entity:get_direction4_to(other_entity)

Like entity:get_angle(), but instead of an angle in radians, returns the closest direction among the 4 main directions.

This is a utility function that essentially rounds the result of entity:get_angle().

To compute the direction to a specified point:

  • x (number): X coordinate of the point.
  • y (number): Y coordinate of the point.
  • Return value (number): The direction this entity should take to look at this point, between 0 (East) and 3 (South).

To compute the direction to another map entity:

  • other_entity (entity): An entity to target.
  • Return value (number): The direction this entity should take to look at the other entity, between 0 (East) and 3 (South).

entity:get_direction8_to(x, y), entity:get_direction8_to(other_entity)

Like entity:get_angle(), but instead of an angle in radians, returns the closest direction among the 8 main directions.

This is a utility function that essentially rounds the result of entity:get_angle().

To compute the direction to a specified point:

  • x (number): X coordinate of the point.
  • y (number): Y coordinate of the point.
  • Return value (number): The direction this entity should take to look at this point, between 0 (East) and 7 (South-East).

To compute the direction to another map entity:

  • other_entity (entity): An entity to target.
  • Return value (number): The direction this entity should take to look at the other entity, between 0 (East) and 7 (South-East).

entity:snap_to_grid()

Makes sure this entity's upper left corner is aligned with the 8*8 grid of the map.

Remarks
Be careful: this function does not check collisions with obstacles.

entity:bring_to_front()

Places this entity in front of all other entities on the same layer.

Since entities that are on the same layer can overlap, you can use this function to change their Z-index.

Remarks
Some entities like NPCs, enemies, the hero and some custom entities have the property to be displayed in Y order. The Z-index of these entities is already managed by the engine to show entities more to the north behind the ones more to the south. Therefore, this function has no effect for them.

entity:bring_to_back()

Places this entity behind all other entities on the same layer.

Since entities that are on the same layer can overlap, you can use this function to change their Z-index.

Remarks
Some entities like NPCs, enemies and the hero have the property to be displayed in Y order. The Z-index of these entities is already managed by the engine to show entities more to the north behind the ones more to the south. Therefore, this function has no effect for them.

entity:get_optimization_distance()

Returns the optimization threshold of this map entity.

Above this distance from the visible area of the map, the entity will be suspended. A value of 0 means an infinite distance (the entity is never optimized away).

  • Return value (number): The optimization distance in pixels.

entity:set_optimization_distance(optimization_distance)

Sets the optimization threshold of this map entity.

Above this distance from the visible area of the map, the entity will be suspended. When an entity is suspended, its movements, sprites and timers are suspended.

A value of 0 means an infinite distance (the entity is never optimized away). The default value depends on the type of entity and is usually fine, but you may need to increase it in some cases, or even disable it for example for an enemy in a huge room.

  • optimization_distance (number): The optimization distance to set in pixels. 0 disables this automatic suspending.

entity:is_in_same_region(other_entity)

Returns whether another entity is in the same region than this one.

Regions of the map are defined by the position of separators. You can use this function to make sure that an enemy close to the hero but in the other side of a separator won't attack him.

  • other_entity (entity): Another entity.
  • Return value (boolean): true if both entities are in the same region.

entity:test_obstacles(dx, dy, [layer])

Returns whether there would be a collision with obstacles if this map entity was translated.

  • dx (number): X component of the translation in pixels (0 means the current X position).
  • dx (number): Y component of the translation in pixels (0 means the current Y position).
  • layer (number, optional): Layer to test. No value means the current layer.
  • Return value (boolean): true if there would be a collision in this position.

entity:is_visible()

Returns whether this entity is visible.

When the entity is hidden, its sprites (if any) are not displayed, but everything else continues normally, including collisions.

  • Return value (boolean): true if the entity is visible.

entity:set_visible([visible])

Hides or shows the entity.

When the entity is hidden, its sprites (if any) are not displayed, but everything else continues normally, including collisions.

  • visible (boolean, optional): true to show the entity, false to hide it. No value means true.

entity:get_movement()

Returns the current movement of this map entity.

  • Return value (movement): The current movement, or nil if the entity is not moving.

entity:stop_movement()

Stops the current movement of this map entity if any.

Events of all entity types

Events are callback methods automatically called by the engine if you define them.

entity:on_created()

Called when this entity has just been created on the map.

entity:on_removed()

Called when this entity is about to be removed from the map (and therefore destroyed).

entity:on_position_changed(x, y, layer)

Called when the coordinates of this entity have just changed.

  • x (number): The new X coordinate of the entity.
  • y (number): The new Y coordinate of the entity.
  • layer (number): The new layer of the entity.

entity:on_obstacle_reached(movement)

Called when the movement of this entity was stopped because of an obstacle.

When an obstacle is reached, this event is called instead of entity:on_position_changed().

  • movement (movement): The movement of the entity.

entity:on_movement_changed(movement)

Called when some characteristics of this entity's movement (like the speed or the angle) have just changed.

  • movement (movement): The movement of the entity.

entity:on_movement_finished()

Called when the movement of the entity is finished (if there is an end).