Killer Wolf wrote: ↑Wed Dec 06, 2023 4:29 pm
is my if statement wrong?
Kind of.
There's a couple of ways to handle it. If you just want to apply the same discount to *everything*, you'd be better off setting the
equipment_price_factor
of the station in your shipdata.plist file.
However, if you only want to discount some selected items, and again use the same discount for all of them, here's some options:
Code: Select all
this.updateEquipmentPrice - function (equipment, currentPrice) {
var newprice = currentPrice;
if (equipment == "EQ_FUEL" || equipment == "EQ_MISSILE" || equipment == "EQ_CARGO_BAY") {
if (player.ship.dockedStation.dataKey == "KW_II") {
newprice = parseInt(newprice * 0.5);
}
}
return newprice;
}
A lot of typing with that one, and it ends up being unreadable once you have a lot of items.
Then there's this:
Code: Select all
this.updateEquipmentPrice - function (equipment, currentPrice) {
var newprice = currentPrice;
if (["EQ_FUEL", "EQ_MISSILE", "EQ_CARGO_BAY"].indexOf(equipment) >= 0) {
if (player.ship.dockedStation.dataKey == "KW_II") {
newprice = parseInt(newprice * 0.5);
}
}
return newprice;
}
We're essentially creating an array and checking the if the equipment item matches one of the elements. If it does, the index will be zero or higher. You just add extra items into the array to expand the list.
Another alternative is:
Code: Select all
this.updateEquipmentPrice - function (equipment, currentPrice) {
var newprice = currentPrice;
switch (equipment) {
case "EQ_FUEL":
case "EQ_MISSILE":
case "EQ_CARGO_BAY":
if (player.ship.dockedStation.dataKey == "KW_II") {
newprice = parseInt(newprice * 0.5);
}
break;
}
return newprice;
}
Here we're using a switch statement, which is kind of like a structured If statement. What this is making use of is the "fall through" processing that a switch statement has. Basically, we start by telling the switch what we're switching on (ie, the equipment variable). Then we specify each of the cases the switch needs to consider. Normally, you'd have the "break;" command after each case, because switch statements will fall through the cases until it hits one. But in this case, by only having the one "break;" we've essentially created a large OR statement. It reads like this:
If equipment is equal to "EQ_FUEL" OR is equal to "EQ_MISSILE" OR is equal to "EQ_CARGO_BAY", then execute the code following. If equipment is not equal to any of those, then skip everything.
The benefit of this last version is it makes it relatively easy to change your mind about discounts on certain items. For instance, if you decide you want to only have a 25% discount on missiles, you rearrange it like this:
Code: Select all
this.updateEquipmentPrice - function (equipment, currentPrice) {
var newprice = currentPrice;
switch (equipment) {
case "EQ_MISSILE":
if (player.ship.dockedStation.dataKey == "KW_II") {
newprice = parseInt(newprice * 0.75);
}
break;
case "EQ_FUEL":
case "EQ_CARGO_BAY":
if (player.ship.dockedStation.dataKey == "KW_II") {
newprice = parseInt(newprice * 0.5);
}
break;
}
return newprice;
}
Of course, you'd end up checking the datakey of the docked station a lot that way. So, one final change I'd make would be to make all that checking unnecessary by doing this:
Code: Select all
this.updateEquipmentPrice - function (equipment, currentPrice) {
if (player.ship.dockedStation.dataKey != "KW_II") return currentPrice;
var newprice = currentPrice;
switch (equipment) {
case "EQ_MISSILE":
newprice = parseInt(newprice * 0.75);
break;
case "EQ_FUEL":
case "EQ_CARGO_BAY":
newprice = parseInt(newprice * 0.5);
break;
}
return newprice;
}
Anyway, hope that helps a bit.