Mauiby de Fug wrote:Although this line is both logged, and implemented, at the same time, which seems strange to me.
Code: Select all
log(this.ship.exitSystem(jumpChain.route[chainPos]));
log() is a normal function. In a function call expression, the expressions making up the arguments are evaluated and the result passed to the function. In this case,
jumpChain.route[chainPos] is evaluated (retrieving a value from the array), and passed to
this.ship.exitSystem(). This returns
true, which is then passed to
log().
Mauiby de Fug wrote:Also, feel free to tell me if there's any bad scripting in there, or better ways to achieve the required result! I mainly worked this out to learn, after all...
The values
jumpChain and
chainPos are declared as vars in global scope, which makes the properties of the global object, which is shared between all scripts. (This is a known deficiency in the JavaScript language itself.) They should either be made properties of
this (preferably with a $ or _ in front, as they’re internal), or enclosed in a closure, which looks like this:
Code: Select all
(function () {
var jumpChain;
var chainPos;
this shipSpawned = …
/* rest of code*/
}).call(this);
In this approach, only the code within the anonymous outer function can see the variables, but they continue to exist when the callbacks are called.
You mix
PlayerShip with
player.ship. The preferred usage is
player.ship. Accessing properties of
PlayerShip could stop working in future. (Pretend it isn’t there.) In the same way, System.ID should be system.ID.
(
PlayerShip and
System are the “prototypes” of player.ship and system. In general, the prototype defines methods and properties that are shared by all objects of a certain type. For instance, the Vector3D prototype has the
add() method, and every vector inherits it – unless a particular vector is given an
add() method of its own. However, you can’t usefully call
Vector3D.add() since there’s no actual vector data attached to the prototype. Conceptually, the same is true of
PlayerShip, but since there’s only one player ship some of the methods use it directly instead of extracting it from the JavaScript “this” parameter, so calling them on the prototype works. You’ll get bitten if you try to use an inherited method or property, like
PlayerShip.position, which is
undefined. The situation for
System is a bit more complex, but the convention is that you use lowercase
system to refer to the current system, and uppercase
System for methods that refer to
other systems – namely those listed under
Static Methods in the wiki.)
Oh, and instead of
System.infoForSystem(galaxyNumber, system.ID) you can use
system.info (which is the info for the current system).