Oolite Javascript basics

From Elite Wiki

Just playing around with the wiki. I may continue this, or I may return to my day job which currently consists of making smaller rocks from larger rocks & hoping that there's gold in them thar 'roids...


Square = 1

So you want to write scripts for Oolite that'll do all sorts of neat stuff, but you don't know where to start. Well, I was once like you when I started to learn Oolite Javascript and look at where I am today: a broken-down, hollow husk of a man... but that's a whole 'nother story.

The very first thing you need to know and always remember is this:

Javascript is object-oriented and (largely) event-driven

In many non-object-oriented programming languages, you use commands to do things- commands like "print". You might write something like: print "Hello World!". When you run your program, you'll be rewarded with a friendly greeting on-screen.

Things are done a little differently in Oolite Javascript because... reasons. Commands are called methods in object-oriented lingo. One of the commands (methods) to print something in Oolite is the consoleMessage method. Your print "Hello World!" will look like this:

consoleMessage ("Hello World!")

That, however, is not complete and will not work. For non-object-oriented humans, amphibians and occasional rhinoceri reading this- always remember and never forget the following: methods ALWAYS ALWAYS ALWAYS need to be attached to an object. Without it, it's like driving your car... but without your car. consoleMessage is a method of the object, player. So our example should now look like this:

player.consoleMessage ("Hello World!")

Knowing this is still not quite enough for your first script. This brings us to the next important point: Javascript is event-driven. Not only does Oolite need to know what to do (methods) and who or what wants to do it (objects), it needs to know when to do it. To this end, almost everything you write needs to be in an event handler (for further enlightenment/confusion, see here and here). Putting your commands in an event handler answers the when. Let's use the startUp event. This is the event (one of them, at least) when you start a new game or load a save file. Here's the format: this.startUp = function () {}

So the TL;DR version:

Don't do this:

consoleMessage ("Hello World!")

Don't even do this:

player.consoleMessage ("Hello World!")

Do this:

this.startUp = function () {
 player.consoleMessage ("Hello World!")
}

There- you've written your very first Oolite Javascript script! Congrats & break out the bubbly- we're gonna party like it's 2084004!

Your honor, I object...

I assume that you are a vibrant person with hopes, dreams, ambitions. Perhaps you want to rescue animals in need or maybe play the piano at Carnegie Hall or possibly become an astronaut and travel to the Moon, Mars and beyond. You've had experiences that have brought you sadness, and others that brought you joy. Perhaps you planted some roses that have bloomed magnificently. Perhaps your favorite sports team won the championship this year. Perhaps you found this nice little video game called Oolite and spent countless hours joyfully exploring its universe.

I have something to tell you, and it is a cold, hard truth:

Oolite objectifies you

Now you've been around the block once or twice. You've had relationships come and go. You may have experienced the highest highs or the lowest lows. You bear the scars to prove it all. Your life is... complicated.

But to Oolite, you are nothing moar than a bucket of properties and methods.

So how does that make you feel? Uncomfortable? Unseen? Insignificant?

Well, get over it! Oolite does this to everyone and everything: you, your shiny new Cobra, the Coriolis you're about to launch from, even the star system you're going to hot-rod around in. This is how Oolite knows you:

player.hopes = true
player.dreams = 5.6
player.ambitions = "to write some stuff in a wiki- any wiki!"
player.petCare = function (name) {dog.walk (name)}

These are the properties (a/k/a bits of info) that comprise you/your ship/any ship/station/etc. It tells Oolite all that needs to be known about you or any other object in the Ooniverse.

There are two basic things to keep in mind for any property: (1) is it read/write or read-only? and (2) what type of data can it hold? Read/write means you can directly change it; read-only is just that- you can look, but you can't touch. While there are a whole bunch of data types, the two most common that you'll probably start with are text strings and numbers.

Let's look at an actual read/write, text string property: your name. To access it, you use player.name. This is how you change it:

this.startUp = function () {
 player.name = "Shepard"
}

The status (F5) screen now displays your new name. You can use this as a start for creating multiple commanders- each with their own stats like score, credits, reputation, etc. Note that when working with text strings, always enclose them in quote marks.

Next, we'll look at another player property: contractReputation. Unlike name, contractReputation is a read-only, number data type property. The higher the number, the moar upstanding a long-hauler you are; the lower it is, the moar likely you'll run off with that cargo of evil juice for a booze-fueled tour of the galaxy. Since it's read-only, you CAN'T do this to magically repair your rep:

this.startUp = function () {
 player.contractReputation = 7
}

There are methods (I mean that literally) to adjust your reputation, but the way they work isn't intuitive so I won't go into them. Still, you can print out this property that you normally don't see in-game:

this.shipWillDockWithStation = function () {
 player.consoleMessage (player.contractReputation)
}

Now, every time you dock, you'll see your contract reputation number printed on-screen. An important thing to remember: just like with methods, anytime you refer to a property, you MUST include its object.

One moar thing- take a look at this:

player.ship.dockedStation.market.food.price

First is the player object.
Next is its property, ship which is also an object.
Next is its property, dockedStation which is also also an object.
Next is its property, market which is also also also an object.
Next is its property, food which is also also also also an object.
Last is its property, price which is not an object- just a humble number.
The point here is that objects can be properties. Think Russian nesting dolls.

Hopefully, all this objectification hasn't been too soul-crushing for you. As for me, I'm going to go outside, smell some roses, feel the grass beneath my feet and a cool breeze on my face.