Timer continues firing

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

Moderators: winston, another_commander, Getafix

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

Timer continues firing

Post by popsch » Sun Sep 13, 2015 12:04 pm

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: 2161
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia
Contact:

Re: Timer continues firing

Post by Wildeblood » Sun Sep 13, 2015 12:56 pm

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.

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

Re: Timer continues firing

Post by popsch » Sun Sep 13, 2015 4:33 pm

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: 2161
Joined: Sat Jun 11, 2011 6:07 am
Location: Western Australia
Contact:

Re: Timer continues firing

Post by Wildeblood » Sun Sep 13, 2015 5:02 pm

Is it apparently working, and just causing these exceptions afterwards? Or are you not saying that it's not actually working at all? :mrgreen:

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

Re: Timer continues firing

Post by popsch » Sun Sep 13, 2015 6:09 pm

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: 2553
Joined: Wed Aug 15, 2012 4:19 am
Location: Finland

Re: Timer continues firing

Post by spara » Sun Sep 13, 2015 6:25 pm

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 » Sun Sep 13, 2015 10:11 pm

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
Commodore
Commodore
Posts: 2438
Joined: Tue Jan 21, 2014 10:37 pm
Location: [p]laying [h]ard and [k]icking [b]utt somewhere in G7...

Re: Timer continues firing

Post by phkb » Mon Sep 14, 2015 4:30 am

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: 4018
Joined: Fri Nov 11, 2011 6:19 pm

Re: Timer continues firing

Post by cim » Mon Sep 14, 2015 6:36 am

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