Page 1 of 1

Can't spawn missile in script

Posted: Fri Oct 21, 2011 6:23 am
by Joshua Calvert
Greetings all,

I have the latest and greatest version of Oolite (1.75.3). I am working on my own OXP. I am using railgun.oxp as a base (copy 'n paste and edit job).
Instead of spawning a railgun projectile I wish to spawn a missile, so I've edited the code to look like:

Code: Select all

        
        this.missile = player.ship.spawnOne("rmb-intercept-missile");
	if(!!this.missile){
		player.consoleMessage("Intercept Missile Spawned OK.", 2);
	}else{
		player.consoleMessage("NOT Spawned !!!!!!!!!!!!!!!!!!!!!!", 2);
	}     
The RMB intercept missile is defined in the Missiles and Bombs OXP. The trouble is I get the NOT Spawned message so it doesn't work. So I tried to spawn a regular missile, using this:

Code: Select all

        
        this.missile = player.ship.spawnOne("missile");
	if(!!this.missile){
		player.consoleMessage("Intercept Missile Spawned OK.", 2);
	}else{
		player.consoleMessage("NOT Spawned !!!!!!!!!!!!!!!!!!!!!!", 2);
	}     
and things got really freaky; as far as I could tell it was spawning all kinds of stuff apart from missiles, including interdictor mines and thargons! :? :shock:

This is not my first time at OXP scripting and yes I've read the Javascript manual entry for spawnOne (unfortunately the docco doesn't come with examples and I find the one line "function spawnOne(role : String) : Ship" not terribly helpful). I blatantly swiped the if(!!this.missile)... construct from a website as I want to do a NULL test (yes I can google); the language I work with on a day to day basis has a completely different way of checking for NULLs (var is null, if anyone is wondering).

Anybody any helpful suggestions?

Ta,

Lagrange

Re: Can't spawn missile in script

Posted: Fri Oct 21, 2011 8:23 am
by Smivs
You would normally spawn by 'roles', so by using 'missile', yes you'd get anything that has 'missile' in its role. Have you tried spawning the override missile by role, ie 'EQ_RMB_OVERRIDE_MISSILE' ?

Re: Can't spawn missile in script

Posted: Fri Oct 21, 2011 9:32 am
by Eric Walch
Slightly related: missiles need a target or they will destroy themselves shortly after spawning. In your code, immediately after spawning, the missile should be there.
e.g. when I type: "PS.spawnOne("missile").name", the console displays the name of the missile as sign it selected one and I see briefly a missile on screen. Than it explodes and my ship receives damage.

Re: Can't spawn missile in script

Posted: Fri Oct 21, 2011 11:52 am
by DaddyHoggy
Of, the previous responders also wanted/meant to say:

"Welcome to the Friendliest Board this side of Riedquat"(tm)

Re: Can't spawn missile in script

Posted: Fri Oct 21, 2011 1:41 pm
by Svengali
Hello Joshua, welcome onboard.
Joshua Calvert wrote:
I blatantly swiped the if(!!this.missile)... construct from a website as I want to do a NULL test (yes I can google);
Checking explicitely for null is not necessary here as the property is either undefined or a array (typeof object).
A simple if(this.missile) is absolutely enough to differentiate between a spawned entity and a not spawned one.

But in case you want to check for null, do it explicitely, e.g.

Code: Select all

if(whatever === null)
Note 3x=. This is similiar to the language you are familiar with .-)
The above mentioned if(!!whatever) does not differentiate between undefined, null, false or 0 (zero) and will fail in other use-cases.