LampSwitch.gsl -- Basic script to enter an input area and use a custom action key to turn off and on a particlar light in the world. The light is added directly to the lamp object and the calls are based on it being a child of that. The action key "lights" is a custom action I created and assigned myself in my inputs.cfg. If you have trouble or need the lines to get that functioning, please let me know but you can easily change "if (params.stringValue == "Lights")" to "== "Action" or "== PickUp". This also contains a delay snippet from Zknack which I've been basically permanently using.
I set custom values for the lights themselves, but you can easily expand this to allow for controller variables such that multiple lights with multiple values can be used with the same basic script.
Please note that it's a great idea to not turn off dynamic lights all the way either to 0 intensity or by way of deactivation. The reason is that when it comes back on, a lot is recalculated. If you set the intensity to 0.01, the calculations are set and left alone as there is no "reactivate" happening on the light source. Basically, this way there is 0 noticable hickup or lag when activating the lights...even multiple lights attached to the same object.
bool DEBUG_ENABLED = true; //set to false to disable console feedback
bool LightsPressed = false; //on/off variable
//Thank you to zknack for the inputs delay code.
float inputTimer = 0.5f; //adjusts time in seconds between inputs
float lastInput = 0; //used to capture the time in seconds from the game
bool inputEnabled = true; //on_off variable for inputs, read in HandleEvent, written in DynamicsAdvance
void DynamicsAdvance( Object@ object, float seconds)
{
//zknack's input Delay code.
if(!inputEnabled)
{
lastInput+=seconds;
if(lastInput >= inputTimer)
{
inputEnabled = true;
lastInput = 0.0f;
if(DEBUG_ENABLED)
Print("Input Timer Stopped");
}
}
}
bool HandleEvent( Object@ object, const String& in event, GameEventParams@ params)
{
GameManager@ game = GetGameManager();
// Only Events for Player.
if (params.player == null)
return false;
// Input event
if (event == "Input")
{
if (params.stringValue == "Lights")
{
if(params.floatValue > 0 && inputEnabled)
{
inputEnabled = false; //if read, shut off
if(DEBUG_ENABLED)
Print("Input Timer Started");
if (LightsPressed == false)
{
game.GetWorld().GetObject("lamp").GetLight("Light1").SetIntensity(0.1);
game.GetWorld().GetObject("lamp").GetLight("Light2").SetIntensity(0.7);
game.GetWorld().GetObject("lamp").GetLight("Light3").SetIntensity(0.86);
GetGameManager().GetWorld().SetAmbientColour( 130,130,0);
LightsPressed = true;
return true;
}
game.GetWorld().GetObject("lamp").GetLight("Light1").SetIntensity(0.01); //don't use 0 or lag on switch
game.GetWorld().GetObject("lamp").GetLight("Light2").SetIntensity(0.01); //" "
game.GetWorld().GetObject("lamp").GetLight("Light3").SetIntensity(0.01); //" "
GetGameManager().GetWorld().SetAmbientColour(15,15,0); //whole numbers only 0-255 0,1,2...
LightsPressed = false;
return true;
}
}
}
return false;
}