My program doesn't have bugs. It just develops random features.

Legacy:Ultimate FlashLight 2k3

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

A really nice flashlight for UT2003. Watch it in action



I spent a few days trying to create a fairly realistic flashlight using projectors, then switched to dynamic lights after i didn't get the specific look i wanted, at the same time discovering they had a random darkening effect when projected onto certain terrain. This worked out better for the terrain issue, but i felt it looked too drab. What i ended up deciding on was a dynamic projector to get a focal point with adjustable FOV, and then keeping the light for ambience. To follow the FOV adjustment of the projector, i'm manually adjusting the LightRadius of the dynamic light, which is dependant on a trace done every tick. The resulting effect was quite stunning, and i decided it was too good to go unshared.

On a side note, i made my projective texture light-affected, which seems to partially cure the darkening effect on terrain.

What You Need[edit]

  1. A texture for the projector. I use a 256x256, DXT1 compressed bitmap. The full info is commented below.


Here is where everything takes place. bHasLight is toggled from an outside control. All you need to do is call spawn(class'Effect_TacLightProjector',Owner); where "Owner" is the pawn that will be owning the light. Then set its bHasLight variable, and the actor will take care of the rest.

// © 2003 Matt 'SquirrelZero' Farber
// "UT2k3 Ultimate Flashlight"
// I wrote this using _both_ a dynamic projector and a dynamic light, with the 
// dynamic projector as the focal point and lightsource providing general 
// ambient illumination.  Either one by itself is pretty dull, but combine the 
// two and you've got yourself a really nice effect if done correctly.
class Effect_TacLightProjector extends DynamicProjector;
// add a light too, there seems to be a problem with the projector darkening terrain sometimes
var Effect_TacLightGlow TacLightGlow;
var Pawn LightPawn;
var Controller LightController;
var bool bHasLight;
	// relevant variables needed by the client
	reliable if (Role == ROLE_Authority)
		LightPawn, LightController;
	// relevant variables needed by the client
	reliable if (Role == ROLE_Authority)
// setup the pawn and controller variables, spawn the dynamic light
simulated function PostBeginPlay()
	if( bProjectActor )
		SetCollision(True, False, False);
	if (Owner != None)
		LightPawn = Pawn(Owner);
		LightController = LightPawn.Controller;
	if (TacLightGlow == None && Role == ROLE_Authority)
		TacLightGlow = spawn(class'Effect_TacLightGlow');
// updates the taclight projector and dynamic light positions
simulated function Tick(float DeltaTime)
	local vector StartTrace,EndTrace,X,Y,Z,HitLocation,HitNormal;
	local material SurfaceMat;
	local float BeamLength;
	// we're changing its location and rotation, so detach it
	// fallback
	if (LightPawn == None || LightController == None || !bHasLight)
		if (TacLightGlow != None)
			TacLightGlow.bDynamicLight = false;
	// ok, first we need to get a location directly in front of the player
	StartTrace = LightPawn.Location + LightPawn.EyePosition();
	// not too far out, we don't want a flashlight that can shine across the map
	EndTrace = StartTrace + 1800*vector(LightController.GetViewRotation());
	// find out how far the first hit was
	BeamLength = VSize(StartTrace-HitLocation);
	// clear the base, we're going to be doing some adjustments
	if (Base != None);
	// this makes a neat focus effect when you get close to a wall
	if (BeamLength <= 90)
		SetLocation(StartTrace+vector(LightController.GetViewRotation())*Max((0.2*BeamLength), 1));
	// else we project it normally, positioning slightly in front of the player
	// reattach it
	// turns the dynamic light on if it's off
	if (!TacLightGlow.bDynamicLight)
		TacLightGlow.bDynamicLight = true;
	// better than tracing for the actor, makes it more projector-like
	if (SurfaceMat != None)
		// again, neat focus effect up close, starts earlier than the dynamic projector
		if (BeamLength <= 100)
			TacLightGlow.LightBrightness = TacLightGlow.Default.LightBrightness * (1.0 + (1.0 - (BeamLength/100)));
			TaclightGlow.LightRadius = TacLightGlow.Default.LightRadius * FMax(0.3,(BeamLength/100));
		// else we scale its radius and brightness depending on distance from the material
			// fades the lightsource out as it moves farther away
			if (BeamLength >= 1300)
				TacLightGlow.LightBrightness = TacLightGlow.Default.LightBrightness * ((1800-BeamLength)/500);
			// else normal brightness
				TacLightGlow.LightBrightness = TacLightGlow.Default.LightBrightness;
			// this makes the light act more like a spotlight, resizing depending on distance
			TacLightGlow.LightRadius = TacLightGlow.Default.LightRadius + (4.5 * (BeamLength/1900));
	// else if the trace returned nothing, then we put the light right in front of the player, scale its brightness down,
	// and give it normal radius.  This makes it seem as if the light is casting a dull glow  onto the ground in front 
	// of the player, and is dim enough for players to not notice the transition.
		TacLightGlow.LightRadius = TacLightGlow.Default.LightRadius;
		TacLightGlow.LightBrightness = 50;
	// for my projtexture, i'm using a 256x256 DXT1 texture, created in the same way as the bulldog headlights --
	// just a bright white circle dead center on a black background.  Make sure the texture is set to TC_Clamp
	// or it'll repeat itself over the whole surface.


A dynamic light used by the Effect_TacLightProjector class to provide extra ambience.

// © 2003 Matt 'SquirrelZero' Farber
class Effect_TacLightGlow extends Light;
function PostBeginPlay()
// makes bots "see" the light when it's on
function Timer()


In my case, i created a specialized exec function in my PlayerController class for toggling the light on or off, named ToggleItem(). Since my light will only be used by specific weapons and not all the time, the exec then calls my custom function SpecialFire(), declared in my Weapon class, on the currently held weapon. By default SpecialFire() is a blank function, but on taclight weapons here is where i insert my trigger for the light. This then calls a client->server (Reliable if Role < ROLE_Authority) replicated function, and the server spawns the light if it doesn't exist, else toggles it on or off if it does (bHasLight = true; or bHasLight = false;). The Owner of the light is always the weapon's Instigator, since by default the Instigator is replicated in the WeaponClass. To see how to specify the Owner when spawning the projector, look at the spawn() function in the Actor class.

Final Note[edit]

That's all there is to it, the projector actor handles nearly everything on its own. If you intend to use this method, please contact me and let me know, only because i'd like to hear how you made use of it.


MythOpus: Wow, this looks good. I've been looking for a flshlight for awhile now. My search is over :D I have a question though... how did you get this ingame and how do you turn it on ?? I've compiled it.. what next?

SquirrelZero: Thanks =]. I added an 'applications' section, to explain how i put it to use.

MythOpus: I see... Way outta my league :) So this is for Face Off then ?

SquirrelZero: nah, different mod coming out soon. Frag.Ops

Foxpaw: Could this page be renamed to something simpler, like just Flashlight or Creating a flashlight or something to that effect? "Ultimate Flashlight 2K3" sound like "Xtreme Advertising" run amok.

SquirrelZero: what am i advertising? This a free script. I'm not forcing anyone to use it over another one. Ultimate FlashLight 2k3 is the name of the program, i made it up in exactly 5 seconds, because honestly i didn't care what it was called as long as it was useful for someone. I mean cmon, that's along the same lines as asking someone to rename their mutator or mod. I'm sorry that my choice doesn't agree with you, but i think you're a bit too "Xtreme" yourself.

Mosquito: IN BEFORE THE LOCK!! wait... this isn't a forum.... oh well. Hey? how did you record the video WITH sound?!

Dark Pulse: He probably dumped Pictures repeatedly (I know there's a console command for this) and then Did a Rip of Audio using total Recorder or something, then bundled them together.

(v)adOnion: Be glad he's sharing his code - a lot of people don't and you'll have to figure stuff out all on your own.

Bslayerw: SquirrelZero, FragOps looks awesome. Thanks for sharing the code. It's people like you that make the modding community a better place. Sharing code like this is very valuable.

RegularX: Ditto here SZ. I just started researching into some good flashlight code, had kinda wondered about a path like this, then found this page. Exactly what I was looking for. Thanks for sharing, this will shave off a lot of frustration I think :)

SquirrelZero: I've gotten quite a few requests over the past few days on how to actually implement it in your own code, so i think i'll add some detailed examples soon. Gimme a few days to think them up tho :)

MythOpus: I know your probably busy with FragOps. (Impressive BTW) but how are those 'examples' coming along :P I want to just put the turn on flashlight exec function in my playercontroller but I don't want it to have anything to do with the current weapon. Any ideas?

EricBlade: I popped this in as a mutator in my game, and everything seems to be good on it, except that i'm not actually getting any light.  :( I don't know if I know enough to debug this, either. All the code seems to be functioning.. am I having replication issues?

MythOpus: Well, I haven't looked at this in a long time, but I don't think that it actually gives off light, as a light actor would. It should only display a projector of a light. Unless, that's the thing you're talking about.

EricBlade: Turned out it was replication issues.. a tad difficult thing to work with in a mutator, i'm thinking.. so i implemented it into my playercontroller, and spent an hour or so before realising that exec function flashlight() { .. } doesn't work, for no obvious reason. :) Now, I have it working, though it obviously makes a much better spot/pointlight than a flashlight. Since it is implemented as both a projector, and a light, it should give off light, and it does (although I had to change the "if(TacLightGlow == None && Role == ROLE_AUTHORITY)" to just if(TacLightGlow == None), else there was no light glow on the local players end, and the local players end filled with a zillion Accessed Nones. :) Now, to see if I can make it more flashlight like.. if anyone has suggestions, i'd love to hear :)

MythOpus:Modify the texture so that it will have those... flashlight 'rings' around the light?

EricBlade:Looks like that would be the first order. It looks like the Light part of this really doesn't seem to work. I actually commented out all the code having to do with the LightGlow, and it worked almost identically, except I didn't get weird spot flashes on parts of the level..

SuperApe: A different Flashlight example for UT200x. Not sure how it compares to this one, but it includes bot support, etc. Comments welcome on the UP forums.