| Home Page | Recent Changes | Preferences

# MythOpus/ReloadingWeaponBindAnim

Okay... so this is code I need help with. (the reloading code and reload bind is by whoever made the Reloading Weapons page. Anyways... I've made my gun that shoots and has a need to reload. To reload, I first made an alt fire and made it reload that way... but i wanted a secondary fire mode so I made the reload bind instead. It's in the game and it works. When you reload with the alt-fire however it plays the reload anim but with the ReloadBind, it reloads but without an anim.... can someone help me ?

    00001   class ReloadingWeapon extends Weapon
    00002      abstract;
    00003
    00004  var() int ClipCount; //What's that you say? Clip is not a technically correct term? Do I care?
    00005  var() float ReloadRate; //Time it takes to insert one bullet.
    00006  var float ReloadTimer;
    00007
    00008  var() sound ReloadBeginSound, ReloadSound, ReloadEndSound; //Sounds played when start to reload, on insertion of each bullet, and when reloading has ended.
    00009  var() name ReloadAnim; //Animation to play when reloading is started.
    00010  var() float ReloadAnimRate;
    00011
    00012  var bool bIsReloading, bReloadEffectDone;
    00013
    00014  replication
    00015  {
    00016      reliable if(Role == ROLE_Authority)
    00017          ClipCount;
    00018
    00019      //Functions called on the server from the client.
    00020      reliable if(Role < ROLE_Authority)
    00021          ReloadMeNow, FinishReloading;
    00022
    00023      //Functions called on the client from the server.
    00024      reliable if(Role == ROLE_Authority)
    00025          ClientReload, ClientFinishReloading, ClientReloadEffects;
    00026  }
    00027
    00028  //So reloading can be bound to a key. Exec functions in weapons can only be called for the currently held weapon, which is perfect for this purpose.
    00029  exec function ReloadMeNow()
    00030  {
    00031      if(!AllowReload())
    00032          return;
    00033
    00034      bIsReloading = true;
    00035      ReloadTimer = Level.TimeSeconds;
    00036      PlaySound(ReloadBeginSound, SLOT_Misc, TransientSoundVolume,,,, false);
    00037      ClientReload();
    00038  }
    00039
    00040  //Called on the client when reloading starts.
    00041  simulated function ClientReload()
    00042  {
    00043      bIsReloading = true;
    00044      PlayAnim(ReloadAnim, ReloadAnimRate, 0.1);
    00045  }
    00046
    00047  //For effects during reloading, like smoke or shells ejected from the breech.
    00048  simulated function ClientReloadEffects(){}
    00049
    00050  //Reloading ends when the key is released.
    00051  exec function FinishReloading()
    00052  {
    00053      if(!bIsReloading)
    00054          return;
    00055
    00056      PlaySound(ReloadEndSound, SLOT_Misc, TransientSoundVolume,,,, false);
    00057      ClientFinishReloading();
    00058      bIsReloading = false;
    00059      bReloadEffectDone = false;
    00060  }
    00061
    00062  //Called on the client when reloading ends.
    00063  simulated function ClientFinishReloading()
    00064  {
    00065      bIsReloading = false;
    00066      PlayIdle();
    00067
    00068      if(Instigator.PendingWeapon != None && Instigator.PendingWeapon != self)
    00069          Instigator.Controller.ClientSwitchToBestWeapon();
    00070  }
    00071
    00072  function bool AllowReload()
    00073  {
    00074      //Can't reload whilst firing.
    00075      if(FireMode[0].IsFiring() || FireMode[1].IsFiring())
    00076          return false;
    00077      //Can't reload if already reloading.
    00078      else if(bIsReloading)
    00079          return false;
    00080      //Can't reload if already full.
    00081      else if(ClipCount >= Default.ClipCount)
    00082          return false;
    00083      //Can't reload if not enough ammo.
    00084      else if(Ammo[0] == None || Ammo[0].AmmoAmount <= ClipCount)
    00085          return false;
    00086
    00087      return true;
    00088  }
    00089
    00090  //Don't allow weapon switching whilst reloading.
    00091  simulated function bool PutDown()
    00092  {
    00093      if(bIsReloading)
    00094          return false;
    00095
    00096      return Super.PutDown();
    00097  }
    00098
    00099  simulated function BringUp(optional Weapon PrevWeapon)
    00100  {
    00101      Super.BringUp(PrevWeapon);
    00102      bIsReloading = false;
    00103  }
    00104
    00105  //Reduce ClipCount every time a bullet is fired.
    00106  function ConsumeAmmo(int Mode, float load)
    00107  {
    00108      if(Ammo[Mode] != None)
    00109      {
    00110          if(Ammo[Mode].UseAmmo(int(load)) && load > 0)
    00111              ClipCount--;
    00112      }
    00113  }
    00114
    00115  simulated function bool HasAmmo()
    00116  {
    00117      //Ignore FireMode[1] which doesn't use any ammo.
    00118      return (Ammo[0] != None && FireMode[0] != None && Ammo[0].AmmoAmount >= FireMode[0].AmmoPerFire);
    00119  }
    00120
    00121  event WeaponTick(float dt)
    00122  {
    00123      if(!bIsReloading)
    00124      {
    00125          //Stupid bots like to run around with an empty weapon, so force them to reload when appropriate.
    00126          if(!Instigator.IsHumanControlled())
    00127          {
    00128              if(Level.TimeSeconds - Instigator.Controller.LastSeenTime > ClipCount)
    00129                  ReloadMeNow();
    00130          }
    00131      }
    00132      else
    00133      {
    00134          //Add one bullet at a time as long as reloading key is held down.
    00135          if(Level.TimeSeconds - ReloadTimer >= ReloadRate)
    00136          {
    00137              if(ClipCount >= Default.ClipCount) //Full.
    00138              {
    00139                  ClipCount = Default.ClipCount;
    00140                  FinishReloading();
    00141              }
    00142              else if(Ammo[0].AmmoAmount <= ClipCount) //Out of ammo.
    00143              {
    00144                  ClipCount = Ammo[0].AmmoAmount;
    00145                  FinishReloading();
    00146              }
    00147              else //Add another bullet.
    00148              {
    00149                  PlaySound(ReloadSound, SLOT_Misc, TransientSoundVolume,,,, false);
    00150                  InsertBullet();
    00151
    00152                  if(ClipCount >= Default.ClipCount)
    00153                  {
    00154                      ReloadTimer = Level.TimeSeconds - (ReloadRate / 2);
    00155                  }
    00156                  else
    00157                      ReloadTimer = Level.TimeSeconds;
    00158              }
    00159          }
    00160          else if(!bReloadEffectDone && Level.TimeSeconds - ReloadTimer >= ReloadRate / 2)
    00161          {
    00162              bReloadEffectDone = true;
    00163              ClientReloadEffects();
    00164          }
    00165      }
    00166  }
    00167
    00168  //Can play animations for inserting individual bullets here, or other effects. Server-side only, so should call a replicated function here and play animations in it, rather than in this function itself.
    00169  function InsertBullet()
    00170  {
    00171      ClipCount = Default.ClipCount;
    00172  }
    00173
    00174  //Show how many bullets left until reload.
    00175  simulated function float ChargeBar()
    00176  {
    00177      local float CurrentClip, MaxClip;
    00178
    00179      //Store the int values as floats to avoid rounding errors.
    00180      CurrentClip = ClipCount;
    00181      MaxClip = Default.ClipCount;
    00182
    00183      return FMin(1, CurrentClip/MaxClip);
    00184  }
    00185
    00186  function byte BestMode()
    00187  {
    00188      if(ClipCount > 0)
    00189          return 0;
    00190
    00191      return 1;
    00192  }
    00193
    00194  defaultproperties
    00195  {
    00196      ClipCount=30
    00197      ReloadRate=1.0
    00198      ReloadAnim=Reload
    00199      ReloadAnimRate=1.0
    00200
    00201      bShowChargingBar=True
    00202  }

And thne this is the Bind code...

    class ReloadBinding extends GUIUserKeyBinding;

    defaultproperties
    {
        KeyData(0)=(KeyLabel="Reloading Weapon",bIsSection=True)
        KeyData(1)=(Alias="ReloadMeNow | OnRelease FinishReloading",KeyLabel="Reload")
    }

Foxpaw: It looks like it should work, though the netcode looks a bit flaky. That exec function is going to run on the client, get replicated to the server, then run the clientreload stuff. A few milliseconds later the server will have sent back the results from IT's replicated "reloadmenow" and you will have the client animations attempt to play twice. I wouldn't think that that would cause a problem though in botplay.

MythOpus: Well i have noticed that the relaod anim plays far too often. After you run out of ammo, it plays it, when you click on the reloading secondfire button and when you release it. It also does it when you stop firing.

MythOpus: I still haven't figured tihs out and its bugging me so much lol...

Mr Evil: Hmmm, I'm not sure what's going wrong. That code works perfectly well where I'm using it. If you wait a while you will be able to see the code in use in the LawDogs mod - maybe a 'real' example will help you solve the problem. FoxPaw: The exec function is only run on the server, then ClientReload is run only on the client. Check it with some logs if you want to see for yourself.

MythOpus: Hmmm, yes, I shall be waiting....

Foxpaw: Yowza, I thought exec functions were automatically simulated as well.

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In