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

Timer continues firing

For test results, bug reports, announcements of new builds etc.

Moderators: another_commander, winston, Getafix

Post Reply
popsch
Above Average
Above Average
Posts: 27
Joined: Sun Jan 01, 2006 3:50 pm

Timer continues firing

Post by popsch »

Can anyone tell me, why the timer continues firing, although it is already stopped?

Code: Select all

this.name = 'AsteroidTrap';
this.copyright = '(C) 2015 Popsch.';
this.licence = 'CC-NC-by-SA 2.0';

this.debug = 1;

'use strict';

/** Distance at which the pirates take off */
this.proximityWarning = 15000;

/** Likelyhood of police trap present */
this.TRAP_LIKELIHOOD = 0.4; // will be divided by the tech level
//this.TRAP_LIKELIHOOD = 1;

this.asteroids = null;
this.announcer = null;

this.distanceChecker = null;

/** launch from station for testing */
this.shipWillLaunchFromStation = function() {
    if (this.debug != 1) return;

    log(this.name, 'Ship LAUNCHED.');
    player.ship.position = system.locationFromCode('OUTER_SYSTEM_OFFPLANE');

    this.$populateWithTrap(player.ship.position.add([1E4, 1E4, 1E4]));
};

this.$populateWithTrap = function(pos) {
    asteroidCount = (system.scrambledPseudoRandomNumber(clock.seconds) / 2) * 10 + 2;
    log(this.name, 'initialized trap with asteroids: ' + asteroidCount);

    // instantiate cargopods
    this.asteroids = system.addShips('piratetrap_asteroid', asteroidCount, pos, 7000);

    this.distanceChecker = new Timer(this, this.$checkDistance, 5,5);

};

/** populate the system */
this.systemWillPopulate = function(station) {

     if (system.scrambledPseudoRandomNumber(clock.seconds) < this.TRAP_LIKELIHOOD / system.techLevel) {
         system.setPopulator('trap', {
             callback: function(pos) {
                 this.$populateWithTrap(pos);
             }.bind(this),
             location: 'LANE_WP'
         });
     }
};

/** Timer function callback to check the distance to the player */
this.$checkDistance = function() {
    ships = system.filteredEntities(this,
                                    function $checkAsteroid(entity) {
                                        return (entity.primaryRole == 'piratetrap_asteroid');
                                    },
                                    player.ship, 17000);
    
    log(this.name,'timer: '+this.distanceChecker);
    
    if ( (ships == null) || (ships.length == 0)) {
        // nothing close
        return;
    }

    // spring the trap
    asteroids.forEach(function(entry) {
        s = system.addShips('pirate', 1, entry.position, 250)[0];
        s.target = player.ship;
        s.performAttack();
        this.announcer = s;
    }.bind(this));

    s.commsMessage('Surprise!');

    this.distanceChecker.stop();
    this.distanceChecker = null;
};
I see this in the log:

Code: Select all

...
07:57:09.057 [AsteroidTrap]: timer: [Timer nextTime: 6.51177, interval: 5, running, function: anonymous]
...
07:57:17.564 [AsteroidTrap]: timer: null
07:57:17.631 [script.javaScript.exception.unexpectedType]: ***** JavaScript exception (AsteroidTrap 0.3.1): TypeError: this.distanceChecker is null
...
07:57:19.545 [exit.context]: Exiting: Exit Game selected on options screen.

User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2302
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia

Re: Timer continues firing

Post by Wildeblood »

It's not continuing to fire every 5 seconds, it's just firing one last time? That's just because you've got the sequence inside this.$checkDistance in the wrong order? I dunno.
Delete the timer just above the "//spring the trap" comment. I dunno.

The exception is caused because you aren't deleting it.

Code: Select all

this.distanceChecker.stop();
    this.distanceChecker = null;
Should be something like:

Code: Select all

if (this.distanceChecker) {
    this.distanceChecker.stop();
    delete this.distanceChecker;
    }
That will still cause a different exception, if it's called one last time? I dunno.


'use strict';
Needs to be at the top of file before anything else.
"So anti-globalist, he's practically a flat-earther."
popsch
Above Average
Above Average
Posts: 27
Joined: Sun Jan 01, 2006 3:50 pm

Re: Timer continues firing

Post by popsch »

Deleting doesn't help. It still fires multiple times afterwards. However, I get this error then.

Code: Select all

[script.javaScript.exception.unexpectedType]: ***** JavaScript exception (AsteroidTrap 0.3.1): TypeError: this.distanceChecker is undefined
User avatar
Wildeblood
---- E L I T E ----
---- E L I T E ----
Posts: 2302
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia

Re: Timer continues firing

Post by Wildeblood »

Is it apparently working, and just causing these exceptions afterwards? Or are you not saying that it's not actually working at all? :mrgreen:
"So anti-globalist, he's practically a flat-earther."
popsch
Above Average
Above Average
Posts: 27
Joined: Sun Jan 01, 2006 3:50 pm

Re: Timer continues firing

Post by popsch »

Wildeblood wrote:
Is it apparently working, and just causing these exceptions afterwards? Or are you not saying that it's not actually working at all? :mrgreen:
They continue working and calling the function.
User avatar
spara
---- E L I T E ----
---- E L I T E ----
Posts: 2676
Joined: Wed Aug 15, 2012 4:19 am
Location: Finland

Re: Timer continues firing

Post by spara »

popsch wrote:
Wildeblood wrote:
Is it apparently working, and just causing these exceptions afterwards? Or are you not saying that it's not actually working at all? :mrgreen:
They continue working and calling the function.
They?
popsch
Above Average
Above Average
Posts: 27
Joined: Sun Jan 01, 2006 3:50 pm

Re: Timer continues firing

Post by popsch »

spara wrote:
popsch wrote:
Wildeblood wrote:
Is it apparently working, and just causing these exceptions afterwards? Or are you not saying that it's not actually working at all? :mrgreen:
They continue working and calling the function.
They?
"It". Grammar mistake, sorry. :)
User avatar
phkb
Impressively Grand Sub-Admiral
Impressively Grand Sub-Admiral
Posts: 4676
Joined: Tue Jan 21, 2014 10:37 pm
Location: Writing more OXPs, because the world needs more OXPs.

Re: Timer continues firing

Post by phkb »

Not sure if this is the issue, but you're calling $populateWithTrap from the systemWillPopulate routine as well as the shipWillLaunchFromStation routine. I think you only need one of these calls. Also, I don't think systemWillPopulate takes station as a parameter.
User avatar
cim
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 4072
Joined: Fri Nov 11, 2011 6:19 pm

Re: Timer continues firing

Post by cim »

phkb wrote:
Not sure if this is the issue, but you're calling $populateWithTrap from the systemWillPopulate routine as well as the shipWillLaunchFromStation routine. I think you only need one of these calls. Also, I don't think systemWillPopulate takes station as a parameter.
That's the issue - you're starting two timers (at least two, depending on how many times you launch from the station before trying to stop one), and only stopping one of them. The other you're deleting the timer object that it uses to determine what to do, but not stopping the internal routine used to call it on a timer.

What I'd recommend is that rather than using shipWillLaunchFromStation as your debug function, you get the debug console and enter

Code: Select all

player.ship.position = system.locationFromCode('OUTER_SYSTEM_OFFPLANE');
worldScripts["AsteroidTrap"].$populateWithTrap(player.ship.position.add([1E4, 1E4, 1E4]));
into that.

You'll also want a shipWillEnterWitchspace function which checks if the timer exists and is running, and stops it if so, or it'll keep running when you enter a new system (and you may get a second one)
Post Reply