Creating a simple switchable light

Learn how to create a simple switchable light source in Amazon Lumberyard, that can be switched on and off via gameplay events.

There are a few other ways to build this, such as communicating directly with the light request bus from Lua, bypassing the need for a Simple State component. However, the solution shown here is much more generic. It can be extended to any type of switch action, such as opening a door, or pulling a lever, and it does not require bespoke bus interaction.

Create the entities

First of all, create 2 new component entities.

Name the first entity ‘SwitchableLight’. We will use this as the control entity for our light for deciding whether it is on or off. Think of it as the switch.

Name the second entity ‘Light’. This will be our actual spotlight.

When you have the two entities, parent the ‘Light’ to the ‘SwitchableLight’ like so:

Now, you need to adjust the transform on the ‘Light’ so that it has no offset, and is rotated to point downwards (spotlights in Lumberyard currently treat X as forward, rather than Y):

Let’s make the light actually look like a light. On the ‘Light’ entity, add a ‘Projector Light’ component.

Tip: You can also use other types of light components here, or try adding a mesh if you like.

Configure the projector light to your liking. For example:

Position and rotate the ‘SwitchableLight’ to your liking, and add some geometry to the scene if you want to see how it interacts with objects.

Try adding a lens flare component too:

Add state

That’s the light done, but now we need a way of turning it on and off at runtime. For this, we need to add a ‘Simple State’ and a ‘Lua Script’ component to the ‘SwitchableLight’ entity, like so:

The Simple State component is a really easy way of switching child entities on and off at runtime. It can have any number of states, with arbitrary names, and each state references 0 or more child entities. When the state is changed, all the child entities that are part of that state get activated, and all the child entities that aren’t part of that state get deactivated.

For the light, we want 2 states: ‘On’, and ‘Off’. When it is in the ‘On’ state, we want our ‘Light’ entity to be enabled, and when it is in the ‘Off’ state, we want the ‘Light’ entity to be disabled. Configure the Simple State component like so:

Tip: You can add any number of child entities to the states in a Simple State component. For example, you could add multiple spotlights, or you could even add an entity with a mesh that will appear when the state is activated.

Handle events

Next up, we need a way for the game to communicate with our spotlight, to be able to tell it to switch on and off.

Create a new Lua script called ‘switchable_light.lua’, and enter the following code:

local switchable_light =
    Properties = 
function switchable_light:OnActivate()
    self.gameplayBusHandler = GameplayNotificationBus.Connect(self, GameplayNotificationId(EntityId(), "ToggleLight"))
function switchable_light:OnDeactivate()
    self.gameplayBusHandler = nil
function switchable_light:OnEventBegin(gameplayNotificationId, value)
return switchable_light

This code just connects to the GameplayNotificationBus, listens for an event called ‘ToggleLight’, and then sends a message to the Simple State component to cycle to its next state.

Add the Lua script to the ‘SwitchableLight’ entity’s ‘Lua Script’ component, like so:

That’s it! You now have a spotlight that starts switched off, and toggles on and off every time it receives a gameplay event named ‘ToggleLight’.

To test it out, try creating a flow graph that simply fires that gameplay event each time you press a key:

Tip: You don’t have to use Flow to send this event. You could equally use C++, or even another Lua script.

Leave a Reply