weird problem with a search-and-remove function

For test results, bug reports, announcements of new builds etc.

Moderators: winston, another_commander, Getafix

Post Reply
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

weird problem with a search-and-remove function

Post by Commander McLane »

I've got a strange one. Perhaps one of you JS-wizards (Eric?) can help.

I am overhauling Anarchies for Oolite 1.72, an also took the opportunity to add/change some features. One of them is that the Hacker Outpost is now spawn via the Henchman's ship-script.

Works fine. But then I thought: I actually generate two henchmen in each Anarchy system with TL >= 7, one on the witchpoint-planet route, one on the witchpoint-sun route, so the player can encounter them and therefore create an Outpost, regardless whether he goes to the station or sun-skimming. NOw I thought, of course it's possible that the player flies around a little more, and eventually meets both henchmen, resulting in two Hacker Outposts being created (possibly in the same place). Not good.

Fortunately there is the new remove() function, so I thought, why not removing the other henchman in the ship-script, right after spawning the Outpost? Said and done. Here's the code:

Code: Select all

this.removeOtherHenchman = function()
{
	// create an array of both henchmen in the system
	this.allHenchmen = system.shipsWithPrimaryRole("anarchies_henchman", this.ship);
	// remove the last one (which is not the calling)
	log("Anarchies", this.allHenchmen[1])
	this.allHenchmen[1].remove()
}
This creates an array containing all (both) henchmen in the system, ordered by proximity to the calling henchman. Obviously the other one will always be the second in the array. So only the second entry has to be removed.

Except it doesn't work. I always get
[script.javaScript.warning.162]: ----- JavaScript warning: reference to undefined property this.allHenchmen[1]
[script.javaScript.warning.162]: /Applications/Spiele/Oolite 1.72/AddOns/Anarchies1.1.oxp/Scripts/anarchies-henchman-script.js, line 65.
[script.javaScript.exception.39]: ***** JavaScript exception: TypeError: this.allHenchmen[1] has no properties
[script.javaScript.exception.39]: /Applications/Spiele/Oolite 1.72/AddOns/Anarchies1.1.oxp/Scripts/anarchies-henchman-script.js, line 65.
But now comes the really strange part. I didn't encounter the problem while testing it by spawning a henchman with the Debug-menu. Only with the ones spawned by script.

I repeat: If I spawn a henchman via Debug-menu/JS-console, everything works fine. He scans for me, finds me, spawns the Hacker Outpost, searches for other henchmen in the system, lists them correctly in the this.allHenchmen-array, and removes the second entry from the array. Everything's fine.

But if I meet one of the henchmen spawned by script, he scans for me, finds me, spawns the Hacker Outpost, searches for other other henchmen in the system (I hope), but then Oolite insists that the this.allHenchmen-array doesn't exists, or has no properties.

I am completely puzzled. If I examine and compare a henchman spawned by script and one spawned by using the JS-console, I can discover to difference between them whatsoever. So what's the problem? Why does it work in one case, and doesn't in the other?

Help!

PS: When my console-created test-henchman successfully eliminates one of the other henchmen in-system, I get this in the log:
[method.undead]: Believed-dead method -[ShipEntity setSuppressExplosion:] called.
Does that help somehow?

PPS: And I do get the same error message, if I console-create a henchman in a system where there are no other henchmen. But that can't be the culprit with the script-created henchmen, because there are always two of them. And I can find and eliminate them perfectly, if I just type the same code I use in the script directly in the console-window. I am really puzzled.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post by Commander McLane »

Okay, as ever so often, the very moment I submitted the post I got an idea.

I replaced the "this.ship" in the code with "player.ship" (which--for what I want to achieve--is practically the same) and now it works with the script-spawned ships.

But of course that doesn't explain why it worked with console-spawned ships before.
User avatar
LittleBear
---- E L I T E ----
---- E L I T E ----
Posts: 2876
Joined: Tue Apr 04, 2006 7:02 pm
Location: On a survey mission for GalCop. Ship: Cobra Corvette: Hidden Dragon Rated: Deadly.

Post by LittleBear »

Had a similar problem spawning pods in Random Hits. The first pod to be spawned worked, but the second in the same game session didn't and generated similar errors. Added with addSytemShips the second pod followed its script, but not when spawned. Don't ask me why, but commenting out the descriptive bit of the script stopped this, like this:-

Code: Select all



/* 
this.name = "oolite-random-hits-pod1";
this.author = "LittleBear";
this.copyright = "September 2008 - But do what you like with it in your OXPs!";
this.description	= "Tests who killed or scooped a RandomHits OXP Escape Pod carring the mission target and how they did it. The main script acts on the setting of variables."
this.version = "2.0";
*/
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.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post by Commander McLane »

Very strange indeed. Perhaps in your case this.description is too long? Or the "!"-character is illegal in this.copyright?
User avatar
LittleBear
---- E L I T E ----
---- E L I T E ----
Posts: 2876
Joined: Tue Apr 04, 2006 7:02 pm
Location: On a survey mission for GalCop. Ship: Cobra Corvette: Hidden Dragon Rated: Deadly.

Post by LittleBear »

Maybe, but it worked for the first pod. If I killed a RH victim and he podded, no problem the pods script worked. If I then took another mission and killed another victim the second pods script didn't even though its the same script. Anyway the commenting out fixed it. :?
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.
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Post by Eric Walch »

McLane wrote:
Works fine. But then I thought: I actually generate two henchmen in each Anarchy system with TL >= 7, one on the witchpoint-planet route, one on the witchpoint-sun route, so the player can encounter them and therefore create an Outpost, regardless whether he goes to the station or sun-skimming. NOw I thought, of course it's possible that the player flies around a little more, and eventually meets both henchmen, resulting in two Hacker Outposts being created (possibly in the same place). Not good.
A complete other approach would be to check if the hacker outpost already exists. If yes, the henchman just switches to pirateAI and doesn't do anything.

And what is also possible is to set a variable in the worldscript with worldScripts[anarchies].variable. This way ships can see if others have already done something special. I currently use this in the new buoyRepair.oxp to keep count of special added ships.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Post by Commander McLane »

Yes, but that would require continuous checking for the Hacker Outpost. As it is working now, I am happy. The player will never notice the removal of the ship, as he is (by definition) someplace else.
Post Reply