Beacon target setting

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

Moderators: winston, another_commander

Post Reply
User avatar
jnorichards
Average
Average
Posts: 15
Joined: Mon May 24, 2010 7:30 pm

Beacon target setting

Post by jnorichards »

Hi, I'm debugging an OXP that my son is writing; he's new to scripting, and my Javascript isn't wonderful, but we've found behaviour in Oolite which is, umm, non-intuitive. I expect it's me that can't see the obvious...

The intention is to label a non-player ship with a beacon, which he's doing with

Code: Select all

var beacon = system.addShips("x__beacon",1,x_ship.position,50);
beacon.primaryRole = "x__beacon"; //set explicitly; is this necessary?
beacon.position = x_ship.position.add(x_ship.heading.multiply(x_ship.collisionRadius+1)); //shift the beacon outside the target's collision zone 
log(this.name, "x_spawnBeacon spawned beacon for " + x_ship.displayName);
beacon.target = x_ship;
log(this.name, "x_spawnBeacon assigned a target to it of " + beacon.target);

The log shows that the beacon target has indeed been assigned:
  • 16:14:17.027 [x_Loc]: x_spawnBeacon spawned beacon for Chopped Cobra
    16:14:17.027 [x_Loc]: x_spawnBeacon assigned a target to it of [Ship "Chopped Cobra" position: (5609.6, 11493.7, 20624.9) scanClass: CLASS_CARGO status: STATUS_IN_FLIGHT]
On a recurring eight-second timer, a scan is made for the beacons:

Code: Select all

var beacons = system.shipsWithPrimaryRole("x__beacon");
That works, producing the expected number of beacons. However, each element of the beacons array always comes back with .target === null. Nowhere in the code is beacon.target manipulated other than initially to assign it.
Surely the beacon ought to remember its target for eight seconds? Any insights gratefully accepted!

Jonathan

EDIT: OK, I've seen it. system.addShips() returns *an array*, so the target assignment is going astray: it should be beacon[0].target = x_ship;

I'll leave this post here as a memorial to the invisibility of the obvious... with hindsight!
Kubuntu 12.04 on Asus A7N8X with Creative GeForce FX 5600
Cdr Trubshaw is currently flying test sorties in a Python within G3
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Beacon target setting

Post by Smivs »

jnorichards wrote:
Hi, I'm debugging an OXP that my son is writing...
Cool! My son is only three, but I'm sure he'll go over to the 'Dark Side' when he's a bit older. Give your lad a cookie :)
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
jnorichards
Average
Average
Posts: 15
Joined: Mon May 24, 2010 7:30 pm

OT Re: Beacon target setting

Post by jnorichards »

Smivs wrote:
Cool! My son is only three, but I'm sure he'll go over to the 'Dark Side' when he's a bit older. Give your lad a cookie :)
:) When he gets home, perhaps. I was debugging 'cos I'm retired and although the problem was driving him nuts, he had to go out to work!

Further OT: your three-year-old will grow up with computing all around him, and things ought to be easier for the coming generation, except that systems are so complicated these days. It was possible for ordinary mortals to understand how a ZX81 or a Commodore 64 worked. The Raspberry Pi has the ambition of bringing hands-on computing back into the realms of the accessible; I got one for Christmas and I have to say that the documentation for the hardware is a LOT tougher than the C64 stuff I remember. Mind, the C64 wouldn't run Debian...
Kubuntu 12.04 on Asus A7N8X with Creative GeForce FX 5600
Cdr Trubshaw is currently flying test sorties in a Python within G3
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Beacon target setting

Post by Thargoid »

If it makes you (and he) feel any better, I can put my hand up to falling over that one myself in the past. Maybe we should start a sticky thread for such things, or perhaps use the scripters cove for such tips and findings.
User avatar
jnorichards
Average
Average
Posts: 15
Joined: Mon May 24, 2010 7:30 pm

Re: Beacon target setting

Post by jnorichards »

Thargoid wrote:
Maybe we should start a sticky thread for such things, or perhaps use the scripters cove for such tips and findings.
It would help if I followed the old, old adage of RTFM. I started off trying to assign ownership of the beacon to the ship, until I read the wiki again and noted that ship.owner is read-only...
The trouble is that Oolite has a really rich set of objects and methods, and the learning curve is pretty steep, even if you mercilessly borrow from other authors! I'm sure we haven't complied with half of the OXP Good Practice guidelines.
Kubuntu 12.04 on Asus A7N8X with Creative GeForce FX 5600
Cdr Trubshaw is currently flying test sorties in a Python within G3
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Beacon target setting

Post by Thargoid »

Sounds like you'll fit right in with the rest of us then ;)
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: OT Re: Beacon target setting

Post by Smivs »

jnorichards wrote:
Smivs wrote:
Cool! My son is only three, but I'm sure he'll go over to the 'Dark Side' when he's a bit older. Give your lad a cookie :)
:) When he gets home, perhaps. I was debugging 'cos I'm retired and although the problem was driving him nuts, he had to go out to work!
:oops: :lol:
Commander Smivs, the friendliest Gourd this side of Riedquat.
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Beacon target setting

Post by Switeck »

jnorichards wrote:
EDIT: OK, I've seen it. system.addShips() returns *an array*, so the target assignment is going astray: it should be beacon[0].target = x_ship;

I'll leave this post here as a memorial to the invisibility of the obvious... with hindsight!
Another thing you can do is use:
var beacon = system.addShips("x__beacon",1,x_ship.position,-50)[0];
(-50 since if it's behind the ship, the ship shouldn't run into it.)

...Then you don't need to do beacon[0].whatever to call up information about what should be 1 beacon.

Assuming there's supposed to be only 1 beacon ever, just check that beacon is not dead (I forget the beacon.extension you'll need for this!) instead of using:
var beacons = system.shipsWithPrimaryRole("x__beacon");
...all the time.
User avatar
jnorichards
Average
Average
Posts: 15
Joined: Mon May 24, 2010 7:30 pm

Re: Beacon target setting

Post by jnorichards »

Switeck wrote:
Another thing you can do is use:

Code: Select all

var beacon = system.addShips("x__beacon",1,x_ship.position,-50)[b][0][/b]; //(-50 since if it's behind the ship, the ship shouldn't run into it.)
...Then you don't need to do beacon[0].whatever to call up information about what should be 1 beacon.
Ah, useful, thanks.
Assuming there's supposed to be only 1 beacon ever, ...
Well, there's only one beacon per ship, but there might very well be more than one x_ship that needs to be assigned a beacon. The current strategy is to find all the beacons, read the targets of each, and then the x_ship candidates left over are the ones that need new beacons spawning for them. We've cannibalised things from Escape Capsule Locator, Trackers, and others. Thanks, guys. My most-used command at the moment:

Code: Select all

jonathan@Odin:~/.Oolite/AddOns$ find . -name '*.js' | while read f; do grep -iH 'latest_puzzle' "$f"; done
:)
Kubuntu 12.04 on Asus A7N8X with Creative GeForce FX 5600
Cdr Trubshaw is currently flying test sorties in a Python within G3
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Beacon target setting

Post by Thargoid »

It also implicity presumes that the target ship (the one you're adding the beacon to) exists. But that would presumably be checked in a previous line of the code.

Plus also it presumes that the beacon itself can be added successfully. If not then the output of the addShips will be null, and you'll throw an error when you try and refer to the properties of the first element in the array, as it won't exist.

By the way, if you want an example of adding beacons to things, look at my Tracker OXP (although that requires trunk 1.77).
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: Beacon target setting

Post by Eric Walch »

jnorichards wrote:
We've cannibalised things from Escape Capsule Locator, Trackers, and others.
Or you could make an oxp that only works from 1.77 onwards. In that version will beaconCode become a writable ship property. So no need to spawn a special entity for that purpose. :lol:

But, than you don't get the benefit of learning the more complex code you use now. :P
User avatar
Svengali
Commander
Commander
Posts: 2370
Joined: Sat Oct 20, 2007 2:52 pm

Re: Beacon target setting

Post by Svengali »

Eric Walch wrote:
jnorichards wrote:
We've cannibalised things from Escape Capsule Locator, Trackers, and others.
Or you could make an oxp that only works from 1.77 onwards. In that version will beaconCode become a writable ship property.
But be aware that changing beaconCodes of other OXPs entities breaks AIs (targetFirstBeaconWithCode, targetNextBeaconWithCode).
User avatar
Tricky
---- E L I T E ----
---- E L I T E ----
Posts: 821
Joined: Sun May 13, 2012 11:12 pm
Location: Bradford, UK. (Anarchic)

Re: Beacon target setting

Post by Tricky »

jnorichards wrote:
Thanks, guys. My most-used command at the moment:

Code: Select all

jonathan@Odin:~/.Oolite/AddOns$ find . -name '*.js' | while read f; do grep -iH 'latest_puzzle' "$f"; done
:)
OT: That can be simplifed with the -exec argument of find.

Code: Select all

find . -type f -name '*.js' -exec grep -iH 'latest_puzzle' {} \;
User avatar
jnorichards
Average
Average
Posts: 15
Joined: Mon May 24, 2010 7:30 pm

Re: Beacon target setting

Post by jnorichards »

Tricky wrote:
OT: That can be simplifed with the -exec argument of find.

Code: Select all

find . -type f -name '*.js' -exec grep -iH 'latest_puzzle' {} \;
Yeah, I struggle with find -exec; sometimes it doesn't do what I expect, especially in the context of filenames containing spaces [1]; that's why I'm piping it to a while loop, and putting the filename in double quotes.
Svengali wrote:
But be aware that changing beaconCodes of other OXPs entities breaks AIs (targetFirstBeaconWithCode, targetNextBeaconWithCode)
Not a problem for this instance, where we're only dealing with beacons created by this OXP. At first glance, scripting for 1.77 is going to be a different ball-game...

Jonathan

[1] The work of :twisted: Satan, IMHO !!
Kubuntu 12.04 on Asus A7N8X with Creative GeForce FX 5600
Cdr Trubshaw is currently flying test sorties in a Python within G3
Post Reply