I've developed the flashlight into a script that turns objects into lightswitches. It's a bit lazy at the moment, but a good start for anybody who's still getting aquainted with scripting. I have plans to make it more clean, such as putting the actual light values into controller variables instead of right in the code so that one script can do multiple lights. However, you'll have to point the switch to a current light source, so you might have to have it figure out which lightsource is closest if you want something more dynamic, such as switches that work on the closest light or something. For now, you have to make a new script for a new light.
This also causes conflicts with using multiple lights at one time. Since each of them sets the ambient to 0,0,0, if the other light is on, it no longer looks correct. I want to figure out how to increase the ambient value by a variable instead of manually inserted values and have it decrease by that amount when off so that it adds to the overall lighting. I gotta get the syntax for that first cause of the commas. This is, of course, if you want to boost the ambient when a particular light goes on or off, otherwise comment that part out. I needed it and it was a great relief to see how easy it was to control.
You can tie this script in to any object that you want to act as the actual switch for the light. It is in that object that the input area is laid. The code itself will need to point to the name of your object and your light, but that means you can have any object control any other. Again, i don't know how to dynamically get it to realize which object to work on, but I don't find it necessary at the moment.
Please help me enhance it any way you can think of or make it more efficient.
config.cfg
Action Lights
KeyMapping KeyL Lights
Object's ORP file. Substitute your own object or replace with a box to test.
OPRP
LoadObject Lamp.obj
AddCollision Lamp
CollisionType UseRenderObject
ControllerType Scripted
ControllerInputCenter 0.0000 3.0000 0.0000
ControllerInputBoxSize 4.0000 6.0000 4.0000
ControllerSetting ScriptFile LampSwitch.gsl
GlobalLight 1
AddLight Light1
LightType Area
LightColour 255 255 26
LightPosition 0.0000 6.0000 0.0000
LightIntensity 0.01 // don't use 0 or lag on switch
LightFalloff InvDistance 40.0000
LightArea 8 8
LightSpecular 0.1000
LightEnabled 1
AddLight Light2
LightType Directional
LightColour 255 255 200
LightIntensity 0.01 // don't use 0 or lag on switch
LightSpecular 0.3300
LightRotation 0.0000 -90.0000 0.0000
LightPosition 0.0000 7.0000 0.0000
LightShadowType None
LightEnabled 1
AddLight Light3
LightType Directional
LightColour 255 255 200
LightIntensity 0.01 // don't use 0 or lag on switch
LightSpecular 0.4000
LightRotation 0.0000 90.0000 0.0000
LightPosition 0.0000 10.0000 0.0000
LightShadowType None
LightEnabled 1
CastShadow 0
VisibleInReflections 0
EnableLightMap 0
CastLightMapShadow 0
LampSwitch.gsl
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; //input time variable
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(0,0,0); //whole numbers only 0-255 0,1,2...
LightsPressed = false;
return true;
}
}
}
return false;
}