Simple lights

A simple light is a fixture that is either on or off. Lights.All() lists them, and each one is a LightInfo, a snapshot of the light at the moment you asked. The snapshot does not update by itself, so ask again for fresh values.

Lights simple light calls and event

Call All() or Get(id) to read simple lights, and SetOn(id, on) to switch one. Toggled fires after a light is switched.

public static class Lights
{
    public static List<LightInfo> All();
    public static LightInfo Get(string id);
    public static bool SetOn(string id, bool on);
    public static event Action<LightToggledArgs> Toggled;
    public static bool IsAvailable { get; }
}
Method Returns What it does
All() List<LightInfo> Every simple light in the active scene.
Get(id) LightInfo One light. null when nobody has that id.
SetOn(id, on) bool Switches the light. true when it now matches.
Property Type What it means
IsAvailable bool false when a light event could not be set up.
Event Gives you When it fires
Toggled LightToggledArgs: the light’s id and whether it is on after the change. A light was just switched on or off.

LightInfo a snapshot of one simple light

All() and Get(id) give you one of these. On is as it was when you asked, so ask again for a fresh value. (It is built from the game’s LightTurnOffOn component and its on flag.)

public sealed class LightInfo
{
    public string Id { get; }
    public bool On { get; }
}
Property Type What it means
Id string Its save id, or when it has none, a text path of where it sits in the scene.
On bool true when the light is on.

LightToggledArgs what the Toggled event tells you

Toggled gives you one of these when a light is switched. On is the state after the change.

public sealed class LightToggledArgs
{
    public string Id { get; }
    public bool On { get; }
}
Property Type What it means
Id string The light, the same as LightInfo.Id.
On bool true when the light is on after the change.

Things to know

  • Id is the light’s save id, or a text path when it has none. A path changes if the object is moved or renamed, so do not keep it for long. (The id is the GUID field, or the scene path when GUID is empty.)
  • SetOn(id, on) switches the light the way the game does. The materials, the light objects and the signals wired to the light all run as they do in the game. If the light is already in the state you asked for, nothing happens, you get true, and the signals do not fire twice. (It calls the game’s turnLightOn() or turnLightOff(). The return value is true when LightTurnOffOn.on now equals the requested value.)
  • Switching on a light that is fading out brings it back properly. The game fades some lights out over a few frames. If you ask for such a light to turn on, the framework uses the game’s fade-in instead of the plain switch, because the plain switch would leave it dark. (It calls TurnLightOnFade(false) when the private fadeOut flag is set.)
  • Toggled fires whenever a light really changes between on and off, whoever changed it. That includes your SetOn calls, the player, and the game’s own scripts. A call that changes nothing does not fire. (It is raised after turnLightOn, turnLightOff and TurnLightOffFade when on is different from before.)
  • Toggled for a light that fades out fires at once, not when the fade is over. The light is already counted as off, and its brightness then fades out over the next few frames. (TurnLightOffFade sets on = false immediately.)

Limits

  • All() and Get(id) only see the scene you are in. Lights that are switched off at the object level, or in another scene, are not listed. Whether a fixture whose script sits on a switched-off object should be listed is still undecided. (They use FindObjectsByType, which skips inactive objects.)
  • SetOn returns false for an id nobody has, and while a save is loading or being written. It also returns false for a multi-state light, because All, Get and SetOn don’t see those.
  • Loading a save does not fire Toggled, even though lights change on screen. The game restores each light’s saved state before it switches the light, so the framework sees no change. (The game’s OnLoadingGame writes on from the save and then calls turnLightOn or turnLightOff. By then on already holds the restored value.)