Making an interactive light
A simple light switch combination has two distinct elements: The Light and the Switch.
There are various ways of creating this, so this is just one way of doing it.
In this example, you will make a red and green changing light.
1 - Make the light actor
You can think at the light as an actor having multiple states. In this case, just two: red
and green
.
The simplest way to achieve this, is to make it a child blueprint of Blueprints\Core\BP_PK_StatefulActor
.
Feel free to design the light actor as you like. In this example, it has a PointLightComponent
named PointLight and a static mesh. This will be the one changed by the button.
Because it is a child blueprint class of BP_PK_StatefulActor
, it will already implement the BPI_PK_ObserverActor interface and will have a BP_PK_IterativeStateComponent attached.
Being a stateful actor, will let you add it to the Send Actions To array of other buttons (more on this later) and, most important, will give you access to the following custom events:
Now, in the Event Graph of your newly created light actor, implement the PK_HandleOwnStateChanged
event, and add the state change logic in there.
In this example, the value passed in the New State Id variable is used. The light will go red on state with index 0
, and green on state with index 1
.
Note: Make sure you implement the
PK_HandleOwnStateChanged
, and not thePK_HandleStateChanged
You are not limited to the New State Id value, you can use any other passed in parameter. By expanding the Args structure, you have access to the new state name, sender, etc.
2 - Make the button
The button is a simple two state button. As shown in the Making buttons section.
3 - Putting it all together.
Now you have a light, with two states, and a button with two states.
Go in the ObserveReactComponent of the button and:
- add the button to the Listen To array. (or click
Listen to Owner
from the default section) - set On State Changed to
Passthrough
- check Enable Reactions
- Add a default rule to the Rules array
- Add the light actor tot the Send Actions To array
The settings should look like this:
Now you should have a functioning light switch.
4 - What to do next
Experiment with the following scenarios:
- A button with two states, but a light with 4 states (each, a different color)
- A button with only one state (make sure the
Fire State Change on Same State
is checked in its IterativeStateComponent.) - A button with for states, and a light with 2 states.
- A button with one state, and multiple lights with multiple states.
- Add an ObserveReactComponent to the light blueprint, and combine buttons and lights together.
Here is an example:
Have fun!