Skip to content

Commit

Permalink
Better error handling for Add/RemoveWeaponMount
Browse files Browse the repository at this point in the history
  • Loading branch information
sturnclaw committed Dec 6, 2024
1 parent dc0d74a commit f566cb1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions data/libs/Ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ function Ship:UpdateWeaponSlots()

for _, slot in ipairs(equipSet:GetAllSlotsOfType("weapon", true)) do
if not slot.gimbal then
print('Missing hardpoint gimbal on ship {} for slot {}' % { self.shipId, slot.id })
logWarning('Missing hardpoint gimbal on ship {} for slot {}' % { self.shipId, slot.id })
end

local gimbal = Vector2(table.unpack(slot.gimbal or { 1, 1 }))
gunManager:AddWeaponMount(slot.id, slot.tag, gimbal)
local ok = gunManager:AddWeaponMount(slot.id, slot.tag, gimbal)

if not ok then
logWarning('Unable to add weapon mount slot {} on ship {}' % { slot.id, self.shipId })
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions src/ship/GunManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,17 @@ bool GunManager::AddWeaponMount(StringName id, StringName tagName, vector2f gimb
return result.second;
}

void GunManager::RemoveWeaponMount(StringName id)
bool GunManager::RemoveWeaponMount(StringName id)
{
auto iter = m_mounts.find(id);
if (iter == m_mounts.end())
return;
return false;

if (IsWeaponMounted(id))
return;
return false;

m_mounts.erase(iter);
return true;
}

bool GunManager::MountWeapon(StringName hardpoint, const WeaponData &gunData)
Expand Down
3 changes: 2 additions & 1 deletion src/ship/GunManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class GunManager : public LuaWrappable {
bool AddWeaponMount(StringName id, StringName tagName, vector2f gimbalLimitDegrees);
// Remove a weapon mount from this gun manager.
// The caller should always ensure that the weapon mount is empty before calling this function.
void RemoveWeaponMount(StringName id);
// Returns false if the mount does not exist or is not empty.
bool RemoveWeaponMount(StringName id);

// Attach a weapon to a specific mount.
// Returns false if the hardpoint cannot be found or the weapon could not be mounted.
Expand Down

0 comments on commit f566cb1

Please sign in to comment.