This I attach to a test ship. Then I spawn several ships around the station with the debugMenu.
When I kill them, all scripts from ships spawn after the first, generate errors in the log about the first line after "this.shipDied = function()". In this case it is "player is not defined". But when I add a log function it says "Log not defined"
However, the script does works with the first spawn ship, even if it is killed after the others that generated errors, showing that the script by itself is OK and implemented at the right way.
Should also add (as posted on the Scripters Cove thread), that when a function (whom) is added, whom seems to always to remain undefined, regardless of whether the kill was by the player or an NPC.
OXPS : The Assassins Guild, Asteroid Storm, The Bank of the Black Monks, Random Hits, The Galactic Almanac, Renegade Pirates can be downloaded from the Elite Wiki here.
Had a look at the code regarding the whom situation and the answer could not be simpler than this: whom with ships other than the player does not work because there is no code to handle it. In the getDestroyedBy method, in ShipEntity.m, there is this:
- (void) getDestroyedBy:(Entity *)whom context:(NSString *)context
{
suppressExplosion = NO; // Can only be set in death handler
// TODO: send didBecomeDead(whom, context) script event here
[self becomeExplosion];
}
I have added code in the trunk that broadcasts the shipDied event (didBecomeDead was the old name used), but I have a few reservations about it. LittleBear, maybe if I send you a modified 1.70 with this fix applied, you will be able to test your script and let me know if it works for you?
Cool! If you e-mail me it I'll give it a test. I don't know how to build anything though, so it'd have to be as a replacement exe (or with some instructions! ). I've pretty much squoshed all the bugs in V.10, but would really need the whom to simplfy the whole thing.
OXPS : The Assassins Guild, Asteroid Storm, The Bank of the Black Monks, Random Hits, The Galactic Almanac, Renegade Pirates can be downloaded from the Elite Wiki here.
For the time being you can also fall back on: this.beingAttacked = function(whom). that one is already working. I tried the code below and it is working. (Only the first ship with me)
this.beingAttacked = function(whom)
{
player.commsMessage("Help we are being attacked by scripttesters!!", 5);
if(whom == player)
{
player.commsMessage("Hello player, whom is player", 5);
}
}
Not just as good, but it at least detects if the played was among the attackers. Than it is just the issue of only the first ship that works. Do you have the same problem? Even when I spawn a ship, dock, load a new game and spawn again a new ship, the shipDied of the second ship is not working. The same is happening with this.beingAttacked. In both cases he complains the second time that player is not defined.
Addition to the bug. I always added the ships with debug menu. Now I added them with a script near the station on launching: addSystemShips: testship 3 0.99
All three ship scripts were working. I killed them, re-launched so three new ones were added and those tree scripts also worked.
So the problem is in the command that added the ships. Probably normal ship adding scripts do work. But the purpose of debug menu was testing of ships and scripts of those ships should also work.
Zip seems corrupted unfortunatley. Could you try another go? Eric's find of the this.beingAttacked should do equally well though for random hits.
I am cheating a bit, but any ship that attacks the mark gets its AI set to a harmlessAI that makes it run away. Once the mark is out of scanner range the ship reverts to its normal AI. This is necessary as otherwise victims in boyracers etc invariable fall to cops before the player finds them. This (obviously) has no effect on the player, but it means no NPC can fire more than one shot at the mark. It is still possible for a mark to be killed by an NPC (if one of Matts Navy Ships opens up with all guns for example - but I'm not too bothered if once in a blue moon the player has a wasted trip!). If I put an else condition to set the who fired to variable NPC, then the variable will only be PLAYERFIRED if the player was the last ship to shoot him before he died.
If I just set the status to KILLED with death_action. Then have status = KILLED, whofired = PLAYER do = This was a player kill. status = KILLED, whofired oneof NPC / undefined do = This was an NPC kill.
OXPS : The Assassins Guild, Asteroid Storm, The Bank of the Black Monks, Random Hits, The Galactic Almanac, Renegade Pirates can be downloaded from the Elite Wiki here.
Eric's find of the this.beingAttacked should do equally well though for random hits.
For normal dogfight, yes. But you'll miss those occasions when a NPC ships weakens your mark and the player kills him by ramming or hitting him with a missile. But I think those will be rare and in general it will be good enough for the purpose you want to use it in Random hits (And for my ups boa mission)
EDIT. Done a script that used both methods (isbeingAttacked and shipDied). Tested the isbeingAttacked (see scripters cove). Will test the shipDied using the new build and report in.
OXPS : The Assassins Guild, Asteroid Storm, The Bank of the Black Monks, Random Hits, The Galactic Almanac, Renegade Pirates can be downloaded from the Elite Wiki here.