T3D Mounted Flashlight Resource
by CSMP · 03/08/2011 (10:43 pm) · 26 comments
Updated: Lens/Lightflare and mountOffset added to Step 3.
Credit goes to deepscratch for helping me figure out how to add "cookies" into a spotlight and in the process fix some of the problems I was having with the flashlight code.

Created and Tested with T3D 1.1 Beta3
Fairly straightforward installation, Most of these were added at the bottom of the corresponding files unless noted otherwise.
1. In your 'scripts/client/default.bind.cs' file, Add:
2. In your 'scripts/server/commands.cs' file, Add:
3. In your 'scripts/server/players.cs' file, Add:
4. Delete your 'scripts/client/config.cs' file, or Add:
5. Add light.png to your 'art/gui/' folder.
This is your texture overlay or "cookie" you can set up your own custom texture or use the one provided, thats up to you.(just make sure you have changed Step3 to point toward your texture)
6. Go ahead and start your game and you should have a flashlight you can toggle using the "f" key, Have fun and I hope you guys found this helpful!
Note: This has been "network" tested with dual instances of T3D and should work, please let me know if there are any errors as it should work out of the box.
Here is an example SpotLight datablock, you can transfer some of its values over to your SpotLight spawning for more customization:
The Light.png is free to use in indie or commercial projects, I only ask for credit.
Credit goes to deepscratch for helping me figure out how to add "cookies" into a spotlight and in the process fix some of the problems I was having with the flashlight code.

Created and Tested with T3D 1.1 Beta3
Fairly straightforward installation, Most of these were added at the bottom of the corresponding files unless noted otherwise.
1. In your 'scripts/client/default.bind.cs' file, Add:
$FlashlightVar = "1";
function toggleFlashlight(%val)
{
if (%val)
{
if($FlashlightVar)
{
$FlashlightVar = "0";
commandToServer('EnableFlashlight');
}
else
{
$FlashlightVar = "1";
commandToServer('DisableFlashlight');
}
}
}
moveMap.bind( keyboard, f, toggleFlashlight );What we have done here is setup the default.bind to toggle between sending an Enable or Disable command to the server side depending on what the $FlashlightVar is, by default the flashlight has just been disabled so the first toggle will always be to Enable.2. In your 'scripts/server/commands.cs' file, Add:
//------------------------------------------------------------------------------
// Flashlight Controls
//------------------------------------------------------------------------------
function serverCmdEnableFlashlight(%client)
{
%player = %client.player;
%player.FlashlightEnable();
}
function serverCmdDisableFlashlight(%client)
{
%player = %client.player;
%player.FlashlightDisable();
}These are the server commands that we just told are default.bind to call, this is how we get our clientID and send the FlashlightEnable/Disable command to the specific player that called it.3. In your 'scripts/server/players.cs' file, Add:
//----------------------------------------------------------------------------
// Enable/Disable Player Flashlight
//----------------------------------------------------------------------------
function Player::FlashlightEnable(%this)
{
// spotlight
%light = new SpotLight() // In here you can add the example datablock values for more control
{
range = "40";
cookie = "art/gui/light.png"; // Basic Lighting will not render this.
// Lens Flare will need a good mount and will need the mountOffset of the Flashlight
// to be modified for your specific application.
flareType = "LightFlareExample1";
};
%this.mountobject(%light, 0, "0.05 0.68 -0.09"); // mountobject(%obj, mountNode, mountOffset)
%this.light = %light;
}
function Player::FlashlightDisable(%this)
{
// delete lights
if(isObject(%this.light))
%this.light.delete();
}This is where we actually decide what happens to the specifc player, in this case we are going to create/destroy a SpotLight mounted to the Mount0 node on your player model.4. Delete your 'scripts/client/config.cs' file, or Add:
moveMap.bind(keyboard, "f", toggleFlashlight);This is personal preferance, I would suggest deleting the config.cs file and letting your updated default.bind.cs file rebuild your config.cs.
5. Add light.png to your 'art/gui/' folder.
This is your texture overlay or "cookie" you can set up your own custom texture or use the one provided, thats up to you.(just make sure you have changed Step3 to point toward your texture)
6. Go ahead and start your game and you should have a flashlight you can toggle using the "f" key, Have fun and I hope you guys found this helpful!
Note: This has been "network" tested with dual instances of T3D and should work, please let me know if there are any errors as it should work out of the box.
Here is an example SpotLight datablock, you can transfer some of its values over to your SpotLight spawning for more customization:
new SpotLight(slTest) { //This is not needed at all, purely for informational use
range = "32";
innerAngle = "40";
outerAngle = "65";
isEnabled = "1";
color = "1 1 1 1";
brightness = "1";
castShadows = "0";
priority = "1";
animate = "1";
animationPeriod = "1";
animationPhase = "1";
flareScale = "1";
attenuationRatio = "0 1 1";
shadowType = "Spot";
texSize = "512";
overDarkFactor = "2000 1000 500 100";
shadowDistance = "400";
shadowSoftness = "0.15";
numSplits = "1";
logWeight = "0.91";
fadeStartDistance = "13";
lastSplitTerrainOnly = "0";
representedInLightmap = "0";
shadowDarkenColor = "0 0 0 -1";
includeLightmappedGeometryInShadow = "0";
position = "0 0 0";
rotation = "0 0 0 0";
mountPID = "";
mountNode = "2";
mountPos = "0 0 0";
mountRot = "0 0 0 0";
canSave = "1";
canSaveDynamicFields = "1";
};The Light.png is free to use in indie or commercial projects, I only ask for credit.
About the author
#2
03/09/2011 (12:57 pm)
This looks wickid,I was looking for something along these lines for a character holding a torch :)
#3
03/09/2011 (12:59 pm)
that's awesome, nice resource.
#4
03/09/2011 (6:16 pm)
Thanks CSMP & DeepScratch! Very nice resource...will use this a lot I think.
#5
03/10/2011 (4:36 am)
Nice resource! Thanks guys!
#7
03/10/2011 (9:04 am)
video!
#8
@all: Glad I could help!
@deepscratch: You helped more then you think, I appreciate it too.
@anthony: I've uploaded a (very)short video showing the basics of the Flashlight Resource..
03/10/2011 (8:58 pm)
Just tested with two instances of T3D, seemed to work fine networked.(had some unrelated problems i thought was a bug in the resource)@all: Glad I could help!
@deepscratch: You helped more then you think, I appreciate it too.
@anthony: I've uploaded a (very)short video showing the basics of the Flashlight Resource..
#9
I will try it and see. Thanks!
03/11/2011 (12:58 pm)
This looks better than the one I am using for my game.I will try it and see. Thanks!
#10
03/11/2011 (4:33 pm)
remember you can add lens/light flare to the lights,flareType = "LightFlareExample1";
#11
I had to modify my mountOffset because I could only see the flare when I looked all the way down with the character, the end result mountOffset I used was just below the hand and I could look straight ahead and run without the flare blinding me.
03/11/2011 (8:07 pm)
@deepscratch: Thats all that was missing, I'm going to update the resource with the flare and mountOffset.I had to modify my mountOffset because I could only see the flare when I looked all the way down with the character, the end result mountOffset I used was just below the hand and I could look straight ahead and run without the flare blinding me.
#12
If my player get a high loot, it give now a nice particle effect around him. TY for the resource. Just had to change some little things that it work like i was need for my game.
03/12/2011 (1:27 pm)
This resource is nice. I did use a particle effect, not the light. And now i can mount some particle effects to my players. :)If my player get a high loot, it give now a nice particle effect around him. TY for the resource. Just had to change some little things that it work like i was need for my game.
#14

Any idea what I'm doing wrong?
03/23/2011 (1:31 pm)
When using this light. It is passing through objects.
Any idea what I'm doing wrong?
#15
Note: Has been logged as a T3D Beta3 Bug in the forums.
03/24/2011 (8:32 am)
@Carl: your not doing anything wrong, it seems like the spotlight code needs some work to acknowledge collision/visible meshes... This should be logged as a bug.Note: Has been logged as a T3D Beta3 Bug in the forums.
#16
~ default.bind.cs
~ player.cs
~ commands.cs
06/01/2011 (11:16 pm)
thanks, was able to quickly make a laser target sight using this method as well ...~ default.bind.cs
$TargetLaserVar = "1";
function toggleTargetLaser(%val)
{
if (%val)
{
if($TargetLaserVar)
{
$TargetLaserVar = "0";
commandToServer('EnableTargetLaser');
}
else
{
$TargetLaserVar = "1";
commandToServer('DisableTargetLaser');
}
}
}
moveMap.bind(keyboard, "t", toggleTargetLaser);~ player.cs
//----------------------------------------------------------------------------
// Enable/Disable Player TargetLaser
//----------------------------------------------------------------------------
function Player::TargetLaserEnable(%this)
{
// spotlight
%light = new SpotLight() // In here you can add the example datablock values for more control
{
range = "50";
innerAngle = 0;
outerAngle = 1;
brightness = "10";
color = "1 0 0 1"; // red?
cookie = "art/gui/laser.png"; // Basic Lighting will not render this.
// Lens Flare will need a good mount and will need the mountOffset of the Flashlight
// to be modified for your specific application.
flareType = "LightFlareExample2";
};
%this.mountobject(%light, 0, "0.05 0.68 -0.09"); // mountobject(%obj, mountNode, mountOffset)
%this.light = %light;
}
function Player::TargetLaserDisable(%this)
{
// delete lights
if(isObject(%this.light))
%this.light.delete();
}~ commands.cs
//
function serverCmdEnableTargetLaser(%client)
{
%player = %client.player;
%player.TargetLaserEnable();
}
function serverCmdDisableTargetLaser(%client)
{
%player = %client.player;
%player.TargetLaserDisable();
}
#17
06/05/2011 (6:04 pm)
Nice, thanks for the example!
#18
'scripts/server/players.cs'
and change
It's not a bug just that you will need it to cast shadows to stop the light rays.
08/15/2011 (9:02 pm)
@Carl To fix this issue you will want to enable shadows for the light so go into'scripts/server/players.cs'
and change
%light = new SpotLight() // In here you can add the example datablock values for more control
{
range = "40";
cookie = "art/gui/light.png"; // Basic Lighting will not render this.
castshadows = true;
// Lens Flare will need a good mount and will need the mountOffset of the Flashlight
// to be modified for your specific application.
flareType = "LightFlareExample1";
};It's not a bug just that you will need it to cast shadows to stop the light rays.
#19
Note: not only does the 'castShadows' variable need to be set, also '$pref::Shadows::disable' must be false.(which the LightingQuality graphics option may or may not set when altering graphics settings)
08/18/2011 (1:09 pm)
@To-mos: Actually thats the same answer that was found upon the "bug" thread, Thanks for clearing that up in here though!Note: not only does the 'castShadows' variable need to be set, also '$pref::Shadows::disable' must be false.(which the LightingQuality graphics option may or may not set when altering graphics settings)
#20
'scripts/server/player.cs'
At the end of function Armor::onDisabled() add
01/19/2012 (7:40 am)
I didn't like having the flashlight remain on the corpse after death so I added:'scripts/server/player.cs'
At the end of function Armor::onDisabled() add
%obj.FlashlightDisable();Hurray.

Torque 3D Owner Andy Wright
BrokeAss Games