[Javascript] cloning objects
Posted: Fri Dec 07, 2012 9:28 pm
Problem : automatically add extra Mamushi whenever one is spawned. Effectively ensuring that they never appear alone.
However... I'm struggling creating new ship objects in a sensible way.
(all working is shown for completeness)
I can't find any kind of clone ship method.
There does not seem to be a constructor for Ship. So I can't create an empty one and populate it from the existing object...
I looked to see if I could spawn one of anything, and copy the existing one over it, but not all fields are writable...
I could use legacy_spawnShip, but that creates a ship, and does not return a reference to it, so the created ship is a mamushi somewhere in the system, not in this group...
So I added a new role to the mamushi allowing me to call this.ship.spawnOne with that role, then move it to the appropriate position, role and group.
Fine? Except that spawnOne creates a shipSpawned event, which leads to the wonderful phenomenon of infinite regression.
To try to control the regression I've tried to override the method, setting a default function which just logs, but it appears that this is neither an error, nor working. (i.e. I can set the function reference, but it seems to use the default as setup originally).
I've also tried to use a global variable which gets changed the very first time the shipSpawned method is called, and therefore I can put a guard condition in so it is limited to how often it is called, but that obviously would need resetting on some event or else you would only be able to do this once.
I have stopped infinite regression by counting the number of Mamushi in the system, but that means that I always get none, or that number split roughly evenly across the groups. I have subsequently limited each group to a maximum number of Mamushi, but this all seems rather clunky.
In addition to that, the 'cloned' ships don't seem quite all there, or sticking in their groups. Dumping data shows some odditites.
I'm manually copying the role, group and AIState, and calling setAI, but I'm seeing data like this:
I looked at AIs but they didn't seem to be very clear, so I left them alone for now.
Anyone else hit this and got an elegant and working solution? Is it in fact possible?
By the way is there any info on the event calling under the hood. I'm guessing events are added to a queue or a set and fired one at a time? I'm not sure if the collection of events is ordered or not.
thanks in advance
However... I'm struggling creating new ship objects in a sensible way.
(all working is shown for completeness)
I can't find any kind of clone ship method.
There does not seem to be a constructor for Ship. So I can't create an empty one and populate it from the existing object...
I looked to see if I could spawn one of anything, and copy the existing one over it, but not all fields are writable...
I could use legacy_spawnShip, but that creates a ship, and does not return a reference to it, so the created ship is a mamushi somewhere in the system, not in this group...
So I added a new role to the mamushi allowing me to call this.ship.spawnOne with that role, then move it to the appropriate position, role and group.
Fine? Except that spawnOne creates a shipSpawned event, which leads to the wonderful phenomenon of infinite regression.
To try to control the regression I've tried to override the method, setting a default function which just logs, but it appears that this is neither an error, nor working. (i.e. I can set the function reference, but it seems to use the default as setup originally).
I've also tried to use a global variable which gets changed the very first time the shipSpawned method is called, and therefore I can put a guard condition in so it is limited to how often it is called, but that obviously would need resetting on some event or else you would only be able to do this once.
I have stopped infinite regression by counting the number of Mamushi in the system, but that means that I always get none, or that number split roughly evenly across the groups. I have subsequently limited each group to a maximum number of Mamushi, but this all seems rather clunky.
In addition to that, the 'cloned' ships don't seem quite all there, or sticking in their groups. Dumping data shows some odditites.
I'm manually copying the role, group and AIState, and calling setAI, but I'm seeing data like this:
worrying bits in bold.21:16:12.126 [universe.objectDump]: Ent: 35 ShipEntity "Mamushi Light Fighter" position: (1.#QNAN, -9728.54, 143254) scanClass: CLASS_NEUTRAL status: STATUS_IN_FLIGHT range: 1.#QNAN (visible: no) mass 12745.8 AI: nullAI.plist:GLOBAL
I looked at AIs but they didn't seem to be very clear, so I left them alone for now.
Anyone else hit this and got an elegant and working solution? Is it in fact possible?
By the way is there any info on the event calling under the hood. I'm guessing events are added to a queue or a set and fired one at a time? I'm not sure if the collection of events is ordered or not.
thanks in advance