Posted: Sat Oct 20, 2007 10:42 pm
unless you specifically use the event triggers for reload/reset etc.. I understand.
For information and discussion about Oolite.
https://bb.oolite.space/
Code: Select all
var Xmark1Shiptype=new Array()
Xmark1Shiptype[0]="Adder"
Xmark1Shiptype[1]="Asp MkII"
Xmark1Shiptype[2]="Anaconda"
etc
Code: Select all
{
"Xmark1Shipdata" =
{
like_ship = "Xmark1Shiptype[0]";
bounty = "missionVariable.Xmark1_bounty_number";
pilot = "missionVariable.Xmark1_pilot_name";
roles = "Xmark1";
etc.
}
Yes, although a more efficient notation is:Arexack_Heretic wrote:so I have been browsing on some JavaScript tutorials.
I can setthen use this array to call the strings at will?Code: Select all
var Xmark1Shiptype=new Array() Xmark1Shiptype[0]="Adder" Xmark1Shiptype[1]="Asp MkII" Xmark1Shiptype[2]="Anaconda" etc
Code: Select all
var Xmark1Shiptype = ["Adder", "Asp MkII", "Anaconda"]
You can’t use JavaScript variables or expressions in a plist file. You could use a declaration similar to the above to declare a property list in JavaScript:for instance
shipdata:Code: Select all
{ "Xmark1Shipdata" = { like_ship = "Xmark1Shiptype[0]"; bounty = "missionVariable.Xmark1_bounty_number"; pilot = "missionVariable.Xmark1_pilot_name"; roles = "Xmark1"; etc. }
Code: Select all
XMark1Shipdata =
{
like_ship: Xmark1Shiptype[0],
bounty: missionVariable.Xmark1_bounty_number
// etc.
}
In what context?Howe does oolite acces arrays?
script_info entries will be copied into the scriptInfo property of a created ship object, and may contain arrays. For instance, if your shipdata.plist entry contains:and can I read existing plist-arrays into a variable?
Code: Select all
<key>script_info</key>
<dict>
<key>AH_testArray</key>
<array>
<string>test1</string>
<string>test2</string>
</array>
</dict>
Code: Select all
this.ship.scriptInfo.AH_testArray[0] // == "test1"
The code foo = bar will cause foo and bar to refer to the same object (in this case, the same array). If you actually want a copy of an array, you can achieve this with the (somewhat obtuse) method foo = bar.slice(0) (which means “copy all elements from element 0 – that is, the first element – to the end of the array).something like:
var Xmark1Shiptype=array(some_uncomplicated_array)
Another problem you’re likely to run into is that no entity properties are implemented in 1.69.x. Note that the documentation in Oolite scripting refers to 1.69.x, and the documentation in Oolite scripting drafts is upcoming functionality.i'll try to construct a simple variable ship test-oxp now...
--edit: a problem I run into is that most entity properties are read-only, I'll have to check the dev-notes, to be sure the wiki is uptodate.
Code: Select all
this.didExitWitchSpace = function()
{
var mark1 = missionVariables.mission_mark1_seed1;
var mark2 = missionVariables.mission_mark2_seed1;
var mark3 = missionVariables.mission_mark3_seed1;
var mark4 = missionVariables.mission_mark4_seed1;
var mark5 = missionVariables.mission_mark5_seed1;
if (mark1 == undefined) this.setSeed1(mark1);
else if (mark2 == undefined) this.setSeed1(mark2);
else if (mark3 == undefined) this.setSeed1(mark3);
else if (mark4 == undefined) this.setSeed1(mark4);
else if (mark5 == undefined) this.setSeed1(mark5);
};
this.setSeed1 = function(m)
{
var m = missionVariables.mission_mark1_seed1;
m = Math.floor(Math.random()*30); //random value between 0-29, not a float I hope.
};
This is the wrong approach. Instead, generate random strings, then store the strings.Arexack_Heretic wrote:What I am trying to do:
1) save a set of random numbers to mission_variables.
2) use these numbers to display predictable message missionscript.
(create JS-arrays from descriptions.plistkeys, use array[#] to select 'name', 'ship', 'bounty', etc strings)
I don’t see why you’d need to do this. Why wouldn’t the marks be using normal ships with the normal stats?5) alter this ship according to additional demands (ship.bounty=mV.mark#_bounty, ship.name=mV.mark#_name, max_speed, max_energy,etc)
I don't see how i can turn a random string into a number. the random number may be needed to set defferent parameters. such as bounty and shiptype may be linked. OK so I can use a descriptions array containing available shiptypes to call a ship.Ahruman wrote:This is the wrong approach. Instead, generate random strings, then store the strings.
I don’t see why you’d need to do this. Why wouldn’t the marks be using normal ships with the normal stats?5) alter this ship according to additional demands (ship.bounty=mV.mark#_bounty, ship.name=mV.mark#_name, max_speed, max_energy,etc)
On the other hand, in 1.70 you can change a ship’s primary role… although there’s no particular reason why you’d need to use numbered roles at all.
Generate and store a random number when you need a random number. When you need a random string, generate and store a random string. Storing a random number and using it to select the same string over and over again is unlikely to be useful.Arexack_Heretic wrote:I don't see how i can turn a random string into a number. the random number may be needed to set defferent parameters. such as bounty and shiptype may be linked. OK so I can use a descriptions array containing available shiptypes to call a ship.
Bounty is writable, but was incorrectly marked read-only in the wiki. Ship name (shipDescription) is writeable.Why change the ship? because I want to. just adding a random ship doesn't cut it.
I want it to have a specified pilot (can set bounty here) and a specified shiptype.
changing the name would be required otherwise you can't find the mark.
Missile locked on to Cobra Mk III (guild target)
Cobra Mk III
guild target
6.394 km (Clean)
This can be handled in multiple ways using JS scripts. For instance, you could add a special property (say, ah_markID) to marks, and then search for them with system.filteredEntities(), as in:I think I need numbered role, because I only want a single instance of a mark(at most) to be present in the system.
Code: Select all
this.findMarkWithID(targetID)
{
function filterPredicate(entity)
{
return entity.isShip && entity.ah_markID != undefined && entity.markID = targetID
}
let matches = system.filteredEntities(this, filterPredicate)
if (matches.length == 0) return null
else return matches[0]
}
This may be important for L_Bs random_marks project as they will only be spawned in a single sytem. having them leave player would need to follow.Alternatively, you could use an array in the mission script to track marks, and use the isValid property to check if they still exist. This behaves slightly differently, as it considers a ship to still exist if it’s “suspended” in a wormhole
Code: Select all
var variable2 = outside
this.funcA = function()
{
var var1=2;
if (var1!=1) {this.funcB;};
}
this.funcB = function()
{
if (var1<=1) {stuff}
else {var1--;};
}
Arexack_Heretic wrote:Small question on variables again:
when I exit a function, all variables defined inside this function are deleted.
when I call a function or method inside a function, do the variables of the first function carry over to the second one?
eg.Code: Select all
var variable2 = outside this.funcA = function() { var var1=2; if (var1!=1) {this.funcB;}; } this.funcB = function() { if (var1<=1) {stuff} else {var1--;}; }
is funcA exited when calling funcB and the variable lost?
Methods are functions. A method is simply an object property which is a function. (Incidentally, I’m writing a JavaScript Concepts page for the wiki…)Arexack_Heretic wrote:if so would putting the "var variable" outside the function keepit existing for as long as the script is being run?
or does this need to be "this.variable = value", as a property of the script?
...I think I'm confusing functions with methods now.
:roll:
Code: Select all
var variable2 = outside
this.funcA = function()
{
var var1 = 2
if (var1 != 1) { var1 = this.funcB(var1) }
}
this.funcB = function(var1)
{
if (var1 <= 1) { stuff }
else { var1-- }
return var1
}
Code: Select all
this.funcA = function()
{
var var1 = 2
function funcB()
{
if (var1 <= 1) { stuff }
else { var1-- }
}
if (var1 != 1) { this.funcB() }
}
// This is where it starts to get weird:
this.outer = function(x)
{
var y = x + 2
function closure(n)
{
return y + n
}
return closure // Returns the function itself; does not call the function.
}
this.sevenPlus = this.outer(5) // this.sevenPlus is a now function which returns 7 + its argument
Log("Ten: " + sevenPlus(3)) // logs "Ten: 10"
Log("Twelve: " + sevenPlus(5)) // logs "Twelve: 12"