Solarus quests  1.6
Quest maker's reference
Switch

A switch is a button that can be activated to trigger a mechanism.

This type of map entity can be declared in the map data file. It can also be created dynamically with map:create_switch().

Overview

A switch may be activated by the hero, by a block or by a projectile, depending on its subtype. The following subtypes of switches are available:

  • Walkable, traversable pressure plate.
  • Button to be activated by shooting an arrow on it with the bow.
  • Solid switch to be activated with the sword or other weapons.

When a switch is activated, the event switch:on_activated() is called. Define that event to implement what happens: opening a door, showing a chest, etc.

Some switches get inactivated when the hero (or the entity that activated them) leaves them. In this case, when the switch is inactivated, the event switch:on_inactivated() is called.

The size of a switch is always 16x16 pixels. Its sprite can be defined at creation time, as well as the sound played when it gets activated.

Methods inherited from map entity

Switches are particular map entities. Therefore, they inherit all methods from the type map entity.

See Methods of all entity types to know these methods.

Methods of the type switch

The following methods are specific to switches.

switch:is_walkable()

Returns whether this is a walkable switch.

  • Return value (boolean): true if this switch is a walkable one.

switch:is_activated()

Returns whether this switch is activated.

  • Return value (boolean): true if this switch is currently activated.

switch:set_activated([activated])

Sets whether this switch is activated or not.

The change is quiet and immediate: no sound is played and no event is triggered.

  • activated (boolean, optional): true to make the switch activated, false to make is inactivated. No value means true.

switch:is_locked()

Returns whether this switch is current locked.

When a switch is locked, its state cannot change anymore: it can no longer be activated or inactivated by other entities. However, it can still changed programmatically by calling switch:set_activated().

  • Return value (boolean): true if this switch is currently activated.

switch:set_locked([locked])

Locks this switch in its current state or unlocks it.

When a switch is locked, its state cannot change anymore: it can no longer be activated or inactivated by other entities. However, it can still changed programmatically by calling switch:set_activated().

  • locked (boolean, optional): true to lock the switch, false to unlock it. No value means true.
Remarks
The method switch:set_activated() works even on a locked switch.

Events inherited from map entity

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

Switches are particular map entities. Therefore, they inherit all events from the type map entity.

See Events of all entity types to know these events.

Events of the type switch

The following events are specific to switches.

switch:on_activated()

Called when this switch has just been turned on.

This is the right place to define the action that you want your switch to perform.

switch:on_inactivated()

Called when a switch has just been turned off.

switch:on_left()

Called when an entity placed on a switch (like the hero or a block) has just left the switch, regardless of whether the switch was activated or not.