How to manually reload

From EDukeWiki
Revision as of 06:10, 1 March 2011 by EmericaSkater (talk | contribs)
Jump to navigation Jump to search

This tutorial will explain how, by accessing a few members of the player structure and manipulating some game events, the player can be given an option to manually reload the pistol, rather than shooting till the clip's empty.

First, we'll declare the necessary gamevars...

gamevar PISTOLAMMO 0 1 
gamevar KICKBACK 0 1
gamevar PWEAPON 0 1 
gamevar PISTOLMAGAZINE 0 1
gamevar PERPLAYER1 0 1
gamevar WEAPON1_CLIP 0 0 // this last gamevar disables the game's automatic reloading

then, under the APLAYER line in the game.con, we'll store our static variables that will be used, and code in a few functions that the reload is supposed to perform...

actor APLAYER MAXPLAYERHEALTH PSTAND 0 0
  getplayer[THISACTOR].curr_weapon PWEAPON // the player's current weapon
  getplayer[THISACTOR].kickback_pic KICKBACK // the frame of animation the player's weapon is at
  getplayer[THISACTOR].ammo_amount 1 PISTOLAMMO // the player's pistol ammo
  
  ifvare PWEAPON 1 // if the player has the pistol selected...
  {
    ifvarl PISTOLMAGAZINE 1 // and his magazine count is at zero...
    {
      getplayer[THISACTOR].weapon_pos PERPLAYER1
      
      ifvarn PERPLAYER1 0
        break // if he has a weapon that's lowering off screen, break
      else ifvarl PISTOLAMMO 1
        break
      else // otherwise, if he has ammo for the pistol, start the reloading anim
      {
        setplayer[THISACTOR].reloading 1
        setplayer[THISACTOR].kickback_pic 4
      } }
      
      ifvare KICKBACK 5 // if the reloading animation is on its second frame...
      {
        ifvarg PISTOLAMMO 11 // if the player has at least twelve pistol rounds left,
          setvar PISTOLMAGAZINE 12 // then set his magazine counter to a full magazine
        else
          setvarvar PISTOLMAGAZINE PISTOLAMMO // otherwise set the magazine counter equal to his remaining ammo
      }
    }
  

Next we'll access the DOFIRE event, which is called if/when a weapon fires its projectile...

onevent EVENT_DOFIRE 

  ifvare PWEAPON 1 // if the player has the pistol selected when he fires a projectile...
    ifvarl PISTOLMAGAZINE 1
      break
    else
      subvar PISTOLMAGAZINE 1 // subtract 1 from the magazine count, unless it's already at zero

endevent

In order to perform a reload, you'll need to assign it a key, which means booting out one of the game's more useless functions. In this example, it'll be the TurnAround key...

definegamefuncname 36 Reload // rename it, so it'll appear as Reload in the control settings menu

onevent EVENT_TURNAROUND 

  setvar RETURN -1 // disable the event
  ifvare PWEAPON 1 // if the player has the pistol armed
  {
    ifvare PISTOLMAGAZINE 12 // and his magazine is full, break
      break
    else ifvarvare PISTOLMAGAZINE PISTOLAMMO // if his magazine is equal to his pistol ammo, break
      break
    else // otherwise, start the reloading sequence
    {
      setplayer[THISACTOR].reloading 1
      setplayer[THISACTOR].kickback_pic 4
    }
  }

endevent

Because the game's original reloading system is disabled, the player won't load in a clip when the game starts up. We'll take care of that, as well as another potential glitch, by accessing the RESETPLAYER event.

onevent EVENT_RESETPLAYER

  setvar PISTOLMAGAZINE 0 // if the player was reset to his starting position (i.e, he died), reset the clip amount to zero.
  
  ifvare PWEAPON 1 // if he has the pistol selected when he loads into the map...
  {
    setplayer[THISACTOR].reloading 1 
    setplayer[THISACTOR].kickback_pic 4 // start the reloading sequence.
  }

endevent

The code that was under the APLAYER line made sure the player will automatically reload the pistol if his magazine's empty (and of course, he has the ammunition to do so), however he'll still shoot the projectile even if the magazine's empty, unless the firing key is disabled appropriately...

onevent EVENT_PRESSEDFIRE // when the player presses the fire button...

  ifvare PWEAPON 1 // and he has the pistol armed...
    ifvarl PISTOLMAGAZINE 1
      setvar RETURN -1 // if his magazine is at zero, disable the firing key

endevent

And lastly, we're going to want the player to know what his magazine count's at, so we'll put a small counter next to the ammo amount...

onevent EVENT_DISPLAYREST

  digitalnumberz 2930 280 175 PISTOLMAGAZINE 0 0 0 0 0 xdim ydim 50000 // display the magazine count at a slightly smaller size (50000) next to the ammo

endevent