problems with variable-assigned ships...

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

Moderators: winston, another_commander, Getafix

Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

problems with variable-assigned ships...

Post by Switeck »

It's somewhat hard for me to describe this problem, so just bear with me. :(

In one of my autojumper OXPs, I use an alloy "ship" to create a wormhole to a nearby system to force the player through:

Code: Select all

this.swi1 = system.addShips("alloy", 1, player.ship.position.add(player.ship.heading.multiply(270)), 0)[0]; // was distance 150
this.swi1.fuel = 7;
this.swi1.switchAI("missileAI.plist");
this.swi1.AIState ="EXPLODE";
this.swi1.exitSystem(i);
this.swi1.remove(true);
(i = destination system.)
The alloy is spawned some 150-270 m(eters?) in front of the player's ship, so the wormhole is almost unavoidable if at full speed.

This...mostly...seems to work. If I remove the switchAI and EXPLODE ai state change, the alloy gets left in the destination systems despite the remove line...but that may not happen in v1.77 trunk betas due to change #4751: "- Don't disgorge and revive dead ships from wormholes."
http://svn.berlios.de/wsvn/oolite-linux ... 1&peg=4771

My problem arises with vaguely similar code in another OXP I'm working on. I'm wanting to assign a special ship/station to a variable and then check that variable for whether that ship exists. Here's the code I was trying:

Code: Select all

this.nullgate = system.legacy_addShipsAt("nullgate2", 1, "spu", [Math.random()*0.4-0.2, Math.random()*0.4-0.2, Math.random()*0.3+0.3]);
...
function nullgates_outboundNPCs(entity) {return entity.isShip && entity.AI == "exitingTraderAI.plist" && entity.AIState == "HEAD_AWAY_FROM_PLANET" && this.nullgate && entity.position.distanceTo(this.nullgate.position) < 300000};
...
if(this.nullgate) {
But of course it doesn't work. :cry:

this.nullgate turns up as being undefined, so I can neither check for its existence nor move it.

I thought maybe the legacy_addShipsAt was to blame, so I converted it to using the newer addShips command:

Code: Select all

this.nullgate = system.addShips("nullgate", 1, Vector3D(Math.random()*0.4-0.2, Math.random()*0.4-0.2, Math.random()*0.3+0.3).fromCoordinateSystem("spu"))[0];
...
if(this.nullgate) {
...so it resembles the "alloy" example above. Yet I still get this in Latest.log:

Code: Select all

03:55:19.796 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (NullGate 0.8.14 - for Oolite 1.75 and later): TypeError: this.nullgate is undefined
03:55:19.796 [script.javaScript.exception.unexpectedType]:       ../AddOns/NullGate v0.8.oxp/Scripts/nullgate.js, line 268.
(That's the if(this.nullgate) { line.)

Any ideas what I'm doing wrong?
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Re: problems with variable-assigned ships...

Post by Capt. Murphy »

I'd imagine that system.legacy_addShipsAt is returning an array even for a single ship if it does the same as system.addShips.

Try this.nullgate[0].


Ignore that, I'm being thick and missed the [0] at the end of the line.

If this.nullgate is not in existence at all (which your error message suggests) then the ship you are trying to spawn isn't being spawned for some reason... I'd double check the roles and such like.
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: problems with variable-assigned ships...

Post by Switeck »

The nullgate role ship is *definitely* being created -- I can target it, shoot it, and even run into it and die.
The "this.nullgate" variable (attached to the ship) is undefined.
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Re: problems with variable-assigned ships...

Post by Capt. Murphy »

What happens if you spawn it via debug console? Does system.legacy_addShipsAt("nullgate2", 1, "spu", [Math.random()*0.4-0.2, Math.random()*0.4-0.2, Math.random()*0.3+0.3]) return true?

Does nullgate = system.legacy_addShipsAt("nullgate2", 1, "spu", [Math.random()*0.4-0.2, Math.random()*0.4-0.2, Math.random()*0.3+0.3]) return the array?
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: problems with variable-assigned ships...

Post by Eric Walch »

Capt. Murphy wrote:
What happens if you spawn it via debug console? Does system.legacy_addShipsAt("nullgate2", 1, "spu", [Math.random()*0.4-0.2, Math.random()*0.4-0.2, Math.random()*0.3+0.3]) return true?

Does nullgate = system.legacy_addShipsAt("nullgate2", 1, "spu", [Math.random()*0.4-0.2, Math.random()*0.4-0.2, Math.random()*0.3+0.3]) return the array?
legacy_ commands will return null by definition. Or better: undefined, as these commands have no return value. That is the main reason for creating the new addSchips commands.
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: problems with variable-assigned ships...

Post by Switeck »

Eric Walch wrote:
legacy_ commands will return null by definition. Or better: undefined, as these commands have no return value. That is the main reason for creating the new addShips commands.
And that is why I wanted to use somevariable = system.addShips command in the first place to simplify my OXP's code. Except I'm not sure what I'm doing...my first example with "alloy" works. Yet with "nullgate", the nullgate ship spawns but doesn't seem to have a variable attached to it or its properties.
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Re: problems with variable-assigned ships...

Post by Capt. Murphy »

The role is nullgate2 in the first code snippet and nullgate in the second...... :?:

Try the system.addShips version in debug console and see what comes back.
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: problems with variable-assigned ships...

Post by Eric Walch »

I don't think it is an unknown role. In that case it would have returned null. Now it is undefined, something that should not happen with the addShips commands. But probably does when trying to use the legacy commands that way. It is just as if the legacy commands are still being used. Maybe forgtten to press the shift during startup.

Or a weird bug in the shift key. I remember one occasion were I went crazy myself. I have the setting to always rebuild the cache. One time it didn't work. The log sayed it was rebuilding but my changes were not active. Manually pressing shift or removing an oxp also didn't work. I even opened the cache file itself to see if it changed. Very weird. At the end I restarted my computer and the problem was gone and never came back.
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: problems with variable-assigned ships...

Post by Switeck »

I manually delete the cache file between runs. I deem that sufficient for a fresh start with each code change of my OXP.

The role differences of nullgate and nullgate2 are trivial -- similar ships for different conditions, both types exist so either should work.

Seems I solved my own problem.

The different code lines I've mentioned are in separate subroutines/functions of the same .js file...so apparently this.nullgate doesn't work. However removing the this. makes it...mostly work.
Now it's a global variable called just nullgate

The way it only "mostly works" is because of this section:

Code: Select all

function nullgates_inboundNPCs(entity) {return entity.isShip && entity.AI == "route2sunskimAI.plist" && entity.AIState == "HEAD_FOR_SUN" && nullgate && entity.position.distanceTo(nullgate.position) < 300000};
Which triggers this error message on start:

Code: Select all

03:45:41.656 [script.javaScript.exception.notDefined]: ***** JavaScript exception (NullGate 0.8.15 - for Oolite 1.75 and later): ReferenceError: nullgate is not defined
03:45:41.656 [script.javaScript.exception.notDefined]:       ../AddOns/NullGate v0.8.oxp/Scripts/nullgate.js, line 198.
...since (at the first time that script section runs) the nullgate doesn't exist yet and may not exist at all for most systems.

Probably coming as no surprise, but defining the nullgate global variable as null at the start of the OXP like so:

Code: Select all

this.version	= "0.8.15 - for Oolite 1.75 and later";

"use strict";

	nullgate = null;
...eliminated the "ReferenceError: nullgate is not defined" in the later "&& nullgate" line.

A workaround has been found, but it turns out this error is effectively identical to Swarm OXP ...in particular this post:
"If I'm using this.weaveX (local variable) and get a NaN location for a Thargoid, even attempting log(this.weaveX); (or similar command) in the same .js script causes the game to crash to desktop!

In later tests, I'd replaced this.weaveX/Y with just weaveX/Y (making them global variables)
...
04:06:31.187 [script.javaScript.exception.notDefined]: ***** JavaScript exception (<unidentified script>): ReferenceError: weaveX is not defined"
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Re: problems with variable-assigned ships...

Post by Capt. Murphy »

Switeck wrote:

The different code lines I've mentioned are in separate subroutines/functions of the same .js file...so apparently this.nullgate doesn't work.
If it is in the same world-script this will make the variable a property (I think that's correct terminology) of the world-script and it will be visible to all functions in that world-script, but not available to every script in Oolite which nullgate on it's own would be. Possibly bad....
Switeck wrote:
The way it only "mostly works" is because of this section:

Code: Select all

function nullgates_inboundNPCs(entity) {return entity.isShip && entity.AI == "route2sunskimAI.plist" && entity.AIState == "HEAD_FOR_SUN" && nullgate && entity.position.distanceTo(nullgate.position) < 300000};
Which triggers this error message on start:

Code: Select all

03:45:41.656 [script.javaScript.exception.notDefined]: ***** JavaScript exception (NullGate 0.8.15 - for Oolite 1.75 and later): ReferenceError: nullgate is not defined
03:45:41.656 [script.javaScript.exception.notDefined]:       ../AddOns/NullGate v0.8.oxp/Scripts/nullgate.js, line 198.
...since (at the first time that script section runs) the nullgate doesn't exist yet and may not exist at all for most systems.
I've had problems with functions defined in that format in JS strict mode not behaving as they do in non JS strict mode or throwing errors on compilation. I'm not sure why, but I think that may be the cause of the problem

With this.nullgate this should work.

Code: Select all

this.nullgates_inboundNPCs = function(entity)
{
if (!this.nullgate){return false;}
return (entity.isShip &&  entity.AI == "route2sunskimAI.plist" && entity.AIState == "HEAD_FOR_SUN"  && entity.position.distanceTo(this.nullgate.position) < 300000);
}
If you want to PM a link to your script, I am quite intrigued (both as to why it's not working and the OXP) :wink: P
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: problems with variable-assigned ships...

Post by Switeck »

The most likely cause of this variable-becoming-undefined problem is because I used a consolidated nullgate.js file -- The Null Gate ships in shipdata.plist called nullgate.js for their script and world-scripts.plist also pointed to nullgate.js.

Breaking the script up into nullgate.js and nullgate_main.js may fix this, but the complexity of the script now is such I've asked for outside help on the task. There's probably a few more surprises...I mean problems...that may either trace back to logic bugs in the script and/or bugs in Oolite. This particular OXP digs into a part of the game that I'm unaware of anyone else doing research on, so I don't have any clue what I'm likely to run into. Sadly, it's a VERY LONG ways away from done...so no sneak previews. :(
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Re: problems with variable-assigned ships...

Post by Kaks »

Ok, the explanation: each instance of the script is a different this object, so this.nullgate in the world script actually means something like worldScripts['nullgates'].nullgate, and this.nullgate inside a ship with primary role 'whatever' actually means system.shipsWithPrimaryRole['whatever'][0].script.nullgate - that is, assuming there's only one 'whatever' ship per system... :)

Yes, if you use the global namespace with a 'free standing' nullgate object it kind of works, but each instance of the script will set nullgate to null, once at startup for the world script and once every time a ship with the same js script is created.

And it does pollute the global namespace.

Instead of having it as a free standing global object, you could reference it in absolute terms ( e.g. always using worldScripts['nullgates'].nullgate ), this will eliminate the possibility of future conflicts with another oxp, and you won't need to set it to null explicitly, as long as the world script gets a chance to create it before any of the 'whatever' ships are actually added to the system.


Hope this helps,

Kaks.
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: problems with variable-assigned ships...

Post by Switeck »

Thanks Kaks, it helps me make progress.
...But I get a little further, only to run into another roadblock.

I crammed this into the script's startup to avoid later undefined errors:

Code: Select all

if(!worldScripts['nullgates'].nullgate) worldScripts['nullgates'].nullgate = 0;
And it generated an undefined error on that very line:

15:43:06.171 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (NullGate 0.8.24 - for Oolite 1.75 and later): TypeError: worldScripts.nullgates is undefined
15:43:06.171 [script.javaScript.exception.unexpectedType]: ../AddOns/NullGate v0.8.oxp/Scripts/nullgate.js, line 18.

Apparently, I'm doing something wrong again. :P

How do I do an if validation to check if a variable is undefined? :?
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:

Re: problems with variable-assigned ships...

Post by Commander McLane »

Switeck wrote:
../AddOns/NullGate v0.8.oxp/Scripts/nullgate.js, line 18.
Your script file seems to be called "nullgate.js" (singular, not plural).

So maybe also your script's name property is "nullgate", not "nullgates"?

Which would mean in code:

Code: Select all

worldScripts['nullgate']
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Re: problems with variable-assigned ships...

Post by Kaks »

Oops, my bad!
You do need to use the name defined inside the script itself - everything else being the same, if you have

Code: Select all

this.name			= "nullgates";
worldScripts['nullgates'] will be defined.

Everything here is case sensitive too, so if you have

Code: Select all

this.name			= "Nullgates";
worldScripts['nullgates'] will be undefined. The right name is - of course :!: - worldScripts['Nullgates'] - the right names are also listed, followed by their version number, inside the log's [script.load.world.listAll] section... :)
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
Post Reply