Main
Tutorials











 

Basic Coding Part 2

Now that we have everything set up to get into scripting, let's take a look at some code and make our own weapon modification.

Weapon Code Organization

Looking in the XWeapons folder you will see that for each weapon there are quite a few files for it. In order to have a complete working weapon with all of its associated items like pickups and ammo, 8 different files are required. If you add a secondary fire mode, at least one more is required. This may seem overwhelming at first, but let's break down a weapon into all of its parts.



Let's take a look at what each of these is used for. In all of these, "MyWeapon" would be replaced with whatever name you have given your weapon.

MutatorMyWeapon - Optional code for a mutator that would replace a certain weapon in levels with yours, for instance with this mutator on all the Lightning Guns in a map would be replaced with your weapon. This can also be used to make an Arena mutator where all weapons are replaced with yours.
MyWeapon - The main script for your weapon, this tells it what fire modes, pickups and attachment classes to use, and how the weapon will look in first person mode.



MyWeaponAttach - Used to tell what our weapon looks like in third person mode (bots and other players holding it).



MyWeaponPickup - Tells our weapon what it looks like when it is sitting on a weapon base or dropped by a player. This is different from the Weapon Attachment class because it uses a Static Mesh, usually lower poly, and it is non-animated. This is also the class we would use to summon our weapon during gameplay for testing, by typing summon MyWeapon.MyWeaponPickup in the console, MyWeapon being the main folder name.



MyWeaponFire - This is where we tell our weapon how to fire, what type of projectile/instant hit to use, how much ammo to use, etc.
MyWeaponAmmo - This is where we tell our weapon how much ammo to start with, the maximum amount we can have, and what pickup class to use.
MyWeaponAmmoPickup - Tells our ammo pickup what it looks like and how much ammo to give the player.



MyWeaponProjectile - For projectile weapons, this tells our projectile what it looks like and how to act, as well as the amount of damage to deal to players.



DamTypeMyWeapon - This is used for the death and suicide messages.

Weapon Coding

First thing we have to do is decide what we want to make. For this basic tutorial we will do something easy. We're going to make a Link Gun that shoots rockets, and call it the "UberLinkGun". Why in the world would we want to do this? Nothing wrong with a little mindless spam once in awhile. Create a folder in the UT2003 folder called UberLinkGun and make a folder inside it called Classes.

Let's start with the main weapon script, MyWeapon, which in this case we will call UberLinkGun. It doesn't necessarily have to have the same name as the folder it's in, the folder just indicates what the final .u file will be named. Since we're just modifying the script for a LinkGun, let's copy over the LinkGun script from the XWeapons folder. Try not to accidentally save over the code we downloaded, but you can always unzip them again if something goes wrong. Copy the LinkGun.uc file into our UberLinkGun/Classes folder, and open it with NotePad and take a look. There's a lot of code in there, but it all just provides the basic function of the LinkGun.

Here's where I have to explain a little bit about how code works in Unreal. Since we never want to directly edit classes that came with the game, we use subclassing to make all of our scripts, which is kind of like making a folder inside a folder. Classes are based on 'inheritance', meaning that when we make our subclass of the LinkGun, all of the code that you see in the LinkGun script is 'inherited' by our subclass. The only things that would change would be things we copy into our new code and change. We can override functions of the parent class in order to do things differently in our subclass. This will come in handy as you get more into coding, but for now all we have to worry about is the first few lines at the top and a few of the items in the DefaultProperties{} block.

Let's take a look at the items we will be changing:

Weapon Class

//=================================================================  
// Link Gun
//=================================================================
class LinkGun extends Weapon
    config(user);

defaultproperties
{
    FireModeClass(0)=Class'XWeapons.LinkAltFire'
    FireModeClass(1)=Class'XWeapons.LinkFire'
    PickupClass=Class'XWeapons.LinkGunPickup'
    AttachmentClass=Class'XWeapons.LinkAttachment'
}

The first three lines are a standard comment block, anything after // on a line isn't read by our code, it's just there for us to put comments so that other coders can figure out what we are doing. So for mine, I would put:

//=================================================================  
// UberLinkGun - Rachel "Angel Mapper" Cordone's uber-linkgun.
//=================================================================

Since we are subclassing the LinkGun, we will want to tell the script that in the next line:

class UberLinkGun extends LinkGun  
    config(user);

Everything between those lines and the DefaultProperties line can be deleted, since we will not be changing any of it and will let Inheritance take care of it.

Now for the Default Properties. If you look, you will notice that all four of the lines we are changing are the connections noted in the pic at the top of this page. Since for now we will only have one fire mode, we will set both of them the same. The format these use is Class'Package.Name', so in our case, we would change these lines to our own weapon scripts:

defaultproperties
{
    FireModeClass(0)=Class'UberLinkGun.UberLinkGunFire'
    FireModeClass(1)=Class'UberLinkGun.UberLinkGunFire'
    PickupClass=Class'UberLinkGun.UberLinkGunPickup'
    AttachmentClass=Class'UberLinkGun.UberLinkGunAttach'  
}

That's it! This isn't as difficult as you thought is it? Be sure to rename the file to exactly as it appears in the "class UberLinkGun extends LinkGun"  line, so in this case, UberLinkGun.uc. This is true for all of the scripts that we make.

Weapon Attachment Class

Now let's take care of the Attachment and Pickup classes for our weapon. Copy the LinkAttachment and LinkGunPickup scripts into our folder, and let's see what we need to change. For our Attachment class, most of the script is used for the LinkGun muzzle flash For now we'll leave it as it is, but we'll want to change it later. Other than that we don't need to change anything about it, so all we need to do is write our attachment class like this.

class UberLinkGunAttach extends LinkAttachment;  

defaultproperties
{
}

Remembering inheritance, all of the code in the LinkAttachment class will be used by our weapon, since we haven't overridden any of the functions or changed any default properties.

Weapon Pickup Class

Now let's take a look at our pickup class. Copy the LinkGunPickup script into our folder and take a look. We have to change a couple of default properties here, to let the pickup know what weapon inventory to give us, and to change the message we get when we pick it up.

//=================================================================  
// UberLinkGunPickup.
//=================================================================
class UberLinkGunPickup extends LinkGunPickup;

defaultproperties
{
    InventoryType=Class'UberLinkGun.UberLinkGun'
    PickupMessage="You got Rachel's Uber-LinkGun."
}

The pickup class isn't the actual weapon itself, it's merely the static mesh spinning on the weapon base, which is why the LinkGunPickup class is pretty small, just a few texture effects. When a player runs over the pickup, it gives you whatever is set in the InventoryType line. If we really wanted to we could have a LinkGun pickup give you a rocket launcher, so there's a lot of flexibility to what you can do.

Weapon Fire Class

For our weapon fire class, copy the LinkAltFire script into our folder (It's actually the primary fire, for some reason the scripts are backwards in the Epic code for the LinkGun). Not a lot we have to change here, but we will have to override a function, so let's take a look:

class UberLinkGunFire extends LinkAltFire;  

Now if you look closely at the rest of the script, you will see a SpawnProjectile function. We will be using this to make our rockets appear, so let's keep that part of the script and reference our weapon in it:

function Projectile SpawnProjectile(Vector Start, Rotator Dir)
{
    local LinkProjectile Proj;

    Start += Vector(Dir) * 10.0 * LinkGun(Weapon).Links;
    Proj = Spawn(class'XWeapons.LinkProjectile',,, Start, Dir);  
    if ( Proj != None )
    {
        Proj.Links = LinkGun(Weapon).Links;
        Proj.LinkAdjust();
    }
    return Proj;
}

First thing we have to change is the LinkProjectile references. Those are what tell the weapon to spawn Link projectiles, which isn't what we want. Since we're making our own projectile class, we'll reference that. Also, the lines with LinkGun(Weapon).Links refers to a variable in the normal LinkGun used for linking up with people. We don't want that either, we can just delete it. Let's see what our final script looks like for that function:

function Projectile SpawnProjectile(Vector Start, Rotator Dir)
{
    local UberLinkGunProj Proj;

    Proj = Spawn(class'UberLinkGun.UberLinkGunProj',,, Start, Dir);  
    return Proj;
}

Now we can take care of the Default Properties of the UberLinkgunFire class that we need to change.

defaultproperties
{
    AmmoClass=Class'XWeapons.RocketAmmo'
    ProjectileClass=Class'UberLinkGun.UberLinkGunProj'  
}

Since we're just using rockets, we can go ahead and use the RocketAmmo class for our weapon, now we don't need to worry about that entire branch of the weapon code. Only two more to go, the Projectile class and our Damage Type!

Projectile Class

Since we decided not to change the AmmoPickup class, that branch from WeaponFire is done. Now we need to take care of our Projectile Class. Copy the RocketProj script into our folder and open it up. A lot of the code you will see is for the rockets "flocking", when you shoot them with the rocket launcher and they curl around each other. This won't really harm our script to leave it in, so all we have to change are the first line and one of the DefaultProperties, the rest can be deleted.

class UberLinkGunProj extends RocketProj;

defaultproperties
{
    MyDamageType=Class'UberLinkGun.DamTypeUberLinkGun'  
}

Damage Type Class

One more to go, and this one's easy! Copy over the DamTypeRocket script into our folder and open it up. The GetHitEffects function is what spawns the fire on people when they get hit, so we'll leave that there. By now it should be obvious what we need to change, so let's do it.

class DamTypeUberLinkGun extends DamTypeRocket;

defaultproperties
{
    WeaponClass=Class'UberLinkGun.UberLinkGun'
    DeathString="%o got smacked down by %k's UberLinkGun."  
    FemaleSuicide="%o Uber'd herself."
    MaleSuicide="%o Uber'd himself."
}

Congratulations, we're done with the scripting part! In the last part of this tutorial we will compile and test out our script. Almost done!

Continue to Part Three of this tutorial