Ok so today when I was working on my epicly awesome mod for Killing Floor, I want to make a pistol that could switch from semi-automatic firing mode to a three short burst firing mode. After about 30 or so minutes of trial and error I final got it to work! I was so proud of my achievement that I decided to share it with the Killing Floor community! Here is the code for the weapon without all the fancy stuff required to actually make the gun:
Code:
class Singleb extends KFWeapon;
replication
{
reliable if(Role < ROLE_Authority)
ServerChangeFireMode;
}
// Use alt fire to switch fire modes
simulated function AltFire(float F)
{
if(ReadyToFire(0))
{
DoToggle();
}
}
// Toggle semi/auto fire
simulated function DoToggle ()
{
local PlayerController Player;
Player = Level.GetLocalPlayerController();
if ( Player!=None )
{
//PlayOwnedSound(sound'Inf_Weapons_Foley.stg44_firemodeswitch01',SLOT_None,2.0,,,,false);
SingleFireB(FireMode[0]).bSetToBurst = !SingleFireB(FireMode[0]).bSetToBurst;
if ( SingleFireB(FireMode[0]).bSetToBurst )
Player.ReceiveLocalizedMessage(class'KFTestMut.BurstSwitchMessage',0);
else Player.ReceiveLocalizedMessage(class'KFTestMut.BurstSwitchMessage',1);
}
Super.DoToggle();
ServerChangeFireMode(SingleFireB(FireMode[0]).bSetToBurst);
}
// Set the new fire mode on the server
function ServerChangeFireMode(bool bNewSetToBurst)
{
SingleFireB(FireMode[0]).bSetToBurst = bNewSetToBurst;
}
exec function SwitchModes()
{
DoToggle();
}
Here is the code for the fire itself:
Code:
class SingleFireB extends KFFire;
var() int Burstlength;
var() int Normallength;
var() float Burstrate;
var int RoundsToFire;
var bool bBursting;
var bool bSetToBurst;
simulated function bool AllowFire() //This doesn't really work but it
//prevents you from firing the rest of the shots in the burst after
//you reload. Pretty handy anyways.
{
if (Singleb(Weapon).MagCapacity <= 2)
return false;
return Super.AllowFire();
}
event ModeDoFire()
{
if(bBursting && RoundsToFire > 0)
RoundsToFire--;
//If not already firing, start a burst.
if(!bBursting && AllowFire())
{
bBursting = true;
RoundsToFire = BurstLength;
SetTimer(BurstRate, true);
}
if(RoundsToFire < 1)
{
SetTimer(0, false);
RoundsToFire = 0;
bBursting = false;
return;
}
if(!bSetToBurst && AllowFire())
{
RoundsToFire = Normallength;
bBursting = true;
}
Super.ModeDoFire();
}
simulated function Timer()
{
if(bBursting)
ModeDoFire();
else SetTimer(0,false);
}
defaultproperties
{
Burstlength=3.000000
Burstrate=0.100000
Normallength=1.000000
bSetToBurst=False
bWaitForRelease=True
}
And here is the code for the weapon change message:
Code:
class BurstSwitchMessage extends CriticalEventPlus;
var() localized string SwitchMessage[10];
static function string GetString (optional int Switch, optional PlayerReplicationInfo RelatedPRI_1, optional PlayerReplicationInfo RelatedPRI_2, optional Object OptionalObject)
{
if ( (Switch >= 0) && (Switch <= 9) )
return Default.SwitchMessage[Switch];
}
defaultproperties
{
SwitchMessage(0)="Set to Three-Short Burst."
SwitchMessage(1)="Set to Semi-Automatic."
DrawColor=(B=0,G=0,R=220)
FontSize=-2
}
Surely the community will find use for this one way or another