Page 1 of 1

[SOLVED] JS Question about global values and errors

Posted: 27 May 2021, 21:01
by Badguizmo
Hello.
In my JS code (for Riftforce) I have a global value named "this.cardActivated = {1:0,2:0,3:0};" in constructor.

I am having an error :
- When clicking a normal button I have a JS Error related to "Cannot read property '0' of undefined"
- If I hit "F5" my button is working without any more error
- Then it is the opponent turn (which have the same button, same error, same way to "F5 Fix")
- Then my turn again ==> Same JS Error

I have added some "console.log" in my scripts
What I can see is that if I do "console.log(this.cardActivated[1]);", the result is "undefined".

What can explain this strange behaviour ? i.e. : error despite the fact that the global value is declared and should be at least equal to "0".

FYI, the logic is
- Button clic ==> method "validateCardToDiscardForActivationFunctionButton" is called
- This method calls another method called "this.hightlightCardsToActivate();"
- this second method I am using these two values : "this.cardActivated[1]" and "this.cardActivated[2]"

I am also using these two values in other method without problems.

NB : the complete JS error log is :

Code: Select all

Javascript error:
During callback: onclick / validateCardToDiscardForActivationFunctionButton
Cannot read property '0' of undefined
TypeError: Cannot read property '0' of undefined
at Object.hightlightCardsToActivate (https://en.1.studio.boardgamearena.com:8083/data/themereleases/current/games/riftforce/999999-9999/riftforce.js?1622144652120:768:60)
at Object.validateCardToDiscardForActivationFunctionButton (https://en.1.studio.boardgamearena.com:8083/data/themereleases/current/games/riftforce/999999-9999/riftforce.js?1622144652120:1623:22)
at Object. (https://studio.boardgamearena.com/1/riftforce?table=280500:2020:50)
at HTMLAnchorElement. (https://en.1.studio.boardgamearena.com:8083/data/themereleases/210527-1006/js/dojoroot/dojo/dojo.js:8:94920)
Script:
Url: https://studio.boardgamearena.com/1/riftforce?table=280500
BGA version 210527-1006
U=2337642
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
EDIT : Note to myself : affect the correct type at a global value at any time :?

Re: JS Question about global values and errors

Posted: 27 May 2021, 21:09
by robinzig
could you share some of the code that causes the error? It's a bit hard to guess without that.

I will say though that you should be cautious about having an object in JS whose keys are numeric. If you need to do this it's better to be explicit that the keys are strings - ie have `this.cardActivated = {"1": 0, "2": 0, "3": 0}`. This is because in JS all object keys are implicitly strings - integer keys are for arrays. (Arrays are themselves objects but have special behaviour, and you shouldn't try to confuse the two.)

If the intention is to have a sequential set of numbers then an array might be better than an object - I don't know what the use case is.

Re: JS Question about global values and errors

Posted: 27 May 2021, 22:25
by Benoit314
In your resetGlobalVarsForPlayer function you set this.cardActivated to 0.

Code: Select all

resetGlobalVarsForPlayer()
        {
            //console.log("resetGlobalVarsForPlayer");
            this.cardIdSelected = 0;
            this.nbCardPlayed = 0; 
            this.actualPlacement = {1:0,2:0,3:0};

            this.nbCardActivated = 0;
            this.cardActivated = 0;
            this.activationPossible = false;
        },

Re: JS Question about global values and errors

Posted: 27 May 2021, 23:16
by Badguizmo
...
I think I spend too much time on this code, not seeing what is in front of my nose :oops: :oops: :oops: :oops: ...

Thank you, that is the the problem.
Solved with "this.cardActivated = {1:0,2:0,3:0};" in the reset method.

Comme on dit mieux vaut un qui sait que 10 qui cherchent ^^
Benoit314 wrote: 27 May 2021, 22:25 In your resetGlobalVarsForPlayer function you set this.cardActivated to 0.

Code: Select all

resetGlobalVarsForPlayer()
        {
            //console.log("resetGlobalVarsForPlayer");
            this.cardIdSelected = 0;
            this.nbCardPlayed = 0; 
            this.actualPlacement = {1:0,2:0,3:0};

            this.nbCardActivated = 0;
            this.cardActivated = 0;
            this.activationPossible = false;
        },