Page 111 of 118

Re: Scripters cove

Posted: Sat Dec 03, 2022 7:24 pm
by Alnivel
Reval wrote: Sat Dec 03, 2022 2:47 pm
Question: is it possible to access a variable from another OXP's world-script?

If so, what is the syntax for doing this? (could someone perhaps give me an example?)
Besides the the global and mission variables, mentioned by Redspear, there is another way. To use it variable that you want to access should be property of "this" of the world-script:

Code: Select all

this.name = "world-script-A";

this.$variable = 42; // the variable you want to access
And then in the place where you access $variable use:

Code: Select all

var variable = worldScripts["world-script-A"].$variable; // read 
worldScripts["world-script-A"].$variable = 123; // write
Note: If you're going to be accessing multiple variables, or do it frequently, then it probably makes sense to store the script object once instead of retrieving it each time.

Re: Scripters cove

Posted: Sun Dec 04, 2022 6:49 am
by Reval
Many thanks, Alnivel - working just as advertised :)

Re: Scripters cove

Posted: Mon Dec 05, 2022 3:54 pm
by Redspear

Code: Select all

if (System.infoForSystem(galaxyNumber, player.ship.targetSystem).ID < 86) {
        ...first code thing
	}
	else {
		if (System.infoForSystem(galaxyNumber, player.ship.targetSystem).ID < 171) {	
		...second
		}
		else {	
			...third
			}
		}		

Another cry for help...

Only the third of these options is ever firing.

Lave, for example, is ID #7 and so should trigger the first but it triggers the third instead :?
It works if I swap ID for economy for example (with different numbers of course).

That it triggers at all suggest that my use of '.ID' is ok but why no discrimination upon calling the value (of ID)?

Any advice please?

Re: Scripters cove

Posted: Mon Dec 05, 2022 4:38 pm
by Alnivel
Redspear wrote: Mon Dec 05, 2022 3:54 pm

Code: Select all

if (System.infoForSystem(galaxyNumber, player.ship.targetSystem).ID < 86) {
        ...first code thing
	}
	else {
		if (System.infoForSystem(galaxyNumber, player.ship.targetSystem).ID < 171) {	
		...second
		}
		else {	
			...third
			}
		}		

Another cry for help...

Only the third of these options is ever firing.

Lave, for example, is ID #7 and so should trigger the first but it triggers the third instead :?
It works if I swap ID for economy for example (with different numbers of course).

That it triggers at all suggest that my use of '.ID' is ok but why no discrimination upon calling the value (of ID)?

Any advice please?
System.infoForSystem returns a dictionary from planetinfo.plist, not a System object. The dictionary does not have an ID property, so the result is undefined. Is undefined lesser than 86? JS thinks not. Is undefined lesser than 171? Same, so the third branch is being executed.

Why are you even trying to get an ID through the system information if you already have one? You can just use it like this:

Code: Select all

if (player.ship.targetSystem < 86) {
        ...first code thing
}
else {
	if (player.ship.targetSystem < 171) {	
		...second
	}
	else {	
		...third
	}
}

Re: Scripters cove

Posted: Mon Dec 05, 2022 4:54 pm
by Redspear
Alnivel wrote: Mon Dec 05, 2022 4:38 pm
Why are you even trying to get an ID through the system information if you already have one?
Damn fine question...

I changed strategy for another batch of code but forgot to change back :oops:

Alnivel wrote: Mon Dec 05, 2022 4:38 pm
Is undefined lesser than 86? JS thinks not. Is undefined lesser than 171? Same, so the third branch is being executed.
It was nothing showing up in the log file that threw me on that score: 'undefined' being a familiar occurrence.

In any case, your example makes perfect sense so I'll try that. Thanks :)

Re: Scripters cove

Posted: Mon Dec 05, 2022 5:51 pm
by Alnivel
I have a few questions:

1. Is it possible to somehow use scriptedAI along with PriorityAI?
2. Is there a handler that gets called when the cargo in cargo hold is destroyed? Or how else to track it?
3. How to find out if a certain OXP is installed if it doesn't have any worldscript? More specifically, I'm want to check retexture OXPs, addition versions. The only way I see is to use Ship.shipDataForKey, but maybe I'm missing something obvious?

The questions are not related to each other, I'm just asking them in bulk.

Re: Scripters cove

Posted: Mon Dec 05, 2022 6:16 pm
by Redspear
Alnivel wrote: Mon Dec 05, 2022 5:51 pm
2. Is there a handler that gets called when the cargo in cargo hold is destroyed? Or how else to track it?
Maybe

// establish cargo count in hold

this.shipTakingDamage = function(amount, whom, type)
{
// test if cargo count has decreased
}

Re: Scripters cove

Posted: Mon Dec 05, 2022 7:32 pm
by Massively Locked
Alnivel wrote: Mon Dec 05, 2022 5:51 pm
3. How to find out if a certain OXP is installed if it doesn't have any worldscript? More specifically, I'm want to check retexture OXPs, addition versions. The only way I see is to use Ship.shipDataForKey, but maybe I'm missing something obvious?
This is a bit clunky, but it seems to do the job. I'm using Griff's Fer-de-Lance as an example.

Code: Select all

const oxzList = oolite.resourcePaths
for (let kowntr = 0; kowntr < oxzList.length; kowntr++)
	if (oxzList [kowntr].indexOf ("oolite.oxp.Griff.Ferdelance.oxz") > -1)
		log (name, oxzList [kowntr] + " is installed")
Forgot one thing- you might want to add a "break" at the end of the IF statement since oxzList tends to be lengthy.

Re: Scripters cove

Posted: Mon Dec 05, 2022 7:36 pm
by Massively Locked
I've been thinking about adding a wiki page regarding things in "regular" Javascript that are NOT available in Oolite Javascript. I've run across a handful. They're mainly string methods: includes, startsWith, endsWith, replaceAll. Also some techniques for deep copying don't seem to be implemented.

So…

1. Worthwhile or a waste of time & space?
2. If worthwile- do you know of any moar? And please correct me if I'm wrong about those I listed. Also, if someone has already done this somewhere, point me to it.

Re: Scripters cove

Posted: Tue Dec 06, 2022 9:09 am
by Switeck
Massively Locked wrote: Mon Dec 05, 2022 7:32 pm
Alnivel wrote: Mon Dec 05, 2022 5:51 pm
3. How to find out if a certain OXP is installed if it doesn't have any worldscript? More specifically, I'm want to check retexture OXPs, addition versions. The only way I see is to use Ship.shipDataForKey, but maybe I'm missing something obvious?
This is a bit clunky, but it seems to do the job. I'm using Griff's Fer-de-Lance as an example.

Code: Select all

const oxzList = oolite.resourcePaths
for (let kowntr = 0; kowntr < oxzList.length; kowntr++)
	if (oxzList [kowntr].indexOf ("oolite.oxp.Griff.Ferdelance.oxz") > -1)
		log (name, oxzList [kowntr] + " is installed")
Forgot one thing- you might want to add a "break" at the end of the IF statement since oxzList tends to be lengthy.
I keep a lot of older versions of OXPs and OXZs.
To keep track of them, I often rename the original files from "Whatever.oxz" to "Whatever v1.2.oxz" (in the case of version 1.2).

Would your logic still work with the renamed files?

Re: Scripters cove

Posted: Tue Dec 06, 2022 9:21 am
by Reval
Massively Locked wrote: Mon Dec 05, 2022 7:36 pm
I've been thinking about adding a wiki page regarding things in "regular" Javascript that are NOT available in Oolite Javascript. I've run across a handful. They're mainly string methods: includes, startsWith, endsWith, replaceAll. Also some techniques for deep copying don't seem to be implemented.

So…

1. Worthwhile or a waste of time & space?
2. If worthwile- do you know of any moar? And please correct me if I'm wrong about those I listed. Also, if someone has already done this somewhere, point me to it.
Definitely worthwhile! - if you have the time. I for one would appreciate it greatly, because I've frequently run across methods from my 'standard javascript manual' that don't work at all in 'Oolite JS'.

Re: Scripters cove

Posted: Tue Dec 06, 2022 1:59 pm
by Alnivel
Redspear wrote: Mon Dec 05, 2022 6:16 pm
Alnivel wrote: Mon Dec 05, 2022 5:51 pm
2. Is there a handler that gets called when the cargo in cargo hold is destroyed? Or how else to track it?
Maybe

// establish cargo count in hold

this.shipTakingDamage = function(amount, whom, type)
{
// test if cargo count has decreased
}
This should work for me, thanks!
Massively Locked wrote: Mon Dec 05, 2022 7:32 pm
Alnivel wrote: Mon Dec 05, 2022 5:51 pm
3. How to find out if a certain OXP is installed if it doesn't have any worldscript? More specifically, I'm want to check retexture OXPs, addition versions. The only way I see is to use Ship.shipDataForKey, but maybe I'm missing something obvious?
This is a bit clunky, but it seems to do the job. I'm using Griff's Fer-de-Lance as an example.

Code: Select all

const oxzList = oolite.resourcePaths
for (let kowntr = 0; kowntr < oxzList.length; kowntr++)
	if (oxzList [kowntr].indexOf ("oolite.oxp.Griff.Ferdelance.oxz") > -1)
		log (name, oxzList [kowntr] + " is installed")
Forgot one thing- you might want to add a "break" at the end of the IF statement since oxzList tends to be lengthy.
Thank you! It seems like it can only be fully relied upon for managed OXPs, but it's still useful to have such a method even for others as a last resort.

Re: Scripters cove

Posted: Tue Dec 06, 2022 7:52 pm
by Massively Locked
Switeck wrote: Tue Dec 06, 2022 9:09 am
I keep a lot of older versions of OXPs and OXZs.
To keep track of them, I often rename the original files from "Whatever.oxz" to "Whatever v1.2.oxz" (in the case of version 1.2).

Would your logic still work with the renamed files?
Sure- as long as the oxz has a valid filename and is loaded by Oolite. "Whatever v1.2.oxz" is fine. On the other hand, "Whatever v1.2.wip" won't be loaded and therefore not included in the resourcePaths property.

Re: Scripters cove

Posted: Tue Dec 06, 2022 7:55 pm
by Massively Locked
Reval wrote: Tue Dec 06, 2022 9:21 am
Definitely worthwhile! - if you have the time. I for one would appreciate it greatly, because I've frequently run across methods from my 'standard javascript manual' that don't work at all in 'Oolite JS'.
Cool- I'll comb thru my notes to see if I logged any others. You obviously have felt the same frustration I have in finding just the right method only to have Oolite thumb its nose when you try to use it. :evil: Anyway, I'll put up the page sometime in the next few days- the bulk of my "Oolite time" is currently spent on LitF+.

Re: Scripters cove

Posted: Tue Dec 06, 2022 7:58 pm
by Massively Locked
Alnivel wrote: Tue Dec 06, 2022 1:59 pm
It seems like it can only be fully relied upon for managed OXPs…
Do you mean that it doesn't work for OXPs in the AddOns dir? I have my Station Bulletins OXP in AddOns, and it's picked up. I also have another OXP in there which is deliberately disabled and doesn't show up.