Weird problem with using arrays in .js

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

Moderators: winston, another_commander, Getafix

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

Weird problem with using arrays in .js

Post by Switeck »

I'm wanting to join together 2 arrays end-to-end as a single, continuous array...but I don't know how.

(NOTE: Hypothetical numbers are being used for the route in the arrays, to hide the location/nature of what I'm doing. 8) I did however test this a couple times with different starting locations.)
What I have is this:
jumpPath_Array = system.info.routeToSystem(System.infoForSystem(galaxyNumber,99), "OPTIMIZED_BY_TIME").route;
jumpPath_Array = jumpPath_Array + [,100,101,102];
jump_Counter = 1;

later it does:
nextStop = parseInt(jumpPath_Array[jump_Counter]);
jump_Counter = jump_Counter + 1;
(...and loops to nextStop again for each additional jump.)

Now assuming my ship is in system 98 and system 99 is a single hop away (optimized by time!)...
I expect the total array will look like this:
jumpPath_Array = [98,99,100,101,102];
And indeed when I do:
player.commsMessage("Jump Path = "+jumpPath_Array,6);
I get:
Jump Path = 98,99,100,101,102
I had to set jump_Counter to 1 instead of 0 because I found that
system.info.routeToSystem(System.infoForSystem(galaxyNumber,99), "OPTIMIZED_BY_TIME").route
...starts counting at the current system rather than the system.ID for the first jump destination.

So far so good...but now the problem.
I expect nextStop to equal 99 (the 2nd element from the array) the first time, but apparently the joined arrays are now (or always were?) a string.
...So nextStop equals 8 -- the 2nd character in the array "string".

I know I'm probably "doing it wrong", but I don't know how to correct it.
User avatar
Lone_Wolf
---- E L I T E ----
---- E L I T E ----
Posts: 546
Joined: Wed Aug 08, 2007 10:59 pm
Location: Netherlands

Re: Weird problem with using arrays in .js

Post by Lone_Wolf »

nextStop = parseInt(jumpPath_Array[jump_Counter]);

parseint converts a string to a number.

IF jumpPath_Array is indeed an array with numerical values, this should work :

nextStop = jumpPath_Array[jump_Counter]
OS : Arch Linux 64-bit - rolling release

OXPs : My user page

Retired, reachable at [email protected]
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Weird problem with using arrays in .js

Post by JensAyton »

The + operator always produces a number or a string. To join two arrays, use a = a.concat(b). (A possibly more efficient – but definitely more confusing – alternative is Array.prototype.push.apply(a, b).)

The best source of documentation for JavaScript’s built-in stuff is Mozilla’s MDC: https://developer.mozilla.org/en/JavaSc ... ects/Array (or search the tubes for “mdc array”).
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Weird problem with using arrays in .js

Post by Switeck »

Thanks to Okti and m4r35n357 help as well from IRC, I ended up using this:
jumpPath_Array = system.info.routeToSystem(System.infoForSystem(galaxyNumber,99), "OPTIMIZED_BY_TIME").route.concat([100,101,102]);

It could probably also be like this in a more simplified case:
jumpPath_Array = jumpPath_Array1.concat(jumpPath_Array2);
(Like Ahruman said above)

Also suggested to get around the 1st element being present system (and not an eligible jump destination normally) problem was:
[11:52] <m4r35n357> try array.shift
[11:52] <m4r35n357> that removes the first element
I'm assuming that means:
jumpPath_Array = jumpPath_Array.shift(1); // or -1?

I already have a simple workaround for that though...just increase the counter by 1 before starting.

...And apparently this doesn't work at all for interstellar space:
21:04:07.703 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (unknown oxp - for Oolite 1.75 and later): TypeError: System.infoForSystem(galaxyNumber, system.ID).routeToSystem(System.infoForSystem(galaxyNumber, player.ship.targetSystem), "OPTIMIZED_BY_TIME") is null
21:04:07.703 [script.javaScript.exception.unexpectedType]: ../AddOns/unknown.oxp/Scripts/unknown.js, line 129.

How do I do an arbitrary route from nearest system to player's targeted system?
jumpPath_Array = System.infoForSystem(galaxyNumber,system.ID).routeToSystem(System.infoForSystem(galaxyNumber,player.ship.targetSystem), "OPTIMIZED_BY_TIME").route;
...apparently doesn't work. :(

These websites were also helpful in figuring out various javascript functions:
http://en.wikibooks.org/wiki/JavaScript/Arrays
http://en.wikipedia.org/wiki/JavaScript_syntax
Post Reply