Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Bizarre behavior when trying to copy variant from ship to drones  (Read 381 times)

Xaiier

  • Lieutenant
  • **
  • Posts: 56
    • View Profile
Bizarre behavior when trying to copy variant from ship to drones
« on: November 20, 2021, 02:28:17 PM »

Ok so from the title alone you probably have lots of questions, but the TL;DR is that I'm trying to make a ship which spawns "clones" of itself as a drone ship system. The key part of this is taking the loadout from the main ship and applying it to the spawned drones, so they match the main ship. This is where the problems start to arise.

I've tried two slightly different methods of accomplishing this, with similar results. Method 1 consists of a built-in hull mod which attempts to check if the ship in question is a drone, and copy the loadout from the mothership to itself:

Code
@Override
public void advanceInCombat(ShipAPI ship, float amount) {
if (ship.isDrone()) {
ship.setShipSystemDisabled(true);

ShipVariantAPI variant = ship.getVariant();
ShipVariantAPI parentVariant = ship.getDroneSource().getVariant();

variant.clearHullMods();
for (String mod : parentVariant.getHullMods()) {
variant.addMod(mod);
}

for (String slot : parentVariant.getFittedWeaponSlots()) {
variant.clearSlot(slot);
variant.addWeapon(slot, parentVariant.getWeaponId(slot));
}

variant.setNumFluxCapacitors(parentVariant.getNumFluxCapacitors());
variant.setNumFluxVents(parentVariant.getNumFluxVents());
}
}

This kinda works, except that the first 3 (out of 10 in this case) drones end up with an "old" loadout. The first time you enter the sim it puts the default variant weapons, and if you change the weapons in the refit screen and go into the sim again, the first three drones deployed will have whatever weapon was previously fitted. If you instead deploy a series of the ship each with different loadouts, you find that all of the drones deployed from each new ship have the weapons from the last ship that was deployed.

Method 2 consists of a built-in hull mod which attempts to find all the drones and copy the loadout from the mothership to them:

Code
@Override
public void advanceInCombat(ShipAPI ship, float amount) {
List<ShipAPI> drones = ship.getDeployedDrones();

for (ShipAPI drone : drones) {
drone.setShipSystemDisabled(true);

ShipVariantAPI variant = drone.getVariant();
ShipVariantAPI parentVariant = ship.getVariant();

variant.clearHullMods();
for (String mod : parentVariant.getHullMods()) {
if (mod != "xai_clone") {
variant.addMod(mod);
}
}

for (String slot : parentVariant.getNonBuiltInWeaponSlots() ) {
variant.clearSlot(slot);
String s = parentVariant.getWeaponId(slot);
if (s != null) {
variant.addWeapon(slot, s);
}
}

variant.setNumFluxCapacitors(parentVariant.getNumFluxCapacitors());
variant.setNumFluxVents(parentVariant.getNumFluxVents());
}
}

This works a bit better, however the first drone deployed always has the "old" loadout - either the previous (or default) weapon you went into the sim with, or the loadout of the last deployed ship of this type.


I'm totally stumped by this behavior, and any help figuring this out would be appreciated. As best as I can tell, my code is working correctly but getVariant() is somehow finding an old version that hasn't been cleaned up by the GC. What's really weird is that the two methods behave slightly differently which seems to indicate something really odd happening under the hood with how variants are handled, at least for drones specifically.
Logged