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
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 »

There are a fair few other problems with what you have posted, including non-matching variable names and some missing curly brackets. This should work to do what you want it to do.

this.thargoidCounter is the variable used to count Thargoids as they spawn. It's set to 0 when the game is launched, when the player docks and when the player is about to jump.

The next code block under the this.shipSpawned will run whenever the game engine adds a ship to the ooniverse. If the player's kills are less than 512 it does nothing - return; stops the code running any further. Otherwise it will check to see if a ship added is a thargoid and is so increase the counter by 1.

The next code block under a combined this.shipExitedWitchSpace = this.shipLaunchedFromStation event handler will run just as the player exits witchspace or launches. By this point the game engine should have populated the system and this.thargoidCounter will be more than 0 if any thargoids were spawned. It then adds 1 UFOship for each Thargoid, within 20000m of the player. After adding the right number of UFOships it resets this.thargoidCounter to 0

Code: Select all

// Standard attributes 
this.name           = "UFOship.js"; 
this.author         = "mandoman"; 
this.copyright      = "This script is hereby placed in the public domain."; 
this.version        = "1.0.1"; 
this.description    = "Script to spawn equal number of UFOships as number of Thargoid ships if player ship with score 512, or higher exits Witchspace."

//code block to set thargoidCounter to 0 on starting the game, docking and entering witchspace.
this.startUp = this.shipDockedWithStation = this.shipWillEnterWitchspace = function()
{
 this.thargoidCounter = 0;
}

// code block that runs when any NPC ship is spawned
this.shipSpawned = function(newship) //newship is now available to the function body
{
  if (player.score<512){return;} // do nothing if players kills are too low.
  if (newship.isThargoid) {this.thargoidCounter++;} //check if newship is a Thargoid (isThargoid is a property of a ship object), and if so increase this.thargoidCounter by 1
}

// code block which runs just as player exits witchspace tunnel, or just as player launches
this.shipExitedWitchSpace = this.shipLaunchedFromStation = function()
{
  if (this.thargoidCounter>0) // check to see if at least one thargoid was spawned
  {
    system.addShips("UFOship", this.thargoidCounter, player.ship.position, 20000); //add one UFOship for each thargoid spawned.
    this.thargoidCounter = 0;// reset the counter after spawning UFOships
  }
}
Another simpler way to do the same thing would be

Code: Select all

// Standard attributes 
this.name           = "UFOship.js"; 
this.author         = "mandoman"; 
this.copyright      = "This script is hereby placed in the public domain."; 
this.version        = "1.0.1"; 
this.description    = "Script to spawn equal number of UFOships as number of Thargoid ships if player ship with score 512, or higher exits Witchspace."

// will run as player exits witchspace or player launches from station, and after game engine has populated the system
this.shipExitedWitchSpace = this.shipLaunchedFromStation = function()
{
  var UFOshipCounter = system.countShipsWithPrimaryRole("UFOship"); // count how may UFO ships are in the system (in case this is a second launch and they have already been spawned.
  if (player.score < 512 || UFOshipCounter > 0){return;} // do nothing if player kills is less than 512 or there are already UFOships in the ooniverse.
  var thargoidCounter = system.countShipsWithPrimaryRole("thargoid"); // counts how many thargoids there are
  if (thargoidCounter> 0) // if more than 0 then spawn equal number of UFOships.
 {
    system.addShips("UFOship", thargoidCounter, player.ship.position, 20000);
 }
}
[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
mandoman
---- E L I T E ----
---- E L I T E ----
Posts: 1375
Joined: Thu Apr 28, 2011 3:17 pm

Re: Scripters cove

Post by mandoman »

Um, thanks. I didn't know it was possible to shorten the script to that extent. So, was the game simply ignoring my old script? It wasn't telling me it was wrong, which it did do for a while. When I stopped getting the script error warnings, I thought I had it licked, but it never produced UFOships when I popped into a system full of Thargoids (which happens more, and more lately). Do you mind if I try out that script? :)
Mandotech Industries Wiki Page.

http://wiki.alioth.net/index.php/User:Mandoman
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 »

mandoman wrote:
Do you mind if I try out that script? :)
Well I posted it for you to try, so why not.... :wink:

I'm not sure what the game engine would have done with the script as you posted - but it definitely wouldn't have done what you wanted it to do.

See if you can follow the logic of what's happening and when with the two version I have posted. The comments should help. Refer to the JS reference on the wiki for any commands you don't recognise. We will get you up to speed with JS :D
[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
mandoman
---- E L I T E ----
---- E L I T E ----
Posts: 1375
Joined: Thu Apr 28, 2011 3:17 pm

Re: Scripters cove

Post by mandoman »

Capt. Murphy wrote:
mandoman wrote:
Do you mind if I try out that script? :)
Well I posted it for you to try, so why not.... :wink:

I'm not sure what the game engine would have done with the script as you posted - but it definitely wouldn't have done what you wanted it to do.

See if you can follow the logic of what's happening and when with the two version I have posted. The comments should help. Refer to the JS reference on the wiki for any commands you don't recognise. We will get you up to speed with JS :D
Thank you. Any help is much appreciated. :)
Mandotech Industries Wiki Page.

http://wiki.alioth.net/index.php/User:Mandoman
Switeck
---- E L I T E ----
---- E L I T E ----
Posts: 2411
Joined: Mon May 31, 2010 11:11 pm

Re: Scripters cove

Post by Switeck »

Capt. Murphy wrote:
Another simpler way to do the same thing would be

Code: Select all

// Standard attributes 
this.name           = "UFOship.js"; 
this.author         = "mandoman"; 
this.copyright      = "This script is hereby placed in the public domain."; 
this.version        = "1.0.1"; 
this.description    = "Script to spawn equal number of UFOships as number of Thargoid ships if player ship with score 512, or higher exits Witchspace."

// will run as player exits witchspace or player launches from station, and after game engine has populated the system
this.shipExitedWitchSpace = this.shipLaunchedFromStation = function()
You might want to remove
this.shipLaunchedFromStation =
...simply because otherwise when the player first launches from station after reloading a savegame, if there's any Thargoids in the system they'll have UFOship/s spawned right at the player's present location. (In short, very near the main station.) While Thargoids in a regular system are normally rare, it would be better if UFOships only appeared near them when that's the case.

How to set/move UFOships to where the Thargoids may happen to be...is a problem though, as the Thargoids' positions will probably need to be stored in a variable first. That's much like the problem I'm having with assigning an npc ship to a variable (presumably an array) so I can quickly access that ship's properties later. More details here:
problems with variable-assigned ships...
User avatar
mandoman
---- E L I T E ----
---- E L I T E ----
Posts: 1375
Joined: Thu Apr 28, 2011 3:17 pm

Re: Scripters cove

Post by mandoman »

Switeck wrote:
Another simpler way to do the same thing would be

...simply because otherwise when the player first launches from station after reloading a savegame, if there's any Thargoids in the system they'll have UFOship/s spawned right at the player's present location. (In short, very near the main station.) While Thargoids in a regular system are normally rare, it would be better if UFOships only appeared near them when that's the case.

How to set/move UFOships to where the Thargoids may happen to be...is a problem though, as the Thargoids' positions will probably need to be stored in a variable first. That's much like the problem I'm having with assigning an npc ship to a variable (presumably an array) so I can quickly access that ship's properties later. More details here:
problems with variable-assigned ships...
That's just another scripting problem I'm having. What type of script would work WITH the game itself, so that the game initiates the script at random. UFOships don't need to appear every time a Thargoid is present, just a certain percent of the time, like maybe %70, or something. I think that gets into the Math stuff, but I don't really know.
Mandotech Industries Wiki Page.

http://wiki.alioth.net/index.php/User:Mandoman
User avatar
Okti
---- E L I T E ----
---- E L I T E ----
Posts: 700
Joined: Sun Sep 26, 2010 1:51 pm
Location: A GH shop, near witchpoint to Oresrati in Galaxy 8

Re: Scripters cove

Post by Okti »

Hi,

Is there a limit for the subentities a ship can have?
My OXP's
And Latest Mission Coyote's Run
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Sanity Check - worldScripts, shipScripts, equipmentScripts

Post by Capt. Murphy »

Hi,

Some PM discussions with another member over some scripting problems leads to this question. Can a dev confirm my understanding of the way that worldScripts, shipScripts and equipmentScripts (this.activated()) work.

My understanding is that:

Scripts flagged as a worldScript are loaded as a single copy into game memory.

Scripts referenced as a shipScript in shipdata are loaded as multiple exclusive copies into game memory, one copy for each entity that uses the script. The upshot of this being that variables declared as this.foo are not shared between different copies of the script in memory.

The other upshot of this is that is also generally a bad idea to try and have a single physical script acting both as a worldScript and a shipScript as this will result in multiple exclusive copies of the same script so that for example variables declared as this.foo under a worldscript event handler will be undeclared when checked under a shipScript handler, even though all the code is contained in one script.

I'm not quite sure where equipmentScripts fit in. My gut feeling is that there should generally be no issue with a worldScript also acting as an equipmentScript, but I think it's probably a bad idea to try and have a shipScript and equipmentScript in one single physical script.
[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
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 »

From my non-coder, layman's perspective it seems reasonable to assume that you'll have in most cases only one instance of an equipment script, because usually you can have each piece of (primeable) equipment only once. I'm not sure, however, if that holds always absolutely true.

For the general question, my basic understanding is that world scripts and ship scripts are two different and distinct things, like apples and oranges. Therefore having one physical script acting as both would be more or less like having one physical fruit acting as both an apple and an orange. I wouldn't do that.

I am not sure, however, whether it would do actual damage, apart from flooding the player's RAM. This in itself should generally be no issue, if I compare a typical script's size to the RAM size of a modern computer. World scripts have to be declared as world scripts by one of two ways: either naming them script.js and putting them the Config folder or by giving them an individual name, putting them in the Scripts folder and writing their name into world-scripts.plist inside the Config folder. No script that doesn't match one of those two conditions will be recognized as a world script. And each physical script can match these conditions only once, therefore it will only be recognized as a world script once. When you attach it to multiple ships in their shipdata, you only will make them have long scripts that do nothing.

However, the "do nothing" part is not entirely true, because there are some event handlers which can be used by both types of scripts, like shipLaunchedFromStation. So it would be tricky to differentiate between actions which you want to have performed when the player launches (world script) and actions you want to have performed whenever an NPC carrying your script launches (ship script), if both scripts are physically the same.

In short: world scripts and ship scripts are apples and oranges. Don't expect them to be both at the same time. To be honest: in a couple of years as a scripter the thought never even crossed my mind.


EDIT: corrected bad example
Last edited by Commander McLane on Tue Feb 21, 2012 9:59 am, edited 1 time in total.
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 »

Commander McLane wrote:
I am not sure, however, whether it would do actual damage, apart from flooding the player's RAM. This in itself should generally be no issue, if I compare a typical script's size to the RAM size of a modern computer.
Though the Oolite Javascript VM only has 8Mb available to it.

Unless you're doing something really ambitious with thousands of entities all with a shipscript with several local variables, though, that's still going to be plenty.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Scripters cove

Post by JensAyton »

cim wrote:
Though the Oolite Javascript VM only has 8Mb available to it.
My understanding is that the 8 MB “limit” is really only a hint that mainly acts as a target for the garbage collector. This may not be true in the version of SpiderMonkey that we’re currently using, though.
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 »

Ahruman wrote:
cim wrote:
Though the Oolite Javascript VM only has 8Mb available to it.
My understanding is that the 8 MB “limit” is really only a hint that mainly acts as a target for the garbage collector. This may not be true in the version of SpiderMonkey that we’re currently using, though.
I found at least one reference to the Spidermonkey API documentation being misleading about the meaning of the memory limit and it actually being a hard cap, and that matched my experience while stress-testing New Cargoes.
User avatar
mandoman
---- E L I T E ----
---- E L I T E ----
Posts: 1375
Joined: Thu Apr 28, 2011 3:17 pm

Re: Scripters cove

Post by mandoman »

Okay, here's another try by the sub-rookie. What is wrong with this script. I know it's wrong, because Latest.log says so, but I don't know why......exactly. It's probably syntax, or just structure, but I don't know for sure.

Code: Select all

this.name        = "solartanker_plea"; 
this.author      = "mandoman"; 
this.copyright   = "� 2012 mandoman"; 
this.description = "This script is the death actions of the SolarTanker."; 
this.version     = "1.1";

this.ship = (SolarTanker)
this.commsMessage = (solarTankerPlea)
this.reaction = (explosiveShrapnel)

this.shipDies = function ()
{
if (this.ship, + this.commsMessage) = this.reaction;
}
Here is the Latest.log message.

Code: Select all

11:52:34.095 [script.load.notFound]: ***** Could not find a valid script file named solarTankderDies.js.
Any help is much appreciated. I was trying to keep it simple, but even simple seems to be hard for me.
Mandotech Industries Wiki Page.

http://wiki.alioth.net/index.php/User:Mandoman
User avatar
Ironfist
Commander
Commander
Posts: 218
Joined: Tue Jun 28, 2011 2:16 pm
Location: London

Re: Scripters cove

Post by Ironfist »

mandoman,

Is it as the latest.log says, this script is probably OK but what is it called - it would appear that the script which is calling this - is looking for "solarTankderDies.js" could it actually be called 'solarTankerDies.js'.
Just a thought.

Ironfist
64bit Mint 10 and Win 8 64bit on E8400 at 3.6GHz - ATI HD5750 graphics.
Concentration is the ability to think of absolutely nothing when it is absolutely necessary.
User avatar
Okti
---- E L I T E ----
---- E L I T E ----
Posts: 700
Joined: Sun Sep 26, 2010 1:51 pm
Location: A GH shop, near witchpoint to Oresrati in Galaxy 8

Re: Scripters cove

Post by Okti »

Mandoman, I think there are many things wrong with that script.

I assume that is a ship script. So the name of the JS File must be typed right at the shipdata.plist. The Second thing is there is no this.shipDies event, must be this.shipDied. Third thing is what are the (solarTankerPlea) and (explosiveShrapnel). PM me a link to the OXP and your clear intention about what do you want to achieve. Then I can help.
My OXP's
And Latest Mission Coyote's Run
Post Reply