Having trouble with 'this' variables in programming.Programmable methods

So I was trying to track the current frame number and wrote a simple method like this under the programming.Programmable component (and exposed it as a programmableProperties):

if (typeof this.myFrame === "undefined") {
    this.myFrame = 0;
} else {
    this.myFrame++;
}

I then call this from a character chooseAction method. I expected to see the myFrame variable incremented once per frame, but it only gets incremented once (it gets stuck at the value of 1).

But if I do this directly in the chooseAction method:

this.incFrame = function() {
    if (typeof this.myFrame === "undefined") {
        this.myFrame = 0;
    } else {
        this.myFrame++;
    }
};

this.incFrame();

it works fine. I’m a novice at Javascript (my expertise is more C/C++/Python) so perhaps there is something I don’t understand at the language level (?).

Not sure if you can look at my level I was working on, but it’s called Method Madness (not published).

Hi SirArt–thanks for the great bug report. I had actually noticed something similar, but it took until you posted this code for me to have any idea as to what might be going wrong. I created an issue on GitHub here: https://github.com/codecombat/codecombat/issues/62

It does seem to follow the pattern that it works within the top-level Programmable method but not in the other method, perhaps either because the nested Programmable methods breaks it or because adding new Thang methods (as opposed to extending old ones) breaks it. I’ll have to take a look if I get more time. If you’re really interested, you could see if you can spot the bug in the Programmable Component–we just open sourced everything so it should be possible to set up the dev environment and play with the Programmable code.

1 Like

I’ll take a look at the code as you suggest and see if I spot anything; if I can’t make sense of it I’ll wait until you get a chance to look at it.

Thanks for the tip on setting up the dev environment. That could be immensely helpful.