The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

Legacy:Changing The Enforcer (UT)

From Unreal Wiki, The Unreal Engine Documentation Site
Revision as of 05:44, 7 April 2010 by 81.236.19.16 (Talk) (Comments)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Preface[edit]

I once made a map a while back that had every player start with a bio rifle instead of the enforcer. The guys on BuF were a great help in getting it to work, but alas, a few formats later, the map is lost – and so, unfortunately, is the code to change the starting weapon.

Rather than asking on BuF again (the old posts with the info I wanted seem to have been deleted), I decided to work out a solution on my own - it's just more fun and more rewarding that way. It took a while of poking around other mutators and various places on the wiki, but I finally made it work. Every player starts with a bio rifle, and does not start with the enforcer.

Its been tested in multiplayer games briefly - seems to work just fine.

The only problem I can see with it is that it destroys ALL enforcers in the entire map, not just the ones being held by players. This was acceptable in my map, but it might not be in yours, and feel free to alter this tut to make it better. Also I don't have anything to prevent the mutator from being added twice, so don't put more than one instance in your map!

Step 1: Starting Out[edit]

This is a simple Embedded Mutator, we're going to start out by making the code to embed it into our map. Read the Embedded Mutator tutorial for more info on this block of code.

//
// BioStart.
//
class BioStart expands Mutator;
 
var bool bPreBPInitialized;
 
function PreBeginPlay()
{
 
    if ( !bPreBPInitialized ) // older versions of UT call this function twice but we
                                         // only want to run our code once!
    {
        bPreBPInitialized = True;
 
        // Add the mutator by linking it into the Mutator List ala Beppo.
        // This Embedded Mutator makes sense to be 1st after the BaseMutator
        // in the Mutator List...
 
        Self.NextMutator = Level.Game.BaseMutator.NextMutator; // Make a place in the List
 
        Level.Game.BaseMutator.NextMutator = Self; // place it 1st after BaseMutator
    }
}

Step 2: Giving the Weapon to the Player[edit]

I used a Useful Mutator Function here, namely GiveWeapon, to actually give the weapon to the player.

For giving the weapon (i.e. biorifle) to the player, we are going to use the ModifyPlayer function, which gets called whenever a player spawns, and is passed a handle to the spawning player. So we're going to call our GiveWeapon function every time a player spawns. Simple!

function Weapon GiveWeapon(Pawn PlayerPawn, string aClassName, optional bool bBringUp)
{
    local class<Weapon> WeaponClass< SEMI >
    local Weapon NewWeapon;
 
    WeaponClass = class<Weapon>(DynamicLoadObject(aClassName, class'Class'));
 
    if ( PlayerPawn.FindInventoryType(WeaponClass) != None )
        return None;
    newWeapon = Spawn(WeaponClass);
    if ( newWeapon != None ) {
        newWeapon.RespawnTime = 0.0;
        newWeapon.GiveTo(PlayerPawn);
        newWeapon.bHeldItem = true;
        newWeapon.GiveAmmo(PlayerPawn);
        newWeapon.SetSwitchPriority(PlayerPawn);
        newWeapon.WeaponSet(PlayerPawn);
        newWeapon.AmbientGlow = 0;
        if ( PlayerPawn.IsA('PlayerPawn') )
            newWeapon.SetHand(PlayerPawn(PlayerPawn).Handedness);
        else
            newWeapon.GotoState('Idle');
        if ( bBringUp ) {
            PlayerPawn.Weapon.GotoState('DownWeapon');
            PlayerPawn.PendingWeapon = None;
            PlayerPawn.Weapon = newWeapon;
            PlayerPawn.Weapon.BringUp();
        }
    }
    return newWeapon;
}
 
function ModifyPlayer (Pawn Other)
{
	GiveWeapon(Other, "Botpack.UT_BioRifle", true);
}

Step 3: Those Damn Enforcers[edit]

Great! The mutator should be working already. When you spawn you should start with the biorifle! But then you notice that the Enforcer is still in your inventory... Damn. Well lets get rid of those little buggers shall we? (NOTE: This code will remove ALL enforcers in the map. If you wish to have enforcers on the ground in your map anyway, this will have to be modified to not remove them.)

For this we are going to use the CheckReplacement function - at some point during map startup every actor in the map is passed through this function, so if that actor is an enforcer, we simply refuse it to be allowed into the map. All mutators must return true for this function for a particular object if that object is to be allowed in. So we'll return false simply if the actor IsA() enforcer!

function bool CheckReplacement (Actor Other, out byte bSuperRelevant)
{
	if (Other.IsA('enforcer'))
	{
		return false;
	}
 
	return true;
}

Wrapping Up[edit]

Well there you have it! Add this mutator into your map, and there you go. Changing the startup weapon wasn't so hard now was it? It certainly would have been easier for me to have a tut like this around... Please feel free to comment in the following section.

Comments[edit]

Draconx: Well its my second tut, and not particularly complicated, but its a good starting point for wannabe uscript coders (like me :)) I havent found any problems with it for what it does, aside from those mentioned in the preface.

Tarquin: Looks good :)

Uwa: If even half the code statements were explained with code comments or in the text, this might be useful for learning.