Legacy:Basic Weapon Class - Unreal Wiki

# Legacy:Basic Weapon Class

### From Unreal Wiki, The Unreal Engine Documentation Site

PLEASE NOTE: This page is finished, at least, computergod666 thinks so. You are free to add content at any time if you find something's missing.

## Creating Unreal Weapons

As far as I know, everything is the same for Weapons as TournamentWeapons (except for a few things like replication.)

## Creating a new TournamentWeapon

### The firing routines

Every weapon needs at least two firing modes. These are covered by two functions in the TournamentWeapon class:

    function Fire(float Value)

and

    function AltFire(float Value)

. These functions are set up so that you can simply specify whether the weapon is an instant hit weapon or if it fires projectiles, and what the class for Projectile and Alt Projectile are. These three values are specified by the variables "bInstantHit", "ProjectileClass", and "AltProjectileClass". You can choose to use these functions and finish your weapon as is, but that's never as interesting as writing these routines yourself.

Fire looks something like this in TournamentWeapon:

    function Fire( float Value )
    {
     if ( (AmmoType == None) && (AmmoName != None) )
     {
      // ammocheck
      GiveAmmo(Pawn(Owner));
     }
     if (AmmoType.UseAmmo(1))
     {
      GotoState('NormalFire');
      bPointing=True;
      bCanClientFire = true;
      ClientFire(Value);
      if (bInstantHit == true)
       TraceFire(0.0);
      else
       ProjectileFire(ProjectileClass, ProjSpeed, false);
     }
    }

The ammocheck code is not really important right now. However, you can use it for a weapon that is not supposed to run out of ammo, to give the player more ammo.

The line that says "(if AmmoType.UseAmmo(1))" attempts to use 1 unit of ammunition. If you are out of ammo, this fails and the weapon doesn't fire.

The line that begins with GoToState('NormalFire'); tells the weapon to activate that state. In TournamentWeapon, this state temporarily disables forcing bots or players to fire, and allows the user to add handling for things that need to happen before, during, or after firing. See Chainguns and Shotguns for more on using firing states.

The next three lines starting after GoToState('NormalFire') are for client/server interaction. We don't need to worry about them right now.

The TournamentWeapon class allows you to simply pick a projectile class to fire. If your weapon is a projectile weapon, make sure you set bInstantHit to False, and specify the ProjectileClass. Use of TraceFire will be covered in Hitscan weapons. Alternatively, you can simply delete the code for the TraceFire() call.

Please note that setting the ProjSpeed value for ProjectileFire will not change the speed of the projectile. This parameter is just for bots to adjust their aim to compensate for moving targets.

The same things are true for AltFire.

### Ammo

So we made the weapon use 1 unit of ammo per shot. That ammo has to come from somewhere! The AmmoName property is a class name that specifies what ammo class to use. The maximum amount of ammo you can carry is determined by the ammo class itself, but you can specify how much ammo you want to be in the gun when you pick it up. That is set by the PickupAmmoCount property. This can be any positive integer value, but if you try to pick up more than you can carry, your ammo just tops off.

So now, you can make a functioning weapon. Create a subclass of TournamentWeapon. All you need to put in the file are these lines:

    class MyWeapon extends TournamentWeapon;

    defaultproperties
    {
     ProjectileClass=Class'Botpack.ShockProj'
     AltProjectileClass=Class'Botpack.WarShell'
     bInstantHit=false
     AmmoName=Class'Botpack.miniammo'
     PickupAmmoCount=150
    }

Hehe. That is an extremely overpowered weapon with way too much ammunition. Please don't do that: overpowered weapons get boring way too quickly. So pick some slightly more sensible values, then compile.

So now you should have a functioning projectile weapon. However, if you put it in a map, you will notice that you can't see it. Even if you do manage to pick it up, you will not be able to see the first-person model. It will also be completely silent when it fires. Now, we need to give it a mesh, then some routines to animate it and play firing sounds.

### Adding meshes

There are three meshes for every weapon in Unreal Tournament:

1. The first-person mesh. This mesh is what you see when you are using the weapon. It is usually the most detailed version, and should include an animation for everything it does.
2. The third-person mesh. This is what everyone else sees when you are using the weapon. This version is usually not as detailed as the first-person mesh, and it includes only the animations that you would be able to see well from a distance, like firing.
3. The pickup mesh. This is what you see on the ground. It has no animations because at the most, it is meant to revolve slowly.

These three meshes are specified by three default variables: PlayerViewMesh, PickupMesh, and ThirdPersonMesh. You would specify them like this:

    defaultproperties
    {
     //...
     PlayerViewMesh=LODMesh'UnrealI.QuadShotHeld'
     PickupMesh=LODMesh'UnrealI.QuadShotPickup'
     ThirdPersonMesh=LODMesh'UnrealI.flakm'
     //...
    }

Next, you need to specify the distance the first-person mesh is from the center of the screen. This is specified by a vector called PlayerViewOffset. For the vector's components, positive X is vertical, toward the top of the screen (!), positive Y is horizontal toward the right of the screen, and positive Z is toward yourself. You may want to check the mesh itself, and this part usually requires some trial-and-error unless you made the mesh yourself. In the example above, I was trying to give the Quad Shot some code (It wasn't used in Unreal), but there weren't any values for PlayerViewOffset, so I had to figure it out myself. Now you can add this line to defaultproperties:

    defaultproperties
    {
     //...
     PlayerViewMesh=LODMesh'UnrealI.QuadShotHeld'
     PickupMesh=LODMesh'UnrealI.QuadShotPickup'
     ThirdPersonMesh=LODMesh'UnrealI.flakm'
     PlayerViewOffset=(X=-15.000,Y=15.000,Z=-30.000)
     //...
    }

One other thing you may want to change is a vector called FireOffset. This vector specifies where the projectile (or trace) should show up relative to the center of your screen, in first-person view. You may have to experiment with this, because it will have to match up reasonably well with the muzzle of your weapon.

Now, you have some meshes for your weapon! Let's make them do something...

### Animations

Animation allows you to see the weapon in action. Your weapon will need at least two functions to handle animation: PlayFiring() and PlayAltFiring(). For most weapons, they contain code that plays an animation and a sound. Here is a very generic set of routines:

    function PlayFiring()
    {
     PlayAnim('Fire', 1.0, 0.05);
     PlayOwnedSound(FireSound, SLOT_Misc, 1.0);
    }

    function PlayAltFiring()
    {
     PlayAnim('AltFire', 1.0, 0.05);
     PlayOwnedSound(AltFireSound, SLOT_Misc, 1.0);
    }

PlayAnim plays an animation from the mesh you have selected. The first parameter is the name of the animation. You can figure this out in the UnrealEd mesh browser if you are using an existing mesh, or you should know it if you created the mesh yourself. The second parameter is the relative playback speed of the animation. A value of 1.0 plays it at 'normal speed', 2.0 plays it twice as fast, 0.5 plays it half as fast, etc. The third parameter is the amount of time to tween from the previous animation to this one.

See Actor/Methods for more information on PlayAnim and PlayOwnedSound.

A note about animation

As I said earlier, there is a first-person and a third-person mesh. Playing an animation will animate both meshes: you don't have to make one call for each mesh. All you will need is these routines. Of course, you can also write your own. The Flak Cannon has a PlayReloading() function which it uses after it fires. This type of animation will be discussed in Chainguns and shotguns.

FireSound and AltFireSound are defined for every TournamentWeapon. You can set these to a sound resource in the defaultproperties section, like this:

    defaultproperties
    {
     //...
     PlayerViewMesh=LODMesh'UnrealI.QuadShotHeld'
     PickupMesh=LODMesh'UnrealI.QuadShotPickup'
     ThirdPersonMesh=LODMesh'UnrealI.flakm'
     PlayerViewOffset=(X=-15.000,Y=15.000,Z=-30.000)
     //...
     FireSound=Sound'Botpack.AMSD.ImpactAltFireRelease'
     AltFireSound=Sound'MyPackage.Group1.altfire1'
     //...
    }

You can also define your own sounds by adding a line at the top of the file like this:

    var Sound OverheatSound;

Now, you should have a functioning, firing weapon that you can see and pick up ammo for. This is only a projectile weapon. To make an instant-hit weapon, read Hitscan weapons.

TkoDark I need help fixing this i put this weapon in my map and now i cant fix it or get rid of it. can you e mail or im me computergod666? When i play the map i can pick the weapon up and change to it but it doesnt shoot or do anything, i messed up somewhere. I don't know how to fix it. In the editor (ued2.0) it is in the search for actors and i can go to it, but i cant see it so i cant click on it and delete it. thanks TKO

Ps where do i get your grenade launcher and vehicles? very cool! Email-tkodark@yahoo.com

##### Views

- Legacy
- Discussion
- Current revision

##### Search