A simple tutorial: Rifle clips

HakujinHakujin Join Date: 2003-05-09 Member: 16157Members, Constellation
edited April 2010 in Modding
<div class="IPBDescription">from a poor Rubyist</div>Hey guys,

My simple goal for a baby-step mod is to convert the ammo system into a clip based one, rather than a pool of ammo. I am by no means a LUA expert - I just want to show people how easy this bad boy is to mod. So the two files I am adjusting are:

Player.lua
Rifle.lua

I have decided that I want players themselves to have a clip count, rather than lump the ammo with the weapon. So if players pick up a weapon, it doesn't magically have ammo. Also, at this point clips are simply discarded for ease of programming.

So, the first step was to add clips to the Player class. I added the following in Player.lua:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Player.networkVars =
    {      
        numRifleClips                = "integer (0 to 10)"  // Instantiate and track our rifle clips - starting at 10 with a max of 10
    }<!--c2--></div><!--ec2-->

And added accessor methods:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// let the Rifle class ask the Player how many clips he has
function Player:GetNumRifleClips()
    return self.numRifleClips
end

// let something else tell the Player how many clips he has now (also used for resupply)
function Player:SetNumRifleClips(int)
    self.numRifleClips = int
end<!--c2--></div><!--ec2-->

Then I went into Rifle.lua to tweak the Reload function and print out clips remaining:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function Rifle:Reload(player)

    // ask the player how many clips he has
    local clips = player:GetNumRifleClips()
    
    // do we have clips AND is the clip not currently full?
    if (clips > 0 and self.numBulletsInClip ~= self.clipSize) then
    
        local viewModel = player:GetViewModelEntity()
        
        viewModel:SetAnimation( "reload" )
        player:PlaySound( self.reloadSound )
        
        self.numBulletsInClip = math.min(self.numBulletsInReserve, self.clipSize)
        
       //update the player's clips count
        player:SetNumRifleClips(clips -1)
        
       // tell him how many clips he has left
        Server.Broadcast(player, ("RIFLE CLIPS REMAINING: "..tostring(clips - 1)))
        
        return true
        
    end
    
    return false

end<!--c2--></div><!--ec2-->

Comments

  • blmarketblmarket Join Date: 2010-01-17 Member: 70192Members
    good article for lua newbie like me XD
  • PsYcH0_Ch!cKeNPsYcH0_Ch!cKeN Join Date: 2009-12-11 Member: 69601Members
    A good start.

    To make it complete though, you should create an array to hold the ammo counts of each magazine, then have a reload call the next value in the array. That way you don't lose the ammo you have, it just goes to the back of the queue. If you completely empty your weapon, then remove that element from the array.

    That way, you can also implement +1 reloading, by checking if you have any rounds remaining and if so, carry one over to the new magazine, setting the max bullets in the weapon to 31 (one in the chamber).
  • RobBRobB TUBES OF THE INTERWEB Join Date: 2003-08-11 Member: 19423Members, Constellation, Reinforced - Shadow
    Info: If the lever on the side of the rifle is pulled, the chambered round is ejected, effectively removing one shot.
  • GDWhiteGDWhite Join Date: 2009-07-17 Member: 68170Members
    I was an addict like you once.
    But I was intervened. By this guy.
    <span style='color:#000000;background:#000000'>trollface.jpg</span>
    There's a difference between clips and magazines! I KNOW RIGHT!
    Where were our beloved game developers when this crucial bit of information slipped through the cracks in the transition from CoD 3 to Modern Warfare? I was no longer plugging clips! I was cramming mags!
    I, for one, have been fighting my addiction ever since, but this photo brings me hope.

    <img src="http://1.bp.blogspot.com/_RYwUPr35rLw/SMSzcYalaPI/AAAAAAAAAM4/pgTUU7jUlFo/s1600/Magazine+-+vs+-+Clip.jpg" border="0" class="linked-image" />
  • HakujinHakujin Join Date: 2003-05-09 Member: 16157Members, Constellation
    I support your efforts, but in the game code they are clips! Shorter to type and already throughout the code!
  • GDWhiteGDWhite Join Date: 2009-07-17 Member: 68170Members
    I love you, and your knowledge.
  • PsYcH0_Ch!cKeNPsYcH0_Ch!cKeN Join Date: 2009-12-11 Member: 69601Members
    edited April 2010
    Hey, I used "magazine" throughout my post! I know the difference* too! :o


    <!--sizeo:1--><span style="font-size:8pt;line-height:100%"><!--/sizeo-->*I program on a realism mod for another game and our community would crucify me if I made a mistake like that :(<!--sizec--></span><!--/sizec-->
  • TrueTrue Join Date: 2004-03-16 Member: 27361Members
    edited May 2010
    I think the code should be changed then to say 'mag' or 'magazine.' The model in use is absolutely using magazines.

    On that note, why is the charging handle so small on the rifle? I have no idea how that thing is supposed to be gripped. And considering the location of the ejection port, why does the charging handle move? Based on its location, I would imagine it would pull the bolt but wouldn't be connected to it. (The rifle is powered - why not just have a button to load a round? Have small arms not progressed this far yet?)
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    Myself I prefer when it is not mags (plural) in the game mechanics, and more like 1 mag in the gun, and a pool of bullets outside of it. Press reload and it fills the mag with bullets from the pool and stick it into the gun again. Think cs, halo, ns1 and others.

    Also, just a question.
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Player.networkVars =
        {      
            numRifleClips                = "integer (0 to 10)"  // Instantiate and track our rifle clips - starting at 10 with a max of 10
        }<!--c2--></div><!--ec2-->
    is that same as:
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Player.networkVars.numRifleClips = "integer (0 to 10)"  // Instantiate and track our rifle clips - starting at 10 with a max of 10<!--c2--></div><!--ec2-->
    I might just be way tired and confused though. Speaking of which gnight :P.
  • TrueTrue Join Date: 2004-03-16 Member: 27361Members
    It's a realism thing. I prefer the realism part although I understand NS isn't necessarily about realism.

    I shoot at a minimum every weekend. If I could just pool all my ammo in a pile, remove my magazine from my weapon, stick it back in and be good to go, I'd be extremely rich - as I'd have some invention on my hands to reload a mag in sub-seconds that could make me some money. And I would have more time to shoot than to reload mags :)

    Maybe the marines have an ammo dispenser on their persons that we just can't see...
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    Yeah, 2 mags, one they currently use, and one that withing seconds is filled with new round when they stick it into the holder.

    One is out of ammo, they stick it in the dispenser (a "hole" in the suit that the mag fits in), take out the second, stick into gun, fire for 5 seconds, out of ammo again, repeat.
  • FragmagnetFragmagnet Join Date: 2010-07-26 Member: 72873Members
    <!--quoteo(post=1767076:date=Apr 15 2010, 11:45 AM:name=GDWhite)--><div class='quotetop'>QUOTE (GDWhite @ Apr 15 2010, 11:45 AM) <a href="index.php?act=findpost&pid=1767076"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec--><img src="http://1.bp.blogspot.com/_RYwUPr35rLw/SMSzcYalaPI/AAAAAAAAAM4/pgTUU7jUlFo/s1600/Magazine%20-%20vs%20-%20Clip.jpg" border="0" class="linked-image" /><!--QuoteEnd--></div><!--QuoteEEnd-->


    mmmmmmmmm stripper :3

    ...What..... it was bound to happen.....

    uhhh..... yeah....

    nothing really.... productive in this post so uhh.... yeah I'm just gunna click add reply now.
  • mu.mu. Join Date: 2007-12-27 Member: 63276Members, Constellation
    edited July 2010
    realism is nice. especially in insurgency. and hostile intent.

    i like that you can add this and play on a modded server with friends for the novelty, but i wouldn't enjoy it.

    for one thing, it's not like you also added tactical and speed reloads, or allow the marine to pick which mag to swap in when tactical reloading, or require that the marine start with N magazines, empty or otherwise, and have to go find some on the ground after too many speed reloads.

    i don't go shooting that much, but i do practice these enough already in real life, and would rather just play a game where i can hose down ugly things with bullets :)
  • BRICEBRICE Join Date: 2010-07-16 Member: 72453Members
    edited August 2010
    Thank you i finally found one person that noticed that fact besides me (know).
    RobB
  • NeXuZNeXuZ Join Date: 2003-08-12 Member: 19594Members
    sweet, haven't look at LUA yet. but it reminds me of javascript.
  • burton75stephensburton75stephens Join Date: 2010-08-23 Member: 73747Members
    <!--quoteo(post=1767076:date=Apr 15 2010, 03:45 PM:name=GDWhite)--><div class='quotetop'>QUOTE (GDWhite @ Apr 15 2010, 03:45 PM) <a href="index.php?act=findpost&pid=1767076"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I was an addict like you once.
    But I was intervened. By this guy.
    <span style='color:#000000;background:#000000'>trollface.jpg</span>
    There's a difference between clips and magazines! I KNOW RIGHT!
    Where were our beloved game developers when this crucial bit of information slipped through the cracks in the transition from CoD 3 to Modern Warfare? I was no longer plugging clips! I was cramming mags!
    I, for one, have been fighting my addiction ever since, but this photo brings me hope.

    <img src="http://1.bp.blogspot.com/_RYwUPr35rLw/SMSzcYalaPI/AAAAAAAAAM4/pgTUU7jUlFo/s1600/Magazine%20-%20vs%20-%20Clip.jpg" border="0" class="linked-image" /><!--QuoteEnd--></div><!--QuoteEEnd-->


    This is something amazing!!!
  • NeoGregorianNeoGregorian Join Date: 2003-02-04 Member: 13093Members, Constellation
    Much shorter way to achieve the same effect (and therefore easier to reverse) :
    In ClipWeapon.lua, comment out a single line in ClipWeapon:FillClip

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function ClipWeapon:FillClip()
        //self.ammo = self.ammo + self.clip
        self.clip = math.min(self.ammo, self:GetClipSize())
        self.ammo = self.ammo - self.clip
    end<!--c2--></div><!--ec2-->


    Also, in the spirit of this thread, refactor every usage of 'Clips' to 'Mags'.
  • remiremi remedy [blu.knight] Join Date: 2003-11-18 Member: 23112Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester
    edited December 2010
    <!--quoteo(post=1812222:date=Dec 1 2010, 01:08 AM:name=NeoGregorian)--><div class='quotetop'>QUOTE (NeoGregorian @ Dec 1 2010, 01:08 AM) <a href="index.php?act=findpost&pid=1812222"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Much shorter way to achieve the same effect (and therefore easier to reverse) :
    In ClipWeapon.lua, comment out a single line in ClipWeapon:FillClip

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function ClipWeapon:FillClip()
        //self.ammo = self.ammo + self.clip
        self.clip = math.min(self.ammo, self:GetClipSize())
        self.ammo = self.ammo - self.clip
    end<!--c2--></div><!--ec2-->


    Also, in the spirit of this thread, refactor every usage of 'Clips' to 'Mags'.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Nice hack. By commenting out that first line you effectively throw out the current clip. :)

    Also because this doesn't add any network vars or new functions, Neo's mod should be able to be run on any server and let vanilla clients still connect (and though there may be a slight prediction hitch every time the player reloads, it's a server authoritative model so the mod will behave as expected).
  • Soylent_greenSoylent_green Join Date: 2002-12-20 Member: 11220Members, Reinforced - Shadow
    Here's how I'd do it; it's very slightly more complex to implement, but shouldn't pose any major problems.

    When you reload, the chambered round is carried over to the new magazine unless there is no round to carry. The magazine selected is always the one with the most bullets remaining(which will always be a fresh mag if one exists). When you fill up ammo at an armory or from ammo drops, the magazine with the smallest number of bullets is filled first(to simulate grabbing a fresh mag if the magazine replaced has zero bullets or throwing away the mag with the least bullets and replacing it with a fresh one if the replaced mag has bullets). The magazine counter is simply the number of mags that have non-zero ammo count.
  • HarimauHarimau Join Date: 2007-12-24 Member: 63250Members
    You wouldn't just discard magazines upon reloading?
  • TigTig Join Date: 2010-05-08 Member: 71674Members, Reinforced - Shadow, WC 2013 - Silver
    <!--quoteo(post=1820565:date=Dec 31 2010, 02:01 PM:name=Harimau)--><div class='quotetop'>QUOTE (Harimau @ Dec 31 2010, 02:01 PM) <a href="index.php?act=findpost&pid=1820565"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->You wouldn't just discard magazines upon reloading?<!--QuoteEnd--></div><!--QuoteEEnd-->

    they're expensive.
  • HarimauHarimau Join Date: 2007-12-24 Member: 63250Members
    So you want to reload halfway through your first magazine, and after going through all of your other magazines completely, you'd still be able to use the half-full magazine you originally used?
  • Soylent_greenSoylent_green Join Date: 2002-12-20 Member: 11220Members, Reinforced - Shadow
    <!--quoteo(post=1820682:date=Jan 1 2011, 02:18 AM:name=Harimau)--><div class='quotetop'>QUOTE (Harimau @ Jan 1 2011, 02:18 AM) <a href="index.php?act=findpost&pid=1820682"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->So you want to reload halfway through your first magazine, and after going through all of your other magazines completely, you'd still be able to use the half-full magazine you originally used?<!--QuoteEnd--></div><!--QuoteEEnd-->

    Yes, why would you throw them away?
  • HarimauHarimau Join Date: 2007-12-24 Member: 63250Members
  • Soylent_greenSoylent_green Join Date: 2002-12-20 Member: 11220Members, Reinforced - Shadow
    <!--quoteo(post=1821071:date=Jan 3 2011, 01:36 AM:name=Harimau)--><div class='quotetop'>QUOTE (Harimau @ Jan 3 2011, 01:36 AM) <a href="index.php?act=findpost&pid=1821071"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Why wouldn't you?<!--QuoteEnd--></div><!--QuoteEEnd-->

    Because you might need them and it takes .2 extra seconds to not throw them away.
  • HarimauHarimau Join Date: 2007-12-24 Member: 63250Members
Sign In or Register to comment.