A simple tutorial: Rifle clips
<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-->
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
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).
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" />
<!--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-->
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?)
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.
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...
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.
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.
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 :)
RobB
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!!!
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'.
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).
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.
they're expensive.
Yes, why would you throw them away?
Because you might need them and it takes .2 extra seconds to not throw them away.