Page 2 of 2
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 1:59 pm
by m4r35n357
Thargoid wrote: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.
OK will download & have a look, thanks.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 2:03 pm
by Thargoid
I'm not sure you can pass a parameter directly on a timer call to a function. You may need to store it away in a script variable (this.something = whatever) and then refer to that in your called function.
I have a memory of having this issue myself in the past. My IronHide code you quoted is the callback function from a mission screen call, which is a slightly different case.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 2:08 pm
by m4r35n357
Thargoid wrote:I'm not sure you can pass a parameter directly on a timer call to a function. You may need to store it away in a script variable (this.something = whatever) and then refer to that in your called function.
I have a memory of having this issue myself in the past. My IronHide code you quoted is the callback function from a mission screen call, which is a slightly different case.
That sounds right to me, I'm looking at Cargo Shepherd right now, I hope that will clue me in on something I can use. Thanks, Ian.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 2:17 pm
by m4r35n357
Ahruman wrote:Fixed, download it again.
Edited requires.plist, now working, thanks.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 2:48 pm
by m4r35n357
m4r35n357 wrote:Thargoid wrote:I'm not sure you can pass a parameter directly on a timer call to a function. You may need to store it away in a script variable (this.something = whatever) and then refer to that in your called function.
Yes, that is exactly how I am now using the timer, and it works! I need to check up on the precise semantics and timing(!) of the Timer, but basically I'm where I needed to be to proceed, so thanks to everyone who helped, Ian.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 3:40 pm
by JensAyton
Thargoid wrote:I'm not sure you can pass a parameter directly on a timer call to a function.
You can’t, but you don’t need to, because JavaScript functions “close over” variables in their declared scope. For example:
Code: Select all
var x = 5;
this.t = new Timer(this, function() { log(x); }, 0); // Will log 5
Or, equivalently:
Code: Select all
function timerFunction(n)
{
log(n);
}
var x = 5;
this.t = new Timer(this, function() { timerFunction(x); }, 0);
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 5:46 pm
by m4r35n357
I think the latter is what I want to do, eventually (I already have something that works so I will take a little time over it). Thanks for the tip.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 6:04 pm
by m4r35n357
OK, it just worked first time, didn't even need variables, just passed literals to the "inner" function from the outer. Very nice Javascript lesson!
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 6:16 pm
by TGHC
round of applause to everyone, more jelly babies.
Re: Newbie help with Oolite Js API
Posted: Fri Apr 08, 2011 6:56 pm
by Okti
TGHC wrote:round of applause to everyone, more jelly babies.