Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Mod bug only after loading  (Read 1391 times)

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7227
  • Harpoon Affectionado
    • View Profile
Mod bug only after loading
« on: August 12, 2012, 09:36:17 AM »

I normally wouldn't post a modding problem here but its so strange I think it might actually be a bug. I have this function (see below) from my restock mod that works perfectly - right up until you load a saved game. When that happens ships are added even when they are already present - the message statements show that everything is working just fine: .getMembersListCopy() is returning the correct number of ships and .getSpecId() is returning a string that appears to be identical to the targetShip string. The if statement returns false however - but only the first call after loading a saved game (I have no idea why that would matter).

Code
private void restockShip(FleetDataAPI mothFleet, String targetShip, FleetMemberType fMT){

List mothShips = mothFleet.getMembersListCopy();
int flag = 0;
//Global.getSector().addMessage("mothSize: " + mothShips.size());
for (int j=0; j < mothShips.size(); j++){
      FleetMemberAPI member = (FleetMemberAPI) mothShips.get(j);
Global.getSector().addMessage("_"+(String) member.getSpecId() + "=?" + targetShip +"_");
if ((String) member.getSpecId() == (String) targetShip){
flag = 1;
Global.getSector().addMessage((String) member.getSpecId() + "==" + targetShip);
}
    }

if (flag == 0){
Global.getSector().addMessage("Adding ship: " + targetShip);
mothFleet.addFleetMember(Global.getFactory().createFleetMember(fMT, targetShip));
}
}

I know the way this is written isn't exactly elegant so I'll be pretty embarrassed if this is just some Java error springing from my ignorance. :P I suppose the better thing to do would be to construct my own list of strings from the member.getSpecId()'s and just use the built in .contains(item) function... but this is just annoying me.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24128
    • View Profile
Re: Mod bug only after loading
« Reply #1 on: August 12, 2012, 09:43:49 AM »

Aha! It is indeed a Java error :)

if ((String) member.getSpecId() == (String) targetShip){


Should be:

if (member.getSpecId() != null && member.getSpecId().equals(targetShip)){


== compares the values of the references - i.e., whether those two are actually the same String object, not whether the object has the same contents. equals() is a function available on all objects that actually compares their contents.

The "member.getSpecId() != null" part is just to be safe. getSpecId() should never return null, but in general, if you're dealing with an API and aren't sure whether null is a valid return value for a given function, it doesn't hurt to check before using it.
Logged

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7227
  • Harpoon Affectionado
    • View Profile
Re: Mod bug only after loading
« Reply #2 on: August 12, 2012, 09:48:34 AM »

Huh, I guess it worked the other times because the ship was made using that string instance and after reloading the instances were different.

Thanks very much!
Logged