• Register
Post tutorial Report RSS 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 on - 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;

DefaultProperties
{
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);
}
}
else
{
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
InAction - - 103 comments

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

Reply Good karma Bad karma+1 vote
ambershee Author
ambershee - - 865 comments

You're welcome =]

Reply Good karma+1 vote
peterpanties
peterpanties - - 11 comments

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.

Reply Good karma Bad karma+1 vote
peterpanties
peterpanties - - 11 comments

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.

Reply Good karma Bad karma+1 vote
ambershee Author
ambershee - - 865 comments

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.

Reply Good karma+1 vote
peterpanties
peterpanties - - 11 comments

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

Reply Good karma Bad karma+1 vote
ambershee Author
ambershee - - 865 comments

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.

Reply Good karma+1 vote
peterpanties
peterpanties - - 11 comments

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.

Reply Good karma Bad karma+1 vote
peterpanties
peterpanties - - 11 comments

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.

Reply Good karma Bad karma+1 vote
loki2643
loki2643 - - 1 comments

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?

Reply Good karma Bad karma+1 vote
ambershee Author
ambershee - - 865 comments

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!

Reply Good karma+1 vote
brunomartelli
brunomartelli - - 4 comments

Any chance of posting the compiled mutator??

Reply Good karma Bad karma+1 vote
Gleedo
Gleedo - - 7 comments

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'

Reply Good karma Bad karma+1 vote
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: