I don't need to test my programs. I have an error-correcting modem.


From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

Fairly Simple Reloadable Weapons

UT2004 :: Actor >> ReloadableWeaponsBase


I am working on a modification for UT2004 that required reloadable weapons, so I sat down and I coded a really simple version of reloadable weapons. I don't have any animations atm, so I didn't bother coding in animation support or sound support, but I will do that later if need be. Also, this is only really useful for new gametypes or total conversions as you need a new hud class to show the player how many bullets/clips he has left to use. Also, this script is NOT tested online but it should work fine.

Also... please note that to reload your weapon, you will have to call 'reloadme' in the console. For gametypes and mods, you should make a custom bind for this.

The Script

class ReloadableWeaponsBase extends Weapon;
/* Note: The Reloading features of this custom class will only affect FireMode(0) or Primary Fire
   As most weapons will only have one fire that will use ammo */
//New Reloadable Weapon System
//Basic Reload Vars
var bool bReloading; //True if the weapon has commenced reloading
var int iReloadWait; //How much time to wait before each reload is done? SET THIS ONLY IN YOUR CUSTOM WEAPON CLASS
var int iTimeToReload; //Internally Set... IGNORE
//Clips Reload Vars
var bool bClipReload; //Set to true in weapon class if you want the gun to reload like an assault rifle
var int iNumberOfClips; //How many clips does this gun have? SET IN WEAPON CLASS
//Single Reload Vars
var bool bSingleReload; //Set to true in weapon class if you want the gun to reload like a shotgun
var int iNumberOfBullets; //How many bullets does this gun have left? SET IN WEAPON CLASS
exec function ReloadMe()
     iTimeToReload=iReloadWait; //reset the amount of time to wait before you have completely reloaded
     bReloading=True; //reloading has commenced
     SetTimer(1.0, True);
function Timer()
             if(bReloading) //Reloading
                if(iTimeToReload != 0)
                iTimeToReload--;  //decrement the wait time
                if(iTimeToReload == 0) //when iTimeToReload is 0, reload the weapon as long as...
                      if(iNumberOfClips != 0) //You still have clips left
                                        if ( AmmoClass[0] != None )
			                AmmoCharge[0] = MaxAmmo(0); //max out the ammo of your primary fire
                                      iNumberOfClips--; //Decrement the number of clips we have as we used one up!
                      bReloading=False; //No longer reloading
            if(bReloading) //Reloading
                         if(iTimeToReload != 0)
                         iTimeToReload--;  //decrement time to reload
                          if(iTimeToReload == 0) //if time to reload is 0 (you don't have to wait anymore)
                            if(iNumberOfBullets != 0) //and if you still have bullets left
                              AddAmmo(1,0);   //give gun 1 ammo and...
                              iNumberOfBullets--; //take away one bullet from the belt
                          if(AmmoMaxed(0)) //if you can't put anymore more bullets in... stop reloading
                                iTimeToReload = iReloadWait;

zugy: Nice. We happen to have reloadable weapons for our mod, HaloUT. It looks pretty similar to this. I think the class was called Haloweapon though...either way...It has the ability to stop firing when reloading as well...Basically it checks if the gun is currently reloading in the allowfire function and if it is, returns false...

MythOpus: Well, in my mod, it's called Polarity_WeaponBase hehe... I just changed the name so it could be bit more descriptive. I tried to use the return value of allowfire (without overiding the function) by using it in an if statement but it didn't work. I will have to probably do what you did with your weapons then. Do you have animations that reload yet?

zugy: Would have answered a but sooner, but I had to wait for a temp ban to get lifted...Anyhow...We have a subclass of weaponfire called HaloFire, and we do have reload animations...our allowfire function checks if the weapon is reloading by accessing a varible in the haloweapon class and if the gun is reloading it returns false.

MythOpus: I see. Well, I don't want to complicate things by making a custom WeaponFire unless I absolutely have to... and perhaps I will absolutely have to :| It works good the way it does now though. When I add the stuff I need to add for sounds and animations, I'll most likely update this page. It sucks that you get temporailly banned for reverting sites with lots of links :(

zugy: It isn't really all that much more complicated; all you do is the check if reloading and rely on the parent class for the rest...

Solid Snake: Hmmm, simplistic, but I dont think you've got replication support.

MythOpus: I hate replication, so I decided I would do all that when I have a complete working beta, but I didn't think I really needed replication as everything is client side anyways. All that the server needs to know is that there were shots fired, which it already does.

Solid Snake: Not a good approach towards implementing replication. Its often much harder to code an entire project and then 'add' in replication which is not what you do. Often, it is much better to code with replication in mind rather than as an after thought. It shouldn't be client side, as that poses a lot of problems? Who regulates when the client needs to reload, who regulates how many bullets are in the magazine/clip, who regulates when a client can shoot and if the client shoot will it remove ammo in the magazine/clip? The client? ... I sure hope not. This isn't so much as an attack on your code example up there, but well, those are some things to consider first as more of a priority goal rather than as an after thought. That depends if your coding a MP or a SP project that is, as obviously with SP projects, replication isn't even a consideration.

MythOpus: Attacks on my code are wanted :D Gives me insight. Anyways, here's why I don't think replication is very neccesary. I'm using functions to add the ammo from the clips from the weapon base class, which means it should all be properly replicated and what not. This new weapon basically works just like a normal weapon would, except that the player has the ability to load more ammo into it. The player can reload at any time. When the player has 0 ammo left, the gun doesn't work until the player calls the reloading function (via a bind or console). The gun doesn't work when it's ammo reaches 0 anyways, but the only difference is it won't switch to a weapon with ammo in it (at least not the way I have it set up). The amount of clips left are all held in the current weapon and I didn't think the server needs to know about how many clips are left. 'Lag' only affects the server-client connection and if you're talking about variables that are just client side, like the ClipsLeft variable, the server doesn't need to deal with them because they don't really affect anything. The server just needs to know that, "Hey, that weapon doesn't have any ammo anymore so it can't fire!" and "Hey, that weapon has ammo now so he can fire."

Unless I'm completely wrong about this :|

Any about the coding in replication later, I think I have replication in mind. I mean, I make doubles of everything to be compared to in my code so all I have to do is replicate the doubles... right?

Solid Snake:Don't really know what you mean by doubling your code and replicating doubles...

MythOpus: Haha. Okay, here's an example then. Instead of:

var int AnInt;
AnInt = 5;

Do this...

var int AnInt,AnotherInt;
replication blah blah
AnInt = AnotherInt;

Haha :S

Wormbo: Uhm, what's the point of that? Generally the server should dictate, what's going on. Especially something as important as ammunition management should not be delegated to the client. Clients might be allowed to simulate the server's behavior, but the server still has the final word about whether the weapon may fire or not. A popular example, how client side fire control can go wrong is the UT ZeroPing InstaGib mod. In an earlier version the server didn't check the firing rate and a so-called "mass-murder" cheat was created, where the client would immediately kill everyone in sight.

MythOpus: Laughs to himself loudly. Okay... well... for replication I thought you couldn't replicate variables that are being used by the code. Instead, I thoguht you had to make another variable that would be replicated and you would set another variable that you would want to be replicated to the variable that was being replicated....

Anyways... Can you show me how I would do this clip/bullet left replication then?

zugy: Well I don't understand much about replication, but this was in our weapons class, maybe it'll give you some ideas...

    reliable if(Role == ROLE_Authority)
    reliable if(Role < ROLE_Authority)
        ReloadMeNow, FinishReloading;
    reliable if(Role == ROLE_Authority)
        ClientReload, ClientFinishReloading, ClientReloadEffects;

Brian_The_Black: And here is my quick and dirty method for reloading weapons:

class ReloadableWepFire extends WeaponFire;
var()	float	ReloadTime;
var	int	ShotsFired;
var()	int	ClipSize;
event ModeDoFire()
	if (ShotsFired<=(ClipSize - 2))
		  if (FireRate==ReloadTime)
		  ShotsFired = (ShotsFired+1);
		  ShotsFired = 0;

The animation and sounds are done in the weapon:

class ReloadableWeapon extends Weapon
var() Name ReloadAnim;
var() Name ReloadDownAnim;
var() Name ReloadUpAnim;
var() Sound ReloadSound;
var   Float SepReloadAnimRate;
simulated function Reload(float ReloadTime)
	if ( HasAnim(ReloadAnim) )
		PlayAnim(ReloadAnim, ReloadTime, 0.0);
		  SepReloadAnimRate = (ReloadTime-2);
		  PlaySound(ReloadSound, SLOT_None, 1.0);
		  PlayAnim(ReloadDownAnim, 0.5, 0.0);
		  SetTimer(SepReloadAnimRate, false);
		  PlayAnim(ReloadUpAnim, 0.5,0.0);

I have just made this today, my only problem with it is that the timer dosent seem to work. What is it's problem?

Oh yea, and I need to sort the HUD out to display the ammo (but I shouldnt have a problem with this)

Anyway, this is part of a project I have been doing in making a uber rocket launcher which has several intresting features:

1. Scope, with target tracking (possible targets have a white box drawn around them)

2. Homming missiles, missile launcher automaticaly locks onto target closest to the cross hairs (the current locked target has a red box instead of white)

3. Once missiles are locked on to targets, they follow them with reasonable agility, even if the missile launcher has selected a new target (or gone out of target locking range, or controller died, or target is no longer visible)

4. To make this weapon fairer, it fires 5 missiles and then has to reload.

Anyway, I have almost finished it, just need to make a nice mesh for it.... problem is I cant find a converter for lightwave 9 files that works!