Example code:
if(player.ship.equipmentStatus("EQ_ENERGY_BOMB") !== "EQUIPMENT_OK") player.ship.awardEquipment("EQ_ENERGY_BOMB");
...Will generate a script crash when it "hits" that line.
Time to rewrite a few OXPs?
Moderators: winston, another_commander
if(player.ship.equipmentStatus("EQ_ENERGY_BOMB") !== "EQUIPMENT_OK") player.ship.awardEquipment("EQ_ENERGY_BOMB");
An unhandled exception in a script is not a crash, dagnabbit.Switeck wrote:I ran into a problem with the Energy Bomb in trunk...any OXP that gives an Energy Bomb as a reward will now have its script crash at the point of the reward.
if (player.ship.canAwardEquipment("EQ_ENERGY_BOMB"))
. Don’t go second-guessing the game.Sorry, I meant only that script quits running. (...and even then, only that part of the script quits running!)Ahruman wrote:An unhandled exception in a script is not a crash, dagnabbit.Switeck wrote:I ran into a problem with the Energy Bomb in trunk...any OXP that gives an Energy Bomb as a reward will now have its script crash at the point of the reward.
In any case, the example is simply wrong. The correct test isif (player.ship.canAwardEquipment("EQ_ENERGY_BOMB"))
. Don’t go second-guessing the game.
No. What Ahruman says is that there is an importance of the difference betweenSwiteck wrote:is there any importance of the space between the if and (player?
Code: Select all
if(player.ship.equipmentStatus("EQ_ENERGY_BOMB") !== "EQUIPMENT_OK")
Code: Select all
if(player.ship.canAwardEquipment("EQ_ENERGY_BOMB"))
canAwardEquipment
). So why do you not use it, but insist on using a homemade workaround (equipmentStatus
) instead? Especially if the workaround throws an exception, but the proper method doesn't?canAwardEquipment
is important NOW, but why I didn't before:player.ship.canAwardEquipment
-- probably because there is no consideration that those pieces of equipment may not be allowed or exist.player.ship.canAwardEquipment
is likewise lacking from the overwhelming majority of OXPs that I have studied. I did a windows search for "canAwardEquipment" (sans quotes) to find a single OXP that used it in one script out of 100's of OXPs I have in my Oolite primary folder at the moment! (No doubt there's other OXPs that use it, but I oddly missed them when trying to learn how to properly reward equipment.) All the other OXPs probably lack that command primarily because the equipment they are awarding is overwhelmingly native equipment OR equipment created by the same OXPs and thus they do not consider that such equipment is not present.canAwardEquipment
really catches the eye, then.Probably, now the oxps need to do an explicit check for availability. Asteroid Storm wasSwiteck wrote:I ran into a problem with the Energy Bomb in trunk...any OXP that gives an Energy Bomb as a reward will now have its script crash at the point of the reward.......
Time to rewrite a few OXPs?
Code: Select all
this.addEnergyBomb = function () {
// equipment can be unknown after Oolite 1.76
if (player.ship.equipmentStatus("EQ_ENERGY_BOMB") !== "EQUIPMENT_UNKNOWN")
{
player.ship.awardEquipment("EQ_ENERGY_BOMB");
return true;
}
return false;
}
player.ship.canAwardEquipment("EQ_ENERGY_BOMB")
.That must be entertainingFatleaf wrote:For me the Ebomb is a Test Pilot only weapon. But I agree with you on the sound it makes. For a laugh swap the file names around so that in the F8 Commodity Market screen, every time you buy something it goes 'BOOM' instead
canAwardEquipment
existed until now. That said I don't think I've ever really needed it in the way that it is designed, instead it would have been "self-repairing" items via the equipmentDamaged
event.There is, and it is of the form that if you already know it, it is a good reference. But if you don't then looking at other people's code is a much better and easier-to-understand one.Smivs wrote:I have to confess canAwardEquipment passed me by as well. Mind you there is a huge wealth of information on those wiki js pages, and I don't claim to have an encyclopedic knowledge of all of it by any means.
Three most valid points indeed!Tricky wrote:Point is... READ, READ and READ!!!