It sounds like a risky thing to do, unless you know exactly what each of the code sections are going to do. One may for example perform a return, or send the script to a different function, at which point the subsequent code would get ignored completely.
But if it were me I would probably copy the existing function into a new one (<scriptname>.shipBeingAttacked = <scriptname>.originalShipBeingAttacked or somesuch), add your code as newShipBeingAttacked and then replace the original function with one that simply calls the two new functions in turn.
P.target.script.originalAttacked = P.target.script.beingAttacked;
P.target.script.beingAttacked = function(){
new stuff
this.originalAttacked();
more new stuff
};
I'd check both name and version of the particular ship script to get some idea of what was inside the original beingAttacked() function.
Of course, name & version could be lies: as a general software thing, changing version numbers tends to be forgotten when making changes to the actual code.
That should work. But to be completely at the save side I would not use this.originalAttacked(); but this.mcLaneAttacked();, (or any other unique name) just for the case there was another oxp that already had changed the script and also used the name this.originalAttacked();
this.applyHeadPatch = function(target, property, patch)
{
var original = target[property];
target[property] = function()
{
// Depending on what you’re doing you might want to swap the order,
// in which case it’s a tail patch.
patch.apply(this, arguments);
original.apply(this, arguments);
}
}
...
this.patchTarget = function()
{
this.applyHeadPatch(player.target.script, "beingAttacked", function()
{
// Do stuff
});
}
Thanks all! Will try how it works. And if it seems that it may break things, I will of course not use it.
What I want is to simulate a requested-but-not-yet-existing shipHittingOther event handler for an NPC by inserting a script line into the ship script of the opponent, which will call the shipHittingOther function when it gets hit itself, therefore I need to access the shipBeingAttacked function in the opponent's script, without completely overwriting it.
this.patchTarget = function()
{
var originalAttacked = player.ship.target.script.beingAttacked;
player.ship.target.script.beingAttacked = function()
{
// Do stuff
if (originalAttacked) originalAttacked();
}
}
In the versions you & Ahruman wrote originalAttacked would always be undefined, and - thinking about it - some ships' scripts might not have a .beingAttacked function to begin with!
I had the problem with the undefined again, because I wanted to amend the world script shipWillDockWithStation from a ship script, when an escape pod was scooped. Of course originalShipWillDockWithStation was always undefined when I actually docked, because at this point the escape pod with the ship script had of course seized to exist. So I had to define worldScripts.personalities.originalShipWillDockWithStation, and now it's working fine.
By the way: this is quite a complicated workaround for giving a ship two different specific escape pods, which both have to be spawned.
I had the problem with the undefined again, because I wanted to amend the world script shipWillDockWithStation from a ship script, when an escape pod was scooped.
Wouldn’t it be easier to just have a world script check a condition and do what you need when it’s appropriate? You could then set a flag like:
Generally I agree. But in this case a ship script of a personalities ship in set A has to access the world script, which will be shipped with the original OXP. While I of course could put it in the world script, I won't be able to do so with upcoming new sets of characters. I don't want to re-distribute the original OXP, because of a small change in its script.
But it will all stay in the family. I have no intention to amend something outside the personalities OXPs.