[Tutorial] Weapon Magazine Reload


In this tutorial i will show you how to create a weapon with Reload function. I will also show you how to create a ammo pickup that will add clips instead of ammo. Then we will create a custom Inventory Manager to track our ammo and tells the weapon to add clips if the maximum clips is lesser the current clips.

So lets get started.

You will need 6 files for this tutorial. They are:

  • MyWeapon.uc
  • MyWeaponAmmo.uc
  • MyInventoryManager.uc
  • MyPlayerController.uc
  • MyPawn.uc
  • MyGame.uc

This is the code for MyWeapon.uc:

class MyWeapon extends UTWeapon;

var int clips; //Number of clips in our weapon
var int MaxClips; //Maximum number of clips supported by this weapon
var bool bIsReloading; //Boolean to check if the weapon is reloading
var float ReloadTime; //Reload Time
var (Animations) name WeaponReload; //Animation sequence name to play when weapon is reloading

simulated function WeaponEmpty()
{ 
   //If the clips is greater than 0 and the weapon is not reloading then...
   if(clips > 0 && !bIsReloading)
   { 
      bIsReloading = true; //Set bIsReloading to true
      PlayReloadAnim(); //Call reload animation function
      SetTimer(ReloadTime, false, 'Reload'); //Set Timer for the reload animation
      return; //Exit this function
   } 
   super.WeaponEmpty(); 
}

simulated function PlayReloadAnim()
{
    PlayWeaponAnimation(WeaponReload, ReloadTime);
}

//The function that we will call in MyInventoryManager.uc file
simulated function AddClip(int clip)
{
    //If clips is lesser than Maxclips then...
    if(clips < Maxclips)
    {
        clips = clips + clip; //Add a clip
        ClearTimer();
    }
    else
    {
        return;
    }
}

simulated function int IncreaseAmmo( int Amount )
{
    AmmoCount = Clamp(AmmoCount + Amount,0,32);
        return AmmoCount;
}

//The function that will reload this weapon
simulated function Reload()
{
    bIsReloading = false; //Set bIsReloading to false
    clips = clips - 1; //Reduce 1 clip
    IncreaseAmmo(32); //Add the amount of ammo you want in your weapon
    ClearTimer();
}
simulated function StartFire(byte FireModeNum)
{
    local PlayerController MyPlayerController;
    MyPlayerController = PlayerController(Instigator.Controller);

    if(MyPlayerController == none || bIsReloading) //You can't shoot while reloading
    {
        return;
    }
    if (AmmoCount == 0 && clips == 0)
    {
        return;
    }

    super.StartFire(FireModeNum);
}

defaultproperties
{
    bIsReloading = false
    clips = 3 //The number of clips that the player will have
    MaxClips = 4 //Maximum number of magazines that this weapon supports

    Begin Object class=AnimNodeSequence Name=MeshSequenceA
        bCauseActorAnimEnd=true
    End Object

    Begin Object Name=FirstPersonMesh
        SkeletalMesh= //Your Weapon Skeletal Mesh
        FOV=70
        Animations=MeshSequenceA
        AnimSets(0)= //Your Weapon AnimSet
        bForceUpdateAttachmentsInTick=True
        Scale= //Weapon Scale
    End Object
    WeaponReload = //Your Weapon Reload Animation Sequence name from your animset
    ReloadTime= //Reload Time
    MaxAmmoCount=250 //IT IS VERY IMPORTANT TO SET THIS VALUE GREATER THAN AmmoCount
    AmmoCount=32

    Mesh=FirstPersonMesh
    //Other Misc Codes
}

So thats our MyWeapon.uc file. Now lets create an ammo class to use with this weapon.  The Ammo class is self-explanatory.

class MyWeaponAmmo extends UTAmmoPickupFactory;

var int clip;

DefaultProperties
{
    Begin Object Name=AmmoMeshComp
        StaticMesh= //Ammo Static Mesh
        Translation=(X=0.0,Y=0.0,Z=-16.0)
        Scale=0.5
        HiddenEditor=false
    End Object

    Begin Object Name=CollisionCylinder
        CollisionHeight=14.4
    End Object

    AmmoAmount=0
    clip=1

    PickupSound=SoundCue'A_Pickups.Ammo.Cue.A_Pickup_Ammo_Link_Cue'
    TargetWeapon=Class'MyWeapon'
}

Now lets create our Custom Inventory Manager. This custom inventory will track our weapons ammo and add the clips if it is necessary.

class MyInventoryManager extends UTInventoryManager;

function bool NeedsAmmo(class<UTWeapon> TestWeapon)
{
    local array WeaponList;
    local int i;

    // Check the list of weapons
    GetWeaponList(WeaponList);
    for (i=0;i<WeaponList.Length;i++)
    {
        if ( ClassIsChildOf(WeaponList[i].Class, TestWeapon) )// Pawn has this weapon
        {
            //This will check if our current clip is lesser than Maximum clips defined in MyWeapon
            if ( MyWeapon(WeaponList[i]).clips < MyWeapon(WeaponList[i]).MaxClips )
                return true;
            else
                return false;
        }
    }

    // Check our stores.
    for (i=0;i<AmmoStorage.Length;i++)
    {
        if ( ClassIsChildOf(WeaponList[i].Class, TestWeapon) )
        {
            if ( AmmoStorage[i].Amount < TestWeapon.default.MaxAmmoCount )
                //MyWeapon can pickup clips. So return true
                return true;
            else
                //MyWeapon cannot pickup clips because clips is equal to MaxClips
                return false;
        }
    }

    return true;

}

//This function will add clips to our weapon. Even though it says "AddAmmoToWeapon" this will add clips.
function AddAmmoToWeapon(int AmountToAdd, class<UTWeapon> WeaponClassToAddTo)
{
    local array WeaponInv;
    local int j;
    GetWeaponList(WeaponInv);
    for (j=0;j<WeaponInv.Length;j++)
    {
        if ( ClassIsChildOf(WeaponInv[j].Class, WeaponClassToAddTo) ) // Pawn has this weapon
        {
            MyWeapon(WeaponInv[j]).AddClip(1);
        }
    }
    super.AddAmmoToWeapon(AmountToAdd, WeaponClassToAddTo);
}

defaultproperties
{
    PendingFire(0)=0
}

Now lets create our PlayerController class.

class MyPlayerController extends UTPlayerController;

//This is our reload function. You can call this either through Console or by binding a key to this function.
exec function RequestReload()
{
    local MyWeapon ThisIsMyWeapon;
    local int clips;
    ThisIsMyWeapon = MyWeapon(Pawn.Weapon);

    if(ThisIsMyWeapon != none)
    {
        clips = ThisIsMyWeapon.clips;

        //If clips is greater than 0 and IsNotReloading and Ammo is not equal to MyWeapon default Ammo Count then...
        if(clips > 0 && !ThisIsMyWeapon.bIsReloading && ThisIsMyWeapon.AmmoCount != ThisIsMyWeapon.default.AmmoCount)
        {
            ThisIsMyWeapon.bIsReloading = true; //Set MyWeapon bIsReloading to True
            ThisIsMyWeapon.PlayReloadAnim(); //Call the reload animation function
            ThisIsMyWeapon.SetTimer(ThisIsMyWeapon.default.ReloadTime, false, 'Reload'); //Set Timer to wait until the weapon reloads
        }
    }
}

Now create our Pawn Class.

class MyPawn extends UTPawn;

function AddDefaultInventory()
{
    //Add our weapon
    InvManager.CreateInventory(class'MyWeapon');
}

defaultproperties
{
    InventoryManagerClass= //Add your Inventory Class here. eg: class'UTGame.MyInventoryManager'
}

Finally create the game class so that we can use our pawn, playercontroller etc

class MyGame extends UTGame;

defaultproperties
{
    PlayerControllerClass=class'MyPlayerController'
    DefaultPawnClass=class'MyPawn'

    //Weapon is applied using AddDefaultInventory() function in MyPawn
    DefaultInventory(0)=None
    bGivePhysicsGun=false
}

Thats it!!. Compile the scripts and run UDK. Go to View — > Game Type –> PIE –> Select MyGame. Then open Content Browser –> Actor Classes and add our MyWeaponAmmo to the world. Then Build Paths –> Start Game –> Enjoy! 🙂

Advertisements

About Satheesh (aka ryanjon2040)

Genuine Unreal Engine user

Posted on October 2, 2012, in Tutorials and tagged , , , , , , , , . Bookmark the permalink. 20 Comments.

  1. MyInventoryManager class is broken!
    Bad syntax for defining the “WeaponList” array in line 5.

    Like

  2. Nice tut, I just post some improvement on the forums.epicgames. Finally thanks to this, my weap is correctly working.

    Like

  3. Well I have it working but what’s not working is ammopickup. I mean when the game type is set to UTGame than it is alright. But when I set it to MyGame even if MyGame is based on UTGame so it should be just fine, it wont pick up. Do you know what to do?

    Thanks

    Like

  4. me ag... and so on

    WOW how are you doing that you are always so quick. xD Well why not. BTW could I add you on skype? We can handle it there if you dont mind that i will know your skype. Or you can add me: wilda95. Thanks

    Like

  5. ummm…..I couldn’t pick up ammo for other weapons

    Like

  6. Error on line 25 of PlayerController :
    exec function RequestReload()
    {
    local DT_P90_weapon ThisIsMyWeapon;
    local int clips;
    ThisIsMyWeapon = DT_P90_weapon(‘DTgame.DT_P90_weapon’); (”) ‘dt….’ gives Error Illegal character in name) if i dont put (”) and leav it to (dt….) it gives error bad or missing expression for token : DT_P90_weapon in ‘=’

    if(ThisIsMyWeapon != none)
    {
    clips = ThisIsMyWeapon.clips;

    //If clips is greater than 0 and IsNotReloading and Ammo is not equal to MyWeapon default Ammo Count then…
    if(clips > 0 && !ThisIsMyWeapon.bIsReloading && ThisIsMyWeapon.AmmoCount != ThisIsMyWeapon.default.AmmoCount)
    {
    ThisIsMyWeapon.bIsReloading = true; //Set MyWeapon bIsReloading to True
    ThisIsMyWeapon.PlayReloadAnim(); //Call the reload animation function
    ThisIsMyWeapon.SetTimer(ThisIsMyWeapon.default.ReloadTime, false, ‘Reload’); //Set Timer to wait until the weapon reloads
    }
    }
    }
    Any ideas ???

    Like

    • Oh ok I’m
      STUPID…. 😀
      exec function RequestReload(){
      local DT_P90_weapon ThisIsMyWeapon;
      local int clips;
      ThisIsMyWeapon = DT_P90_weapon(Pawn.Weapon);

      0 errors …

      Like

      • Yay…Glad you figured that out. 🙂

        Like

      • WTF??? 0 errors in compile but it’s NOT working no reload when R is pressed (sure i bind it -.-‘) , nothing marks nomber of clips and if I take the same weapon on ground it adds ammo to weapon so P90 50rnd wep shoots 200 bullets on full load and then reloads 50 ???? ….

        Like

      • Hmmm not sure but since it has 3 clips and can add only one more total of 4 , so the weapon spawn with 50 rnd’s shoots all then reload 50×3 times but if i have the weapon loaded 50 rnd’s and pickup the same weapon it marks 100,150 and 200 (MaxAmmoCount=200) so if i set Max to 50 it shuold have a max of 50 and reload 3 times 50 ? am i correct ? i think id could work … btw i’m not a scripter so dont know but il try it out.

        Like

  7. I’m having some issues. If the bullets finish, automatically function is working with reload animation but if I’m pressing R key, animation is not showing, magazine is reloading. How can I show the animation with R key?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: