Page 1 of 1
Beacon target setting
Posted: Thu Jan 03, 2013 5:41 pm
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!
Re: Beacon target setting
Posted: Thu Jan 03, 2013 6:15 pm
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
OT Re: Beacon target setting
Posted: Thu Jan 03, 2013 8:55 pm
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...
Re: Beacon target setting
Posted: Thu Jan 03, 2013 8:56 pm
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.
Re: Beacon target setting
Posted: Thu Jan 03, 2013 9:17 pm
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.
Re: Beacon target setting
Posted: Thu Jan 03, 2013 9:27 pm
by Thargoid
Sounds like you'll fit right in with the rest of us then
Re: OT Re: Beacon target setting
Posted: Thu Jan 03, 2013 9:29 pm
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!
Re: Beacon target setting
Posted: Thu Jan 03, 2013 11:21 pm
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.
Re: Beacon target setting
Posted: Thu Jan 03, 2013 11:57 pm
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
Re: Beacon target setting
Posted: Fri Jan 04, 2013 7:51 am
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).
Re: Beacon target setting
Posted: Fri Jan 04, 2013 7:53 am
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.
But, than you don't get the benefit of learning the more complex code you use now.
Re: Beacon target setting
Posted: Fri Jan 04, 2013 2:04 pm
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).
Re: Beacon target setting
Posted: Sat Jan 05, 2013 7:16 pm
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' {} \;
Re: Beacon target setting
Posted: Sun Jan 06, 2013 5:03 pm
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
Satan, IMHO !!