Eric Walch wrote:Screet,
I know about that fix, I even suggested it to McLane. But that fix does not work with 1.73
I changed the code into:
Code: Select all
this.test = function()
{
var preyLimit = 10;
function isOffender(entity)
{
return entity.isShip && entity.bounty >= preyLimit && !entity.isCloaked
}
var nextTarget = system.filteredEntities(this, isOffender, this.ship, 25600)
log("testship", "Testship found target: "+ nextTarget);
}
This works with trunk but with 1.73 i now get:
Code: Select all
Exception: ReferenceError: preyLimit is not defined
Active script: "aquatics_platform" 1.0
testShip.js, line 45:
return entity.isShip && entity.bounty >= preyLimit && !entity.isCloaked
So I get confused to what is wrong JS code or what is a bug in Oolite. It even logs the wrong active script.
I like to fix this in a way it works in both Oolite versions.
I think I know what's happening! It's an out of scope thing. You have a named function inside another function. For a very long time it was not possible to do just that, but apparently it's possible now. There's a mini-essay to be written on the subject, but I'll stop now.
Basically
this inside isOffender refers to the isOffender function itself, which has no connection whatsoever with the rest of the function 'above' it. Instead of using a named function declaration, you're better off using an anonymous function declaration, which still allows for 'external' variables to be used inside its declaration.
If I'm right, either of the two following function declations should work:
the first one, not that much of an improvement:
Code: Select all
function isOffender(entity)
{
this.preyLimit = 10;
return entity.isShip && entity.bounty >= this.preyLimit && !entity.isCloaked;
}
it's hardly worth the bother to create the preyLimit variable to begin with.
The second one, useful if you want to 'import' preyLimit from outside the function:
Code: Select all
var preyLimit = 10;
var isOffender = function(entity)
{
return entity.isShip && entity.bounty >= preyLimit && !entity.isCloaked
}
the following declaration
wouldn't work
Code: Select all
this.preyLimit = 10;
var isOffender = function(entity)
{
return entity.isShip && entity.bounty >= this.preyLimit && !entity.isCloaked
}
because the
this keyword outside the function should refer to 2 different things. Still don't quite know why the original code worked in 1.73.x I thought it wouldn't have worked at all to begin with...