Announcement

Collapse
No announcement yet.

Super simple shotgun!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Super simple shotgun!

    Hello people!
    Welcome to my (first ever) tutorial on how to code a super simple shotgun in UDK using UScript!

    Knowledge needed: Beginner.

    In this tutorial we will derive from UT classes, You will create a BaseWeapon class and make a ShotgunWeapon deriving from that class.

    1. We will begin with making the BaseWeapon class.

    First thing we need to do is to make our class extend the original UTWeapon class.
    Code:
    class BaseWeapon extends UTWeapon;
    Now we add the variables used within' our code.
    Code:
    var int Shells; // Amount of bullets to be shot!
    var bIsShotgun; // Used as a detector if the weapon should fire as a shotgun or normally.
    Shells, is the amount of bullets the gun will fire at the same time. This is declared in the BaseWeapon class so that weapons deriving from this class can easily configure this. (NOTE: Consummation of ammo is not in this tutorial!)

    bIsShotgun, is a bool that will show if the weapon is a shotgun, which is also declared in the weapon class.

    2. Making the fire function shoot more than one bullet!

    The functions we will use here are from UTWeapon and are handling instantfire & projectile fire modes.
    We will begin with ProjectileFire().

    Code:
    simulated function Projectile ProjectileFire() // Main function from UTWeapon
    {
                 local int s;
    
    	if(bIsShotGun == true) // Make the function check if our weapon actually is a shotgun.
    	{
                for(s=0; s < Shells; s++) // Spawn the wanted amount of projectiles.
                {
                    return super.ProjectileFire();
                }
            }
            else // If it isn't a shotgun then just do the normal ProjectileFire.
            {
               return super.ProjectileFire(); // Link to normal ProjectileFire().
            }
            return None;
    }
    Since we have added "super." before ProjectileFire(), it will do what the function does in the main class. Which is spawn a projectile.

    The "for(s=0; s < Shells; s++)" tells the function this: If this weapon is a shotgun, then everytime s IS LESS than Shells, Do this inside {} and add +1 to 's'.
    This is why we declared Shells because now we can choose how many shells we want to spawn by simply giving the desired number to Shells since the "for" function will run until 's' hits the same amount as Shells.

    3. Final part of base code!

    Code:
    defaultproperties
    {
        bIsShotgun=false
        Shells=8
    }
    Here we put 'bIsShotgun=false' because mainly the weapons aren't shotguns and it's for learning purpose.
    Shells is the amount of bullets to spawn, put it at the standard amount of bullets you want for shotgun weapons.

    Congratulations! You are now done with your BaseWeapon class!

    To make the actual shotgun now, just make a new class extend it from our base class and add the meshes and add in default properties -
    Code:
    defaultproperties
    {
          /* Code here */
          Shells=5
          bIsShotgun=true
    }
    EXAMPLE WEAPON USING OUR BASECLASS & IS A SHOTGUN (simply copy, paste & compile)

    Code:
    class TUT_Shotgun extends BaseWeapon;
    
    defaultproperties
    {
            /* WEAPON FIRE PROPERTIES! */
    	ShotCost(0)=0 // keep these for testing purpose!
    	ShotCost(1)=0
    	MaxAmmoCount=1
    	
    	/* CUSTOM CHECKS FOR SHOTGUN! */
    	bIsShotgun=true // Tells BaseWeapon this is a shotgun!
    	Shells=5 // We want 5 shells!
            
            /* WEAPON FIRING MODES! */
    	FiringStatesArray(0)=WeaponFiring
    	FiringStatesArray(1)=WeaponFiring
    	WeaponFireTypes(0)=EWFT_Projectile // First fire mode is a projectile
    	WeaponFireTypes(1)=EWFT_InstantHit // second is a instanthit!
    
    	WeaponProjectiles(0)=class'UTProj_Rocket' // The projectile you want the gun to fire!
    	FireInterval(0)=+0.3 // Fire speed!
    	Spread(0)=0.22 // the spread of the bullets!
    
    	// Weapon SkeletalMesh
    	Begin Object class=AnimNodeSequence Name=MeshSequenceA
    	End Object
            AttachmentClass=class'UTAttachment_ShockRifle'
    
    	// Weapon SkeletalMesh
    	Begin Object Name=FirstPersonMesh
    		SkeletalMesh=SkeletalMesh'WP_ShockRifle.Mesh.SK_WP_ShockRifle_1P'
    		AnimSets(0)=AnimSet'WP_ShockRifle.Anim.K_WP_ShockRifle_1P_Base'
    		Animations=MeshSequenceA
    		Rotation=(Yaw=-16384)
    		FOV=60.0
    	End Object
    
    	Begin Object Name=PickupMesh
    		SkeletalMesh=SkeletalMesh'WP_ShockRifle.Mesh.SK_WP_ShockRifle_3P' // either keep this or change to your 3rd person mesh.
    	End Object
    
    	FireOffset=(X=17,Y=0)
    	PlayerViewOffset=(X=14,Y=0,Z=-10.0) // Correction for Shockrifle
    }
    Finished!
    Now we have a nice rocket shooting... O.O ... shotgun.


    Here is the code to make a burst & shotgun weapon, please follow above tutorial to know how to make this work correctly, THANKS TO ACECUTTER69 FOR THIS!

    Code:
    class UTWeapAdv extends UTWeapon;
    
    var bool bIsShotgun;
    var int Shrapnellcount;
    
    var bool bBurst;
    var int Burst, BurstCount;
    var float BurstInterval;
    
    simulated function InstantFire() // Main function from UTWeapon
    {
    	local int i;
    
            if(bIsShotGun == true) // Make the function check if our weapon actually is a shotgun.
    	{
                for(i=0; i < ShrapnellCount; i++) // Loop And shoot the amount of shrapnell.
                {
                    super.InstantFire();
                }
            }
            else if(bBurst)
            {
                    Burst();  
            }
            else // If it isn't a shotgun then just do the normal Fire.
            {
                super.InstantFire();
            }
    }
    
    simulated function Burst()
    {
             if(bProjectile) //dont have source with me atm but replace with the check if should fire proj of instant trace
                  Super.ProjectileFire();
             else
                  Super.InstantFire();
    
            if(BurstCount < Burst)    
                SetTimer(BurstInterval, false, 'Burst');
            else
                BurstCount = 0;
    }
    
    simulated function Projectile ProjectileFire() // Main function from UTWeapon
    {
    	local int i;
            local projectile P;
    
            if(bIsShotGun == true) // Make the function check if our weapon actually is a shotgun.
    	{
                for(i=0; i < ShrapnellCount; i++) // Spawn the wanted amount of projectiles.
                {
                    P = super.ProjectileFire();
                }
            }
            else // If it isn't a shotgun then just do the normal ProjectileFire.
            {
                P = super.ProjectileFire(); // Link to normal ProjectileFire().
            }
            return P; // Return what the super function calls.
    }
    Feel free to ask any questions below!

    Credits!
    TheAgent & another guy that I can remember his name for unconsiosly helping me.
    Acecutter69 for burst fire code.
    VendorX for tip on right approaches.
    100GPing100 for helping code be cleaner.
    Fixer for finding and providing a mistake I did.

    #2
    well i would change this:

    Code:
    class UTWeapAdv extends UTWeapon;
    
    var bool bIsShotgun;
    var int Shrapnellcount;
    
    var bool bBurst;
    var int Burst, BurstCount;
    var float BurstInterval;
    
    simulated function InstantFire() // Main function from UTWeapon
    {
    	local int i;
    
            if(bIsShotGun == true) // Make the function check if our weapon actually is a shotgun.
    	{
                for(i=0; i < ShrapnellCount; i++) // Loop And shoot the amount of shrapnell.
                {
                    super.InstantFire();
                }
            }
            else if(bBurst)
            {
                    Burst();  
            }
            else // If it isn't a shotgun then just do the normal Fire.
            {
                super.InstantFire();
            }
    }
    
    simulated function Burst()
    {
             if(bProjectile) //dont have source with me atm but replace with the check if should fire proj of instant trace
                  Super.ProjectileFire();
             else
                  Super.InstantFire();
    
            if(BurstCount < Burst)    
                SetTimer(BurstInterval, false, 'Burst');
            else
                BurstCount = 0;
    }
    
    simulated function Projectile ProjectileFire() // Main function from UTWeapon
    {
    	local int i;
            local projectile P;
    
            if(bIsShotGun == true) // Make the function check if our weapon actually is a shotgun.
    	{
                for(i=0; i < ShrapnellCount; i++) // Spawn the wanted amount of projectiles.
                {
                    P = super.ProjectileFire();
                }
            }
            else // If it isn't a shotgun then just do the normal ProjectileFire.
            {
                P = super.ProjectileFire(); // Link to normal ProjectileFire().
            }
            return P; // Return what the super function calls.
    }
    
    defaultproperties
    {
    
    }
    that way you can create shotgun from a new master class or a burst weapon. Works out of the same idea. It works with either projectile or instance trace

    Comment


      #3
      That's exactly what I did but you added an Burst function & the instanthit (which I was gonna add later. xD).
      Either way that is also an possibility if you want a burst function, otherwise it's pretty much the same thing except mines simpler and more for people who have not scripted before at all.

      Comment


        #4
        just wanted to add to it

        Comment


          #5
          Originally posted by Acecutter69 View Post
          just wanted to add to it
          Sure! Just leave it there, it fits perfectly at top

          Comment


            #6
            u may wanna add a reload function and ironsight. the ironsight really depends on whether it is a TPS or a FPS.

            Comment


              #7
              if u want i can tell u how to do the reload.

              Comment


                #8
                This is merely a tutorial for the function of firing multiple bullets in a single click (like a shotgun) not an actual complete weapon guide.

                Comment


                  #9
                  k then. just a suggestion thats it. nothing wrong with a suggestion

                  Comment


                    #10
                    Originally posted by blaklion View Post
                    k then. just a suggestion thats it. nothing wrong with a suggestion
                    Didn't say there was anything wrong with it

                    Comment


                      #11
                      great then!

                      Comment


                        #12
                        I understand this is a simple shotgun tutorial, but would this not just shoot multiple bullets in the same direction instead of putting some kind if "spread" on the shotgun? I haven't looked into the fire projectile function, this is just a devil's advocate question for anyone doing this tutorial and expecting certain results.

                        Comment


                          #13
                          nah since ut3 2.0 they gave us a spread modifier for the weapon. Its Spread(0) and spread(1). depending on that value you spread will be closer of farther, being that 0 is no spread at all. so unless you want at least one shrepnell to hit the middle, that code will do just fine.

                          Comment


                            #14
                            Originally posted by lovindadonks View Post
                            I understand this is a simple shotgun tutorial, but would this not just shoot multiple bullets in the same direction instead of putting some kind if "spread" on the shotgun? I haven't looked into the fire projectile function, this is just a devil's advocate question for anyone doing this tutorial and expecting certain results.
                            You would believe that but since this derives from the main UTWeapon class, Spread(0) is already defined! In the example shotgun I provide you can see that spread is defined into a new value. Good question!

                            Comment


                              #15
                              Absolutely fantastic tutorial, thank you very much! I had a very similar setup in my shotgun class, but having it set in a base weapon file, (and with the handy burst code too!) will be a lot of help later on.

                              By. The. Way.

                              Anyone have any clue at all why my shotgun will kill me about 1/3 times I fire? None of the other weapons I have do so, but this shotgun code seems to kill myself!

                              I realize this may be unanswerable without looking at my crazy tangle of code, but if anyone had a good idea where to look, that'd help. Sorry I can't give any more than that, I'm sort of in a rush.

                              Comment

                              Working...
                              X