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. 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.
Weird problem with using arrays in .js
Moderators: winston, another_commander, Getafix
Re: Weird problem with using arrays in .js
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]
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]
OXPs : My user page
Retired, reachable at [email protected]
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Weird problem with using arrays in .js
The
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”).
+
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”).
E-mail: [email protected]
Re: Weird problem with using arrays in .js
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
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