Unreal Tournament 3 marks the return of the world's première first-person shooter. It unleashes the full power of Unreal Engine 3, taking graphics, gameplay, and challenge to a whole new level. Players engage in intense battles with other human players online, or against Unreal artificial intelligence that sets the industry standard. With the most powerful futuristic weapons and vehicles available, this is FPS action at its best!

Post tutorial Report article RSS Feed Unreal Learning #5: Attachments

This tutorial will show you how to write a mutator that attaches a spotlight to a player. The code was written by Luke 'Ambershee' Parkes-Haskell for Shee Labs Mutator Week.

Posted by ambershee on Feb 28th, 2008
Basic Server Side Coding.

Many of you will be waiting for the third and final part of the 4th tutorial, but hopefully this will tie you over until I can fight it past the ModDB text editor!

Today we're going to recreate a mutator I wrote for Shee Labs Mutator Week! You can read about it here: Moddb.com

It's very short, very sweet and easy to modify to suit your needs. We're going to attach a spotlight to players in the game. This will make it as so they have a torch in the game! Perfect for if you plan to play on any super dark maps any time soon. We'll give red team players red torches, and blue team players blue torches too, just to mix things up a bit.

First things first, we need a class and some properties! This should be easy by now.

class LP_Mutator_Flashlight extends UTMutator;

var Color RedColour, BlueColour, LightColour;
var float LightScale;

RedColour = (R = 255, G = 25, B = 25)
BlueColour = (R = 25, G = 25, B = 255)
LightColour = (R = 255, G = 255, B = 255)
LightScale = 1;

Yeah, easy as pie. I bet you could have done that in shorter time than it took you to read it. The next part is more tricky, and it's all one big function, so let's take a look at it and break it down bit by bit.

function ModifyPlayer(Pawn Other)
local SpotLightComponent LightAttachment;

if (Other.IsA('UTPawn'))
LightAttachment = new(self) class'SpotLightComponent';

if (WorldInfo.Game.bTeamGame == true)
if (UTPawn( Other ).Controller.PlayerReplicationInfo.Team.TeamIndex == 0)
LightAttachment.SetLightProperties(LightScale, RedColour);
else if (UTPawn( Other ).Controller.PlayerReplicationInfo.Team.TeamIndex == 1)
LightAttachment.SetLightProperties(LightScale, BlueColour);
LightAttachment.SetLightProperties(LightScale, LightColour);

LightAttachment.CastDynamicShadows = true;
LightAttachment.SetEnabled( true );

UTPawn( Other ).AttachComponent( LightAttachment );

super.ModifyPlayer( Other );

We use the ModifyPlayer function to modify our players every time they are spawned, and we create a SpotLightComponent to use as our torch. This part is pretty simple. First things first, we check to make sure what we have is a UTPawn - and Unreal Tournament player. When we find one, we create a brand new LightAttachment for that pawn.

We then get some details about the game mode - we want to know if it is a team game, or a free for all game. Every game mode has a boolean bTeamGame, which we can retrieve from the WorldInfo. If it's true, then we'll check what team the pawn is on, by checking the PlayerReplicationInfo in the player controller. If the team is 0, then the player is on the red team. If it is 1, then they are on the blue team. We set the light brightness to the relevant colour. If not, we set the normal colour.

Finally, we allow the LightAttachment to cast shadows and, we turn it on (SetEnabled), and attach it to the pawn using the AttachComponent function. We call the super function of ModifyPlayer to make sure all modify player is dealt with.

Compile, and play!

That's really it, it's that easy.

Post comment Comments
InAction Feb 29 2008, 2:02am says:

Thanks a lot for this one. Will help me a lot later!

+1 vote     reply to comment
ambershee Author
ambershee Feb 29 2008, 2:24am says:

You're welcome =]

+1 vote   reply to comment
peterpanties Mar 25 2008, 4:51pm says:

Does all this code go in one file? Or do I need to put modifyplayer in a different file? I get a runtime error when I use the mutator.

+1 vote     reply to comment
peterpanties Mar 25 2008, 5:16pm says:

Ah, I figured it out. I had added an extra } to where I thought there was one missing. Code (while ugly due to tabbing) works as is. However, I do wonder does default properties have to be at the end of a file? Or can it be anywhere? A note about that could be helpful.

Also some more description on exacty modifyplayer is working would be helpful. Where is it getting the list of all the pawns? When does it cycle through them? Curious. Good stuff though.

+1 vote     reply to comment
ambershee Author
ambershee Mar 26 2008, 5:05am replied:

Good to know you caught it. The code here isn't tabbed out because of a limitation with the text editor - from time to time it conks out and loses all the tabs - even if you type it out in HTML. I'll fix it as soon as it's possible.

+1 vote   reply to comment
peterpanties Mar 25 2008, 5:28pm says:

Also, what's the difference between a spotlight component and spotlight moveable or toggeable that we see in the editor?

+1 vote     reply to comment
ambershee Author
ambershee Mar 26 2008, 5:08am replied:

The way they are used. A SpotLightComponent isn't actually a light - it's an object that is a component, and has a SpotLight, which gives you the functionality you want from both. A SpotLight, is well, a SpotLight. SpotLight moveable and toggleable are used in the editor, and are built to be hooked up to Kismet and Matinee sequences.

+1 vote   reply to comment
peterpanties Mar 25 2008, 9:46pm says:

ModifyPlayer is executed on every Pawn as it is spawned in the game. Good to know. Also, AddAttachment is defined in Actor so pretty much anything in the game can have it. Sweet. Still don't understand the component stuff though.

+1 vote     reply to comment
peterpanties Mar 26 2008, 11:16am says:

Also, for your tabbing. If you try putting down an Anchor before you paste your tabbed text, I think it works better. At least it worked for me once haha. Thanks for the tips.

+1 vote     reply to comment
loki2643 Apr 13 2008, 2:18am says:

I don't know, what I've done wrong. First I copied the code into my .uc from here, then I tried to copy the code from the complete package ('MWLightv100'). The compiling went fine, the mutator showed up in the list ... but the light didn't.
Then I thought 'damn, maybe some scripts changed with patch 1.2 and it doesn't run anymore'. But I tried the whole package and it ran just fine... I'll give it some more tries and hope that I'll find the problem.

But I've got a question to the running version: The spotlight just points forward - is it possible to make it change the angle in height when looking upwards, too?

+1 vote     reply to comment
ambershee Author
ambershee Apr 13 2008, 5:52am says:

If the light isn't showing up, check your log files. If there's nothing obvious there, then you can up the brightness of the lights to double-check.

As for a spotlight with a player-based direction; yes it's possible, but it'll be quite a bit more work!

+1 vote   reply to comment
brunomartelli Apr 11 2009, 3:55pm replied:

Any chance of posting the compiled mutator??

+1 vote     reply to comment
Gleedo Mar 25 2009, 5:40pm says:

I tried this mutator, but i get an error in my log file saying:
Error: Error reading attributes for 'C:\Program Files (x86)\Unreal Tournament 3\ Binaries\..\UTGame\ Unpublished\CookedPC\ Script\LP_Mutator_Flashlight.u'

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Unreal Tournament 3
Windows, X360, PS3
Epic Games
Midway Games
Send Message
Official Page
Release Date
Released Nov 2007
Game Watch
Track this game
Report Abuse
Report article
Related Games
Unreal Tournament 3
Unreal Tournament 3 Single & Multiplayer First Person Shooter
Related Engines
Unreal Engine 3
Unreal Engine 3 Commercial Released Aug 2007
Related Groups
Epic Games
Epic Games Developer & Publisher with 6 members
Midway Games
Midway Games Developer & Publisher
Shee Labs Game Development
Shee Labs Game Development Developer & Publisher with 14 members
Unreal Tournament 3 Mod Developers
Unreal Tournament 3 Mod Developers Fans & Clans group with 232 members