9.4 KiB
Gun-Centric Architecture Guide
The EasyBallistics plugin has been refactored to use a gun-centric approach where weapons are the primary actors with modular components. This consolidates scattered settings and provides a more intuitive workflow for creating realistic weapons.
Overview
The new architecture consists of:
- AEBGun - Main weapon actor
- UEBMagazine - Magazine component for ammunition storage
- UEBBarrel - Refocused barrel component for ballistics
- UEBWeaponConfiguration - Asset that consolidates all weapon settings
- UEBBulletPropertiesAsset - Bullet characteristics (unchanged)
Core Components
Gun Actor (AEBGun)
The main weapon actor that brings everything together:
// C++ Usage
AEBGun* MyGun = GetWorld()->SpawnActor<AEBGun>();
MyGun->ApplyWeaponConfiguration(MyM4Config);
MyGun->LoadMagazine(BulletArray);
MyGun->PullTrigger();
Key Features:
- Centralized weapon state management
- Realistic gun controls (safety, charging handle, trigger)
- Automatic configuration from weapon assets
- Full multiplayer support
- Event-driven architecture
- Support for both skeletal and static mesh guns
- Flexible muzzle positioning (socket-based for skeletal, transform-based for static)
Mesh Configuration
The gun actor supports both skeletal and static mesh setups:
Skeletal Mesh Mode (Default):
// Set gun to use skeletal mesh (default)
MyGun->SetMeshType(true);
MyGun->SetMuzzleSocketName("MuzzleFlash"); // Use socket for muzzle position
Static Mesh Mode:
// Set gun to use static mesh
MyGun->SetMeshType(false);
MyGun->MuzzleTransform = FTransform(FVector(50, 0, 0)); // Set relative muzzle position
Key Differences:
- Skeletal meshes use sockets for precise muzzle positioning and can have animations
- Static meshes use relative transforms and are more performance-friendly for simple weapons
- Both modes maintain full compatibility with the firing system
Magazine Component (UEBMagazine)
Handles ammunition storage and feeding:
Magazine Types:
- Standard Box Magazine
- Drum Magazine
- Tube Magazine (shotguns)
- Stripper Clip
- Belt Fed (machine guns)
- Revolver Cylinder
- Internal Magazine (bolt-actions)
Features:
- Mixed ammunition support
- Malfunction simulation
- Bolt hold-open functionality
- Realistic capacity limits
- Feed rate control for belt-fed weapons
Barrel Component (UEBBarrel)
Fully integrated with the gun system for comprehensive ballistics:
Enhanced Integration:
- Parent Gun Reference: Direct connection to main gun actor
- Configuration Driven: All settings applied from weapon configuration
- Realistic Calculations: Physics-based muzzle velocity and accuracy
- Legacy Compatibility: Maintains old ammo system for existing projects
Barrel Properties Applied from Config:
- Barrel length, rifling twist rate, bore radius
- Muzzle velocity range and variations
- Accuracy and spread characteristics
- Recoil direction and multipliers
Smart Calculations:
// Barrel automatically calculates effective properties
float MuzzleVel = Gun->GetEffectiveMuzzleVelocity(); // Considers bullet weight, barrel length
float Accuracy = Gun->GetBarrelAccuracy(); // Factors in barrel length, rifling
FVector Recoil = Gun->CalculateRecoilImpulse(); // Physics-based recoil calculation
Features:
- Automatic muzzle velocity calculation based on barrel length and bullet weight
- Rifling twist optimization for different bullet types
- Physics-based recoil impulse generation
- Direct magazine and gun state integration
Weapon Configuration Asset (UEBWeaponConfiguration)
Consolidates all weapon settings into a single, manageable asset:
Configuration Sections:
- Barrel Configuration: Length, rifling, muzzle velocity, accuracy
- Fire Control: Available fire modes, rates, burst settings
- Reliability: Malfunction rates, environmental factors
- Ammunition: Compatible bullet types and magazine settings
- Physics: Mathematical vs artistic ballistics settings
Setting Up a New Weapon
1. Create Weapon Configuration Asset
- In Content Browser, right-click and navigate to Ballistics → Weapon Configuration
- Name it based on your weapon (e.g., "M4_Carbine_Config")
- The factory will auto-configure based on the name:
- M4/AR15 → Modern assault rifle settings
- AK → AK-type weapon settings
- Pistol/Glock/1911 → Handgun settings
- Sniper/Bolt → Bolt-action rifle settings
- Shotgun → Shotgun settings
- LMG/Machine → Light machine gun settings
2. Configure Your Weapon
Open the weapon configuration asset and customize:
Weapon Info:
Weapon Name: "M4 Carbine"
Manufacturer: "Colt"
Model: "M4A1"
Caliber: "5.56x45mm NATO"
Fire Control:
Available Fire Modes: [Semi-Auto, Full Auto]
Default Fire Mode: Semi-Auto
Fire Rate Min: 700 RPM
Fire Rate Max: 950 RPM
Burst Count: 3
Barrel Configuration:
Barrel Length: 14.5 inches
Muzzle Velocity: 91440 cm/s (~3000 fps)
Inherent Accuracy: 0.001 radians
Spread Max: 0.001 radians
3. Create Gun Blueprint
- Create a new Blueprint based on EBGun
- Set the Weapon Config to your configuration asset
- Configure the gun mesh and sockets
- Set up animations for firing, reloading, etc.
4. Set Up Ammunition
- Create Bullet Properties Assets for your ammunition types
- Add them to the weapon configuration's Compatible Bullet Types
- Set the Default Bullet Type
Blueprint Usage
Basic Firing
// Event: Player Input Trigger Pressed
Gun->PullTrigger();
// Event: Player Input Trigger Released
Gun->ReleaseTrigger();
Ammunition Management
// Load magazine with specific bullet types
TArray<UEBBulletPropertiesAsset*> Ammo;
Ammo.Add(M855_Bullet);
Ammo.Add(M193_Bullet);
Gun->LoadMagazine(Ammo);
// Check ammunition status
int32 RoundsLeft = Gun->GetRoundsInMagazine();
int32 TotalRounds = Gun->GetTotalRounds();
bool CanShoot = Gun->CanFire();
Weapon Controls
// Safety controls
Gun->SetSafety(true); // Safety on
Gun->SetSafety(false); // Safety off
// Manual cycling (bolt-actions, pump shotguns)
Gun->ChargingHandle(); // Cycle action
// Fire mode selection
Gun->SetFireMode(EFireMode::FM_Burst);
Events
// Gun Events
Gun->OnGunFired.AddDynamic(this, &AMyWeapon::OnGunFired);
Gun->OnMagazineEmpty.AddDynamic(this, &AMyWeapon::OnMagazineEmpty);
Gun->OnGunStateChanged.AddDynamic(this, &AMyWeapon::OnStateChanged);
// Magazine Events
Magazine->OnBulletFed.AddDynamic(this, &AMyWeapon::OnBulletFed);
Magazine->OnMagazineJammed.AddDynamic(this, &AMyWeapon::OnJammed);
Advanced Features
Custom Magazine Types
// Belt-fed machine gun
MagazineConfig.MagazineType = EMagazineType::MT_Belt;
MagazineConfig.MaxCapacity = 200;
MagazineConfig.FeedRate = 0.05f; // Fast feeding
// Tube magazine shotgun
MagazineConfig.MagazineType = EMagazineType::MT_Tube;
MagazineConfig.MaxCapacity = 8;
MagazineConfig.bAllowMixedBulletTypes = true; // Different shell types
Reliability Simulation
// Environmental effects
ReliabilityConfig.DirtSensitivity = 0.3f;
ReliabilityConfig.TemperatureSensitivity = 0.002f;
ReliabilityConfig.MinOperatingTemp = -30.0f;
ReliabilityConfig.MaxOperatingTemp = 55.0f;
// Wear and maintenance
ReliabilityConfig.WearFactor = 0.0001f;
ReliabilityConfig.ServiceLife = 15000; // rounds
Multi-Barrel Systems
// For weapons with multiple barrels (like gatling guns)
BarrelConfig.GatlingSpoolUpTime = 2.0f;
BarrelConfig.GatlingSpoolDownTime = 3.0f;
FireControlConfig.AvailableFireModes.Add(EFireMode::FM_Gatling);
Migration from Old System
Before (Scattered Settings)
// Settings spread across multiple components
Barrel->FireRate = 650.0f;
Barrel->Spread = 0.001f;
Barrel->MuzzleVelocity = 91440.0f;
Bullet->Mass = 0.004f;
Bullet->Diameter = 0.556f;
MaterialMap->PenetrationDepth = 15.0f;
After (Centralized Configuration)
// All settings in one place
WeaponConfig->FireControlConfig.FireRateMax = 650.0f;
WeaponConfig->BarrelConfig.SpreadMax = 0.001f;
WeaponConfig->BarrelConfig.MuzzleVelocityMax = 91440.0f;
WeaponConfig->DefaultBulletType = MyBulletAsset;
WeaponConfig->MaterialResponseMap = MyMaterialMap;
// Apply to gun
Gun->ApplyWeaponConfiguration(WeaponConfig);
Benefits of New Architecture
- Centralized Settings: All weapon parameters in one place
- Realistic Simulation: Proper magazine, safety, and malfunction systems
- Modular Design: Easy to swap magazines, barrels, configurations
- Better Workflow: Intuitive gun-first approach
- Reduced Complexity: No more hunting for scattered settings
- Multiplayer Ready: Built-in replication and authority handling
- Asset Reuse: Share configurations between similar weapons
Best Practices
- Use Weapon Configurations: Always create configuration assets for your weapons
- Group Similar Weapons: Share configurations between variants (M4A1, M4A3, etc.)
- Plan Magazine Types: Choose appropriate magazine types for realism
- Test Reliability: Adjust malfunction rates for your game's difficulty
- Configure Events: Use the event system for UI updates and effects
- Consider Physics Mode: Choose mathematical vs artistic based on your needs
This new architecture provides a much more intuitive and consolidated approach to weapon creation while maintaining all the advanced ballistics features of EasyBallistics.