Page 2 of 2

Re: [WIP] Witchspace blockades

Posted: Wed Dec 11, 2013 2:50 pm
by Commander McLane
My JS-knowledge is actually quite limited, and I'm not familiar with the for (a in b) syntax.

But it seems obvious from the output that the way you've done it oSys turns out to be an integer number, not a systemInfo object (as you would actually need), or else you wouldn't get

Code: Select all

21:45:08.912 [undefined]: oSys: 0
(By the way, your script also doesn't seem to have a name property.)

So this is where you need to look.

EDIT: Yep, looking it up it appears that oSys in your syntax is only a counter, not the counted thing itself. Thus you need to make it point to the thing (the systemInfo) itself, for instance like this:

Code: Select all

this.playerStartedJumpCountdown = function(type, seconds)
{
     var aSysInRange = system.info.systemsInRange();
     for ( counter in aSysInRange )
     {
       var oSys = aSysInRange[counter];
       LogMsg( "========================================" );
       LogMsg( "System in range # " + counter );
       LogMsg( "oSys: " + oSys );
       LogMsg( "System ID: " + oSys.systemID );
       LogMsg( "System coords: " + oSys.coordinates );
       LogMsg( "System government: " + oSys.government );
       
    }
}
This should give the desired result.

EDIT2: And it does. Here's the output from my current location (run and pasted from the JS-console, thus no time stamps):

Code: Select all

========================================
System in range # 0
oSys: [SystemInfo galaxy 7, system 73]
System ID: 73
System coords: (60.8, 28.2, 0)
System government: 5
========================================
System in range # 1
oSys: [SystemInfo galaxy 7, system 102]
System ID: 102
System coords: (63.6, 22.8, 0)
System government: 4
========================================
System in range # 2
oSys: [SystemInfo galaxy 7, system 110]
System ID: 110
System coords: (56, 24, 0)
System government: 6
========================================
System in range # 3
oSys: [SystemInfo galaxy 7, system 120]
System ID: 120
System coords: (63.2, 27, 0)
System government: 2
========================================
System in range # 4
oSys: [SystemInfo galaxy 7, system 127]
System ID: 127
System coords: (63.2, 24.4, 0)
System government: 4
========================================
System in range # 5
oSys: [SystemInfo galaxy 7, system 177]
System ID: 177
System coords: (56.4, 19, 0)
System government: 7
========================================
System in range # 6
oSys: [SystemInfo galaxy 7, system 184]
System ID: 184
System coords: (61.2, 20.2, 0)
System government: 2
========================================
System in range # 7
oSys: [SystemInfo galaxy 7, system 193]
System ID: 193
System coords: (62, 26.8, 0)
System government: 3
========================================
System in range # 8
oSys: [SystemInfo galaxy 7, system 201]
System ID: 201
System coords: (56.4, 26, 0)
System government: 5
:D

Re: [WIP] Witchspace blockades

Posted: Wed Dec 25, 2013 2:37 pm
by Bugbear
Thanks Commander McLane (long time between posts - did I mention that I wasn't able to devote a lot of time to this project? :-) ). I've altered my script accordingly and I'm now getting the desired results.

For any JavaScripters in general: I've created a LogMsg function that allows me to use a (sort of) global variable to switch to and from debug mode:

Code: Select all

function LogMsg( strMsg )
{
	var blnDebug = true;  // Changing this variable to False has the effect of switching off logging.
	if ( blnDebug )
	{
		log( this.name, strMsg );
	}
}
Something I've noticed when using this function: this.name does not resolve to the script name, so when I execute the line:

Code: Select all

log( this.name, "========================================" );
...I get the expected output in the log file:

Code: Select all

22:17:09.375 [Witchspace Blockades 0.1]: ========================================
...but when I call my logging function:

Code: Select all

LogMsg( "========================================" );
...I get:

Code: Select all

22:17:09.375 [undefined]: ========================================
Note that the OXP name is undefined.

Obviously, this is no deal breaker but it would be nice to have...

How do I make 'this' accessible from within my function? I know I could simply pass a reference to 'this' in via the input parameters, but that solution seems a bit cumbersome. If it can't be done...oh well...

Re: [WIP] Witchspace blockades

Posted: Wed Dec 25, 2013 4:00 pm
by Norby
Bugbear wrote:
function LogMsg( strMsg )
This line cause "undefined" name. Use this format:

Code: Select all

this.$WB_LogMsg = function ( strMsg )
Then call as:

Code: Select all

this.$WB_LogMsg("===========");
The "$" need to avoid conflicts with possible future core functions (see here) and "WB" refer to your OXP to avoid conflicts with other OXPs.

You will be in similar trouble if start using FrameCallBacks or primable equipments where this.x() is not working.
The always working solution is worldScripts["yourworldscriptname"].x() instead of this.x() which is executed much more slower so should be saved into a local variable and your scriptname should not contain version number due to you will not want replace it through your code with every new version (this.version is the right place).
Space is working in names but my practice is not using or replacing it to "_".

Your Debug variable should be global to use the same in all functions. Sample code based on these:

Code: Select all

this.name = "Witchspace_Blockades"; 
...
this.$Witchspace_Blockades_Debug = true;
...
this.$Witchspace_Blockades_LogMsg = function ( strMsg )
...
this.playerStartedJumpCountdown = function(type, seconds)
{
   var w = worldScripts["Witchspace_Blockades"];
   var wlog = w.$Witchspace_Blockades_LogMsg;
...
   if ( w.$Witchspace_Blockades_Debug )
   {
...
      wlog("====");
      wlog("System in range # " + counter);
...
   }
}

Re: [WIP] Witchspace blockades

Posted: Sat Dec 28, 2013 6:30 am
by Bugbear
Norby, thanks for your sample code - I hadn't considered creating a reference variable to a function.

I've kind of got your code working:

Code: Select all

this.$Witchspace_Blockades_LogMsg = function ( strMsg )
{
	var blnDebug = true;
	if ( blnDebug )
	{
		log( this.name, strMsg );
	}	
}

this.playerStartedJumpCountdown = function(type, seconds)
{
	var w = worldScripts["Witchspace Blockades"];
	var wlog = w.$Witchspace_Blockades_LogMsg;
	
	wlog( "Logging using the function reference." );	//	This call fails to resolve this.name
	$Witchspace_Blockades_LogMsg( "2nd Logging using the function reference." );	//	this call succeeds in resolving this.name
...
As I've indicated in the comments above, the first call fails to resolve this.name but the second succeeds. Interestingly, both calls to the logging function succeed in creating a log entry:

Code: Select all

14:09:55.017 [undefined]: Logging using the function reference.
14:09:55.017 [Witchspace Blockades]: 2nd Logging using the function reference.
I feel like I'm [this] close (I'm holding my fingers close together...); it's gotta be something simple I'm missing.

Edit: I wonder if I have to set up a separate worldScripts.plist file...be right back :-)

Cheers.

Re: [WIP] Witchspace blockades

Posted: Sat Dec 28, 2013 3:21 pm
by Norby
Sorry, the correct format is:

Code: Select all

   var w = worldScripts["wblogtest"];
   var wlog = w.$Witchspace_Blockades_LogMsg.bind(this);
Within normal event handlers (no FCB nor equipments) you can use the simple form:

Code: Select all

   var wlog = this.$Witchspace_Blockades_LogMsg.bind(this);
These shortened formats maybe a bit more readable but of course you can call the function directly also as in your 2nd example. My practice in this case to write "this." before:

Code: Select all

this.$Witchspace_Blockades_LogMsg( "2nd Logging using the function reference." );

Re: [WIP] Witchspace blockades

Posted: Sun Dec 29, 2013 10:43 am
by Bugbear
Ahh, it's a thing of beauty. Working nicely now. Thanks Norby. :mrgreen: :mrgreen:

For the benefit of anyone wanting to do the same thing, here's the working code sample:

Code: Select all

// First define the LogMsg function that conditionally writes to the Latest.log
this.$Witchspace_Blockades_LogMsg = function ( strMsg )
{
	var blnDebug = true;
	if ( blnDebug )
	{
		log( this.name, strMsg );
	}	
}

// Next, 
this.playerStartedJumpCountdown = function(type, seconds)
{
        // Create a reference to the current script
	var w = worldScripts["Witchspace Blockades"];

       //  Create a reference to the LogMsg function for 'shorthand' access
	var wlog = w.$Witchspace_Blockades_LogMsg.bind(this);
	
	wlog( "Logging using the function reference." );
	this.$Witchspace_Blockades_LogMsg( "2nd Logging using the function reference." );
}
And here's what you should expect to see in Latest.log:

Code: Select all

18:38:33.319 [Witchspace Blockades]: Logging using the function reference.
18:38:33.319 [Witchspace Blockades]: 2nd Logging using the function reference.

Re: [WIP] Witchspace blockades

Posted: Tue Dec 31, 2013 6:14 am
by Bugbear
OK, I think I has teh dumb.

I want to log the government description to the Latest.log.

Here's the code that is successfully logging various system attributes for all systems within 7 LY of Lave:

Code: Select all

	var aSysInRange = system.info.systemsInRange();

	for ( intCounter in aSysInRange )
	{
		objSystem = aSysInRange[ intCounter ];
		LogMsg( "========================================" );   // LogMsg is a logging function that has been defined earlier in the script
		LogMsg( "oSys: " + objSystem );
		LogMsg( "System ID: " + objSystem.systemID );
		LogMsg( "System coords: " + objSystem.coordinates );
		LogMsg( "System government: " + objSystem.government );
		LogMsg( "System govt desc: " + objSystem.governmentDescription );
		LogMsg( "System name: " + objSystem.name );
	}
And below is the output that I've been getting:

Code: Select all

13:57:17.213 [Witchspace Blockades]: ========================================
13:57:17.213 [Witchspace Blockades]: oSys: [SystemInfo galaxy 0, system 39]
13:57:17.213 [Witchspace Blockades]: System ID: 39
13:57:17.213 [Witchspace Blockades]: System coords: (7.6, 30.2, 0)
13:57:17.213 [Witchspace Blockades]: System government: 3
13:57:17.213 [Witchspace Blockades]: System govt desc: undefined
13:57:17.214 [Witchspace Blockades]: System name: Reorte
13:57:17.214 [Witchspace Blockades]: ========================================
13:57:17.214 [Witchspace Blockades]: oSys: [SystemInfo galaxy 0, system 46]
13:57:17.214 [Witchspace Blockades]: System ID: 46
13:57:17.214 [Witchspace Blockades]: System coords: (1.2, 36.2, 0)
13:57:17.214 [Witchspace Blockades]: System government: 0
13:57:17.214 [Witchspace Blockades]: System govt desc: undefined
13:57:17.215 [Witchspace Blockades]: System name: Riedquat
...etc...
So it's clear that I am successfully establishing a connection to the System object, so why is the Government Description coming back as undefined?

An urelated second question - I've noticed that the hours, minutes and seconds on the system clock for a new game are synchronised to the local (real life) system time. So for me, it's currently 14:49 in the afternoon, and on a fresh game, the ships clock also reads 14:49. This behaviour is intentional? (it doesn't bother me one way or the other, but is useful to know)


Thanks in advance.

Re: [WIP] Witchspace blockades

Posted: Tue Dec 31, 2013 8:34 am
by another_commander
The way the systeminfo scripting entity gets the planet information it requests from the core is such that the result has the form of a dictionary with the following entries:
KEY_GOVERNMENT
KEY_ECONOMY
KEY_TECHLEVEL
KEY_POPULATION
KEY_PRODUCTIVITY
KEY_RADIUS
KEY_NAME
KEY_INHABITANT
KEY_INHABITANTS
KEY_DESCRIPTION
Anything not included in the above will not be part of the scripting objects retrieved from system.info. The systemID and coordinates are a different case, as they are internal to the systeminfo object and are always present. You will find that you will get the same undefined result if you try to log e.g. the economy description.

I guess the best thing for us to do is to include all this missing information in the dictionary returned from the core to systeminfo, provided that the performance penalty for generating this additional data is not significant (which it shouldn't be).

Regarding the clock behaviour, yes, what you are seeing is intentional.

Re: [WIP] Witchspace blockades

Posted: Tue Dec 31, 2013 10:22 am
by cim
another_commander wrote:
I guess the best thing for us to do is to include all this missing information in the dictionary returned from the core to systeminfo, provided that the performance penalty for generating this additional data is not significant (which it shouldn't be).
Descriptions wouldn't be. Something like system.info.corona_flare would be, though, since that's highly dependent on the exact number of RNG calls in the particular part of the setUpSpace function. I think we'd have to write everything to planetinfo.plist (and then it will show up in system.info) but that has its own problems.

Re: [WIP] Witchspace blockades

Posted: Tue Dec 31, 2013 12:09 pm
by Norby
Bugbear wrote:
why is the Government Description coming back as undefined?
There is a workaround in cim's [wiki]New_Cargoes[/wiki] OXP, until Admirals coming up with the localized solution you can do this:

Code: Select all

this.govTypes = ["Anarchy",
                 "Feudal",
                 "Multi-Government",
                 "Dictatorship",
                 "Communist",
                 "Confederacy",
                 "Democracy",
                 "Corporate State"];
...
LogMsg( "System govt desc: " + this.govTypes[objSystem.government] );

Code: Select all

13:04:58.030 [wblogtest]: oSys: [SystemInfo galaxy 0, system 39]
13:04:58.030 [wblogtest]: System ID: 39
13:04:58.030 [wblogtest]: System coords: (7.6, 30.2, 0)
13:04:58.030 [wblogtest]: System government: 3
13:04:58.030 [wblogtest]: System govt desc: Dictatorship
13:04:58.030 [wblogtest]: System name: Reorte
13:04:58.030 [wblogtest]: ========================================
13:04:58.030 [wblogtest]: oSys: [SystemInfo galaxy 0, system 46]
13:04:58.030 [wblogtest]: System ID: 46
13:04:58.031 [wblogtest]: System coords: (1.2, 36.2, 0)
13:04:58.031 [wblogtest]: System government: 0
13:04:58.031 [wblogtest]: System govt desc: Anarchy
13:04:58.031 [wblogtest]: System name: Riedquat

Re: [WIP] Witchspace blockades

Posted: Wed Jan 01, 2014 12:17 pm
by Bugbear
Once again, my thanks Norby. A custom array / dictionary will have to do until there's a clean way to access the required strings from the core.