Join us at the Oolite Anniversary Party -- London, 7th July 2024, 1pm
More details in this thread.

Newbie help with Oolite Js API

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Newbie help with Oolite Js API

Post by m4r35n357 »

I'm new both to Javascript and the Oolite API, and am having an issue with

Code: Select all

system.shipsWithRole("blah")
(also shipsWithPrimaryRole), it always returns an empty array when I call it - I'm trying to access things created with

Code: Select all

system.addShips("blah", count, offset,radius)
from a different OXP.

I inferred from the documentation that this is possible, but on grepping my entire AddOns directory I can only see examples of its use from within the same OXP. Can anyone put me straight on this? I was under the impression that the system object is shared, and all OXPs should see the same view . . . . ? TIA,

Ian.
User avatar
Svengali
Commander
Commander
Posts: 2370
Joined: Sat Oct 20, 2007 2:52 pm

Re: Newbie help with Oolite Js API

Post by Svengali »

You can also create this array on spawning

Code: Select all

this.myShips = system.addShips("blah", count, position, radius);
Check if the ship is/ships are there by creating a dump (pause and press 0) and check the log. If it's not there check the roles and the values of count, position and radius.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Svengali wrote:
You can also create this array on spawning

Code: Select all

this.myShips = system.addShips("blah", count, position, radius);
Check if the ship is/ships are there by creating a dump (pause and press 0) and check the log. If it's not there check the roles and the values of count, position and radius.
Sorry if I wasn't clear, I'm know the ships are there, someone else has already created them - it's the

Code: Select all

system.shipsWithRole("blah")
that I can't get to work.

Ian.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Newbie help with Oolite Js API

Post by JensAyton »

m4r35n357 wrote:
I inferred from the documentation that this is possible, but on grepping my entire AddOns directory I can only see examples of its use from within the same OXP. Can anyone put me straight on this? I was under the impression that the system object is shared, and all OXPs should see the same view . . . . ?
Correct.

I’m with Svengali on this one; the most likely explanations are that you’re calling shipsWithRole() before the other OXP has actually added anything, or that you’re misspelling the role. That the function works can easily be verified by typing system.shipsWithRole("trader") in the [EliteWiki] debug console.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Ahruman wrote:
I’m with Svengali on this one; the most likely explanations are that you’re calling shipsWithRole() before the other OXP has actually added anything, or that you’re misspelling the role. That the function works can easily be verified by typing system.shipsWithRole("trader") in the [EliteWiki] debug console.
I concur that the order of execution may be significant, not sure what I can do about this, I'm currently using shipExitedWitchspace to allow for other OXPs to do their stuff first in eg. shipWillExitWitchspace . . . not brilliant - is there a better way? I'm not misspelling the role (unless I'm misspelling twice with roles from 2 different OXPs, but I am cut&pasting) so it looks like I need to make my code a bit cleverer, or just loop & wait (?!)

Is there any existing code I can refer to? Otherwise, it's back to the drawing board . . . Thanks, Ian.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Ahruman wrote:
That the function works can easily be verified by typing system.shipsWithRole("trader") in the [EliteWiki] debug console.
BTW http://jens.ayton.se/oolite/files/debug-oxp-1.75.1.zip says it is incompatible with Oolite 1.75.1 . . . . !
Ian.
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: Newbie help with Oolite Js API

Post by Eric Walch »

m4r35n357 wrote:
I'm currently using shipExitedWitchspace to allow for other OXPs to do their stuff first in eg. shipWillExitWitchspace . . . not brilliant - is there a better way?

Is there any existing code I can refer to? Otherwise, it's back to the drawing board . . . Thanks, Ian.
The easiest way is using a timer, like in rockHermitLocator.oxp:

Code: Select all

this.shipExitedWitchspace = function ()
{
    // use a delay to allow all other oxp's to add their stuff before we start counting hermits.
    this.buoyTimer = new Timer(this, this.addBuoys, 0.90);
}
Than in this.addBuoys() the code is executed. For the timer is any value > 0 good enough. During the handler is the clock halted and starts ticking again after all oxps have executed their this.shipExitedWitchspace() code. So, when the timer starts the function, the ships you are looking for are added by that other oxp, no matter in which order they executed.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Newbie help with Oolite Js API

Post by JensAyton »

m4r35n357 wrote:
Ahruman wrote:
That the function works can easily be verified by typing system.shipsWithRole("trader") in the [EliteWiki] debug console.
BTW http://jens.ayton.se/oolite/files/debug-oxp-1.75.1.zip says it is incompatible with Oolite 1.75.1 . . . . !
Ian.
Fixed, download it again.
Eric Walch wrote:
For the timer is any value > 0 good enough.
0 should be fine too; it will fire in the next frame.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

[quote="Eric Walch"]The easiest way is using a timer, like in rockHermitLocator.oxp:/quote]
Thanks v. much Eric, I think that will help a lot!
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Ahruman wrote:
Fixed, download it again.
OK will do, thanks.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Ahruman wrote:
Fixed, download it again.
Still says the same thing . . . ?????
Ian.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Eric Walch wrote:
The easiest way is using a timer, like in rockHermitLocator.oxp:

Code: Select all

this.shipExitedWitchspace = function ()
{
    // use a delay to allow all other oxp's to add their stuff before we start counting hermits.
    this.buoyTimer = new Timer(this, this.addBuoys, 0.90);
}
I notice the function takes no parameters; my function will need to, is this supported, or will I need to use "this" to pass them?
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: Newbie help with Oolite Js API

Post by Lone_Wolf »

m4r35n357 wrote:
I notice the function takes no parameters; my function will need to, is this supported, or will I need to use "this" to pass them?
You can use parameters in your own functions, here's an example from IronHide oxp by thargoid :

Code: Select all

this.choice = function(choice)
	{
	switch(choice)
		{
		case "IRONHIDE_1_ACCEPT":
			{
			player.credits -= missionVariables.ironHide_cost;
			missionVariables.ironHide_percentage = 100;
			break;
			}

		case "IRONHIDE_1_RENOVATION_ACCEPT":
			{
			player.credits -= missionVariables.ironHide_cost;
			missionVariables.ironHide_percentage = 100;
			break;
			}	
		case "IRONHIDE_2_REJECT":
			{
			break;
			}
		}
	}
OS : Arch Linux 64-bit - rolling release

OXPs : My user page

Retired, reachable at [email protected]
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Newbie help with Oolite Js API

Post by Thargoid »

A fairly simple example to review is the Cargo Shepherd I just released.

That scans for cargopods (by role and scanclass) and if it finds them it acts on them. And it's a very simple and small script. Have a look at that and see if it helps you.
m4r35n357
---- E L I T E ----
---- E L I T E ----
Posts: 296
Joined: Wed Jan 19, 2011 4:00 pm

Re: Newbie help with Oolite Js API

Post by m4r35n357 »

Lone_Wolf wrote:
You can use parameters in your own functions, here's an example from IronHide oxp by thargoid :
Hmm, not sure what you mean - how would you call that function (with a choice parameter) from new Timer?
Post Reply