Let me try to explain what I’m doing in the code below, which just feels so unclean and wrong… I’m struggling to find a way to do this.
Imagine I have a vehicle that has two weapons of the same type, firing from different locations on the vehicle. They have a “Shot Delay” of one second, so every second, both weapons release one piece of ordnance/projectile at the same time. I have all this working, but I now want to add a flexible system that allows me to offset the fire time of each weapon based on how many duplicates of it there are. I’m calling this flag ‘Shot Alternate’. If the weapons in this scenario had this set to true, then the first weapon would fire, then 0.5 of a second later (Shot Delay / Num Of This Weapon), the second weapon would fire. So on and so forth.
I want this to be the case regardless of how many weapons of the same type are on the craft. So if there are three, they will fire sequentially every 0.33 seconds (approximately). You get the idea. Essentially if a Sub-Class of ABZGame_Weapon has this flag set to true, whenever mounted on a vehicle it will cycle the shots between each ‘Hardpoint’.
This is where I’m heading with it so far, which just feels incredibly long-winded. The hardpoints of the craft may change at runtime as craft collect powerups and replace weapons, so it doesn’t have to be run often, but it still feels long-winded.
My idea is the following.
1 - Store a float ‘TimeOffset’ in each weapon class. (The weapons are of the Actor class, like ShooterGame).
2 - Offset the float in each weapon in the GameObjectComponent, based on how many duplicates of it there are in the same firing group*.
3 - This needs to change and update / reset when the loadout changes.
*The Firing-Groups are arrays of weapons that fire at the same time. The player can select different banks of weapons to fire.
This code is obviously wrong anyway, and I haven’t got as far as applying the offsets… But it’s the general direction I’m heading in. Can somebody possibly suggest a better way to do this?
void UBZGame_GameObjectComponent::UpdateWeaponTimeOffsets()
{
TArray<ABZGame_Weapon*> UniqueWeaponTypes;
TArray<int32> NumberOfDuplicates;
/* Search the Hardpoints for Weapon Types. Only add the Unique Weapon types that have ShotAlternate to TRUE. */
for (int32 i = 0; i < HardPoints.Num(); i++)
{
if (HardPoints*.CurrentWeapon && (HardPoints*.CurrentWeapon->WeaponConfig.ShotAlternate == true))
{
UniqueWeaponTypes.AddUnique(HardPoints*.CurrentWeapon)
}
}
/* Set Num Duplicates To Same Length as Unique Wep Types */
NumberOfDuplicates.Init(UniqueWeaponTypes.Num());
/* For each of the unique weapon types, count how many are in the array. */
for (int32 i = 0; i < UniqueWeaponTypes.Num(); i++)
{
/* Loop through the array and count how many of each unqiue weapon type we have. */
for (int32 n = 0; n < HardPoints.Num(); n++)
{
if (HardPoints[n.CurrentWeapon && (HardPoints[n].CurrentWeapon->WeaponConfig.ShotAlternate == true) && HardPoints[n].CurrentWeapon->IsA(UniqueWeaponTypes*))
{
NumberOfDuplicates* += 1;
}
}
}
// DO OFFSET STUFF HERE
}