Join us at the Oolite Anniversary Party -- London, 7th July 2024, 1pm
More details in this thread.

Scripters cove

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

Moderators: winston, another_commander

User avatar
Svengali
Commander
Commander
Posts: 2370
Joined: Sat Oct 20, 2007 2:52 pm

Re: Scripters cove

Post by Svengali »

No surprise to get so different results with absolutely different approaches. The snippet with forEach creates a new function on every iteration...
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Re: Scripters cove

Post by Capt. Murphy »

Interesting - I did another repeat of the test but with a simple arithmetic operation to cut out the native overhead - each element was multiplied by 2. array.forEach performance is very poor compared to the various ways for loops. It looks nice in code, but maybe should be avoided.

Edit to add - thanks Svengali. Is there a situation where forEach would be the best way?

Code: Select all

this.forwardforloop = function()
{
 var counter = 0;
 this.arrayOfSystemNames = new Array;
 for (counter = 0; counter < this.arrayOfSystemID.length;counter++)
 {
  this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
 }
}
Total time: 0.934 ms
JavaScript: 0.764 ms, native: 0.159 ms
Counted towards limit: 0.912 ms, excluded: 0.0219997 ms
Profiler overhead: 0.153 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:19) <anonymous> J 1 0.65 0.65 69.5 69.5 0.65
WorldScriptsGetProperty N 1 0.16 0.15 17.0 16.3 0.15
(<console input>) codeToBeProfiled J 1 0.92 0.12 98.8 12.3 0.12
OOStringFromJSString N 1 0.01 0.01 0.7 0.7 0.01

Code: Select all

this.optimisedforwardforloop = function()
{
 var counter = 0;
 var length = this.arrayOfSystemID.length
 this.arrayOfSystemNames = new Array;
 for (counter = 0; counter < length ;counter++)
 {
  this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
 }
}
Total time: 0.841 ms
JavaScript: 0.67 ms, native: 0.159 ms
Counted towards limit: 0.818 ms, excluded: 0.0229999 ms
Profiler overhead: 0.125 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:29) <anonymous> J 1 0.58 0.58 69.4 69.4 0.58
WorldScriptsGetProperty N 1 0.16 0.15 18.9 18.1 0.15
(<console input>) codeToBeProfiled J 1 0.83 0.09 98.6 10.2 0.09
OOStringFromJSString N 1 0.01 0.01 0.8 0.8 0.01

Code: Select all

this.reversedforloop = function()
{
 var counter;
 this.arrayOfSystemNames = new Array;
 for (counter = this.arrayOfSystemID.length - 1; counter >= 0;counter--)
 {
  this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
 }
}
Total time: 0.816 ms
JavaScript: 0.638 ms, native: 0.167 ms
Counted towards limit: 0.794 ms, excluded: 0.0219997 ms
Profiler overhead: 0.121 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:40) <anonymous> J 1 0.56 0.56 68.1 68.1 0.56
WorldScriptsGetProperty N 1 0.17 0.16 20.5 19.6 0.16
(<console input>) codeToBeProfiled J 1 0.80 0.08 98.7 10.0 0.08
OOStringFromJSString N 1 0.01 0.01 0.9 0.9 0.01

Code: Select all

this.optimisedreversedforloop = function()
{
 var counter;
 var length = this.arrayOfSystemID.length - 1;
 this.arrayOfSystemNames = new Array;
 for (counter = length; counter >= 0;counter--)
 {
  this.arrayOfSystemNames.push(this.arrayOfSystemID[counter]*2)
 }
}
Total time: 0.717 ms
JavaScript: 0.544 ms, native: 0.159 ms
Counted towards limit: 0.695 ms, excluded: 0.0219997 ms
Profiler overhead: 0.12 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:50) <anonymous> J 1 0.27 0.27 38.2 38.2 0.27
(<console input>) codeToBeProfiled J 1 0.70 0.27 98.0 37.7 0.27
WorldScriptsGetProperty N 1 0.16 0.15 22.2 21.2 0.15
OOStringFromJSString N 1 0.01 0.01 1.0 1.0 0.01

Code: Select all

this.forEachloop = function()
{
 this.arrayOfSystemNames = this.arrayOfSystemID.forEach(function(systemID){return systemID*2},this);
}
Total time: 7.923 ms
JavaScript: 7.753 ms, native: 0.159 ms
Counted towards limit: 7.901 ms, excluded: 0.0219997 ms
Profiler overhead: 4.858 ms
NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:61) <anonymous> J 512 6.99 5.40 88.2 68.1 0.24
(../AddOns/Array_Iteration_Profiling.oxp/Config/script.js:61) <anonymous> J 1 7.67 2.28 96.9 28.8 2.28
WorldScriptsGetProperty N 1 0.16 0.15 2.0 1.9 0.15
(<console input>) codeToBeProfiled J 1 7.91 0.08 99.9 1.0 0.08
OOStringFromJSString N 1 0.01 0.01 0.1 0.1 0.01
Last edited by Capt. Murphy on Wed Dec 14, 2011 12:46 pm, edited 1 time in total.
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
User avatar
Svengali
Commander
Commander
Posts: 2370
Joined: Sat Oct 20, 2007 2:52 pm

Re: Scripters cove

Post by Svengali »

I've digged a while back into loop optimizations. And yes, counting downwards is often faster.

http://www.websiteoptimization.com/speed/10/10-3.html
http://devpro.it/examples/loopsbench/
User avatar
Svengali
Commander
Commander
Posts: 2370
Joined: Sat Oct 20, 2007 2:52 pm

Re: Scripters cove

Post by Svengali »

Capt. Murphy wrote:
Edit to add - thanks Svengali. Is there a situation where forEach would be the best way?
If I understand it right there is no general advice. Every loop has different factors and it's up to the scripter to choose the right way for a specific situation. Even loop unrolling can be pretty slow compared to other ways.
User avatar
Cmdr. Maegil
Sword-toting nut-job
Sword-toting nut-job
Posts: 1294
Joined: Tue Feb 27, 2007 10:28 pm
Location: On the mend in Western Africa

Re: Scripters cove

Post by Cmdr. Maegil »

Does anyone have an idea on how to stop/restart the player turrets at will?

Cancelling the current target or setting it to a non-hostile will stop them, but there are situations where I want to be able to use the targeting system without having them shooting at my target, and in tight melees too often the turrets end up shooting what they shouldn't.
You know those who, having been mugged and stabbed, fired, dog run over, house burned down, wife eloped with best friend, daughters becoming prostitutes and their countries invaded - still say that "all is well"?
I'm obviously not one of them.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Scripters cove

Post by Thargoid »

The only way is to physically remove them (remove them as sub-entities), and to restore them when you want to turn them on again. But there is the additional item that if you dock with missing sub-ents, then you'll get offered a maintenance overhaul (at a large cost usually) to restore them as the game thinks they've been shot off rather than removed.

It can be worked around though, with a bit of scripting. Look at the script for the Vortex - that has switchable turrets by this method (including the work-around for the overhaul issue).
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Re: Scripters cove

Post by Commander McLane »

And it's only workarounds because turrets were originally intended as non-player equipment only.
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: Scripters cove

Post by Eric Walch »

Cmdr. Maegil wrote:
Does anyone have an idea on how to stop/restart the player turrets at will.
I thought we had a key for this: weapons off line, (the '_' key)
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Scripters cove

Post by Smivs »

Commander McLane wrote:
because turrets were originally intended as non-player equipment only.
And so they should have remained!
Mind you, the WonderWorm wouldn't be half as silly without them, so...... :P
Commander Smivs, the friendliest Gourd this side of Riedquat.
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5525
Joined: Thu Jun 12, 2008 6:55 pm

Re: Scripters cove

Post by Thargoid »

Eric Walch wrote:
Cmdr. Maegil wrote:
Does anyone have an idea on how to stop/restart the player turrets at will.
I thought we had a key for this: weapons off line, (the '_' key)
I got the impression from his post that he wanted to keep his lasers active, just switch off the turrets.

If not then yes, just activating weapons-safe will work.
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2323
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia

Re: Scripters cove

Post by Wildeblood »

Is there a way to suppress the "Target lost." console message when setting player.ship.target = null or removing the targeted entity? This is the code I have been using:-

Code: Select all

		this.my_drone = system.addShips("splinter", 1, player.ship.position.add(player.ship.heading.multiply(5000)), 5000)[0]; 
		player.ship.target = this.my_drone;
Later, that same evening...

Code: Select all

		if (this.my_drone)
			{
			if (player.ship.target === this.my_drone) player.ship.target = null;
			this.my_drone.remove(true);
		//	delete this.my_drone;
			}
I want to remove the drone quietly without drawing attention to it, but regardless of whether I set player.ship.target = null first, or just remove it, it always causes a "Target lost." console message.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Re: Scripters cove

Post by Commander McLane »

You can of course remove the drone quietly if you don't make it the player's target in the first place. But I guess making it the target is essential for what you want to do, so this may not be an option.
User avatar
Cmdr. Maegil
Sword-toting nut-job
Sword-toting nut-job
Posts: 1294
Joined: Tue Feb 27, 2007 10:28 pm
Location: On the mend in Western Africa

Re: Scripters cove

Post by Cmdr. Maegil »

<shameless plug>...of course, if you had handlers over the controls you could order the code to "press r" or flick the weapons off, then back on, to cancel the target lock-on just before removing it...</shameless plug>
You know those who, having been mugged and stabbed, fired, dog run over, house burned down, wife eloped with best friend, daughters becoming prostitutes and their countries invaded - still say that "all is well"?
I'm obviously not one of them.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Scripters cove

Post by cim »

Quick question: is there a way with script to find out how much cargo a station has in its market, and/or at what price? I can't see one in the docs, but I want to check I'm not missing something obvious.
User avatar
Commander McLane
---- E L I T E ----
---- E L I T E ----
Posts: 9520
Joined: Thu Dec 14, 2006 9:08 am
Location: a Hacker Outpost in a moderately remote area
Contact:

Re: Scripters cove

Post by Commander McLane »

cim wrote:
Quick question: is there a way with script to find out how much cargo a station has in its market, and/or at what price? I can't see one in the docs, but I want to check I'm not missing something obvious.
Not to my knowledge. The manifest object only exists for the player ship, as far as I know.
Post Reply