Page 1 of 2

Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 9:14 am
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 10:30 am
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 10:55 am
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 11:10 am
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 11:27 am
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 11:40 am
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 12:29 pm
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 12:43 pm
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 12:45 pm
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!

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 12:47 pm
by m4r35n357
Ahruman wrote:
Fixed, download it again.
OK will do, thanks.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 12:57 pm
by m4r35n357
Ahruman wrote:
Fixed, download it again.
Still says the same thing . . . ?????
Ian.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 1:10 pm
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?

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 1:42 pm
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;
			}
		}
	}

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 1:54 pm
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.

Re: Newbie help with Oolite Js API

Posted: Fri Apr 08, 2011 1:57 pm
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?