Clocks affect each other

In my Game: Editor ,Play

I’ve multiple clocks counting down… for researching, building something and a general clock for the game…
Research scripts are multiple instance and game is a single instance…
Script name for both differ… I made sure every variable.name is different, as the text elemets to display the clocks ( alarm clock and hourglas);

every clock in game works perfectly nice from coding side:
reseach take 30 sec
invoke Monster take 2 sec
game ends after 600 s

but on display; alarm clock stops at first time the hourglas apears on screen, and
hourglas shows time remaining for alarm clock…

it’s so insane, I’ve no idea where to look or start debunging… :sleepy:

remaining time is a script attribute (cause this works best for me with multi instances)
remaining time is in console.log() quite right…

only element.text = remaining time. toString() doesen’t work :sob:

every if so little hint what I can do is very desired…

thanx
:cucumber:

ps: even if I messed up, ehy is alarm clock stop running but ring alarm after 600 secs?
I’m totaly lost…

What should happen instead?

As far as I know, if remainingTime is a number, you can just do element.text = remainingTime. I also suggest to check the value of remainingTime if that doesn’t work.

1 Like

console.log remaingTime is ok;

I took a harbit on toString() because I had some issues with that before…
but I’m quite sure toString() is not the problem (or?)…

:cucumber:

ps: or working to long with GoLang and typescript made me superstitious about casting types :wink:

I need to take a look at your project. Which script and entities I have to look at?

all clocks ( there is one more the army whatch) should run independly from each other…

every clock get a remain Time when entity.enabled via attributes…
every single instance should count down with ever update(dt) call

In the Timer.js script: PlayCanvas | HTML5 Game Engine

You create global variables for Text and oldText so all Timer.js scripts are referring to same text element Entity

Therefore, if you have multiple timers on the screen using the same script, only one will update.

Change:

Timer.prototype.initialize = function() {
    Text = this.entity.findByName('Text');
};

To

Timer.prototype.initialize = function() {
    this.text = this.entity.findByName('Text');
};

And in update:


    if (this.run) { 
        var oldText = this.text.element.text;
        this.time -= dt;
        this.text.element.text = (Math.round(this.time)).toString();
        if (this.time < 0) {
            this.ticking = false;
            this.run = false; 
            this.entity.root.findByName('GameLoop').findByName('SoundAlarm').enabled = true;
            this.entity.root.findByName('GameLoop').script.gameLoop.timeUp(this.mode);
        }
        if ( this.time < 10 && oldText != this.text.element.text) {
            this.entity.findByName('Beep').enabled = true;
        }
    }

    this.entity.findByName('SoundTicking').enabled = this.ticking;

};

The referenced text entity variable is now in scope of the script object and oldText is locally scoped.

2 Likes

I see… and sure I implemend… but …
there are two different scripts: Counter.js (for Hourglas and watch)
and Timer.js (for arlam clock)
I know JS is not to accurate for accessing varibales, and ‘Text’ is not a very
good name…

sorry … :innocent:
I’ve a first testing of the game tomorow to see reacting a.s.o.
(LAN party with friends, so they have to),
There’s so much to do ( win 3 of 5 or 5 of 7) including reset the game …
but I’m afraid all I get from testing is the clocks shows wrong :sob:

Counter.js has the same problem and using the same global variable Text

Basically, your main problem here is variable scoping. This page may help: You-Dont-Know-JS-1/ch1.md at master · jumaschion/You-Dont-Know-JS-1 · GitHub

1 Like

I disagree here. JS is rules on how scope works but the rules can be complex

but the Scope of a boolean may differ from Scope of a string in any possible way…

I did something like “fontWeight = 120” and that works global… all the time …
It’s hard to reduce JS to your own app ?

After one Year with typeScript and GoLang I embrace that everything could be everything

Vec3 = ‘’ // ok

I want to be a better Noop, so I knew to know… :stuck_out_tongue_closed_eyes:

Scope seems random to me

I disagree here. JS is rules on how scope works but the rules can be complex
that should slow me down… maybe I’dont know all JS… but I would’t like to know what i Did wrong

I’ve laeand about JS types like null, ukown , object, numer and boolean?
I’m not really sure about scope…:
i do single instanc e = instane
and multi Instances = this.instance…

Taking your code here:

Timer.prototype.initialize = function() {
    Text = this.entity.findByName('Text');
};

Text was not declared as a new variable within the current scope, so JS automatically assigns it to ‘global’ scope. This would be same as doing:

Timer.prototype.initialize = function() {
    window.Text = this.entity.findByName('Text');
};

And window is a global object in the browser.

If you did this:

Timer.prototype.initialize = function() {
    var Text = this.entity.findByName('Text');
};

This makes Text variable scoped to the function

And this:

Timer.prototype.initialize = function() {
    let Text = this.entity.findByName('Text');
};

This is ES6 which scopes it to the block.

Doing the following:

Timer.prototype.initialize = function() {
    this.text = this.entity.findByName('Text');
};

Is assigning the value to a property on the this object which in this case, the pc.ScriptType instance and is therefore accessible in any function of this pc.ScriptType

I’am famous for work arrounds…
I’am hated because I do so many work arrounds…
I neeed to know how to turn workAround into cleanCode…