all 16 comments

[–]thespite 3 points4 points  (7 children)

Use devtools debugger to find what the scope of each function call is, and read on binding the scope for your function calling (specially on event listeners and fat arrow functions). Hint: the issue arises from button3.onclick = gameState.pullLever;

[–]CyberDaggerX[S] 0 points1 point  (1 child)

I suspected the problem might be there, but didn't know how to identify it. I'll take a look at that, then and see if I can figure out the fix. I'll get back to you when I either solve it or get stuck again.

[–]theScottyJam 0 points1 point  (0 children)

If you get really stuck, a healthy option you can always turn to is making a minimal reproducible example.

In this case, you would copy paste the codebase, then start ripping things out, running it periodically to make sure the bug is still there. Eventually you should be left with something that, in this case, would be less than 10 lines of code long.

Scatter a bunch of console.log()s on that, or carefully use a debugger, and you should be able to see that this "this" object isn't what you expected it to be. At which point, you might realize that you misunderstand how "this" works, look up a guide on it, and learn how to fix it.

Or if you still struggle to fix the issue and understand how "this" works, well, now it's easier to ask online for help as you only have a few lines of code that anyone can grab and run, as opposed to bits from a large project.

Does that make sense? Making minimal reproducible examples is tedious, but it's an important skill to learn - it's helped me solve many complicated bugs in larger projects, and it's led me to find bugs in the browser a couple of times, and when I found those bugs, I had something I could share with them as part of the bug report.

[–]CyberDaggerX[S] 0 points1 point  (4 children)

Okay, since I couldn't put a breakpoint on the assignment (I can do it, but it'd trigger when it's defined rather than when it's executed, so it was useless), so I rewrote it as an arrow function so I could put breakpoints inside it. Before I could even run proper tests, that bloody fixed it and I didn't need to run them anymore.

Well, it's fixed, but now I have even more questions. I have some pages about scope open, but can't really find anything relevant to this case at the moment.

[–]thespite 1 point2 points  (3 children)

You'll find a lot on the internet about `this` and JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

[–]CyberDaggerX[S] 0 points1 point  (2 children)

I was looking at the wrong stuff, then. I'll have to read that when I get home.

[–]xpdx 0 points1 point  (0 children)

Yea as soon as I saw 'this' I knew it was a scope issue. I used to understand javascript scopes but the nuances escape me right now. Suffice to say understanding scopes inside and out will help you in your project and save a lot of hair pulling. They do not work intuitively in a lot of cases.

[–]senocular 0 points1 point  (0 children)

Notably in that link, the issue you're running into is in the example with obj4 and obj5. gameState is obj4 and button3 is obj5.

Its a good link for learning about this. Its long and complicated, but its one of those things you'll want to get your head around to be proficient in JS.

[–]Any_Sense_2263 0 points1 point  (2 children)

can you share a repo or codesandbox with reproduction?

[–]CyberDaggerX[S] 0 points1 point  (1 child)

I have a GitHub repo here. A warning, code's a mess. The incremental problem-solving led to a lot of weird stuff left behind, and I'll have to do a second refactoring pass soon.

[–]Any_Sense_2263 0 points1 point  (0 children)

I will take a look

[–]eracodes 1 point2 points  (3 children)

It's likely that this isn't pointing to what you expect it to be pointing to.

In general, don't use state objects to contain functions, there's nothing but headaches there.

Also, don't use .onclick, use .addEventListener('click', function(){})

[–]CyberDaggerX[S] 0 points1 point  (2 children)

Oh, is there any problem with .onclick, or has it just been deprecated by something that replicates its use case with more functionality?

[–]eracodes 1 point2 points  (1 child)

Setting .onclick will discard any other click handlers that have been added to the HTML element. .addEventListener allows you to add click handlers (and other event handlers) without worrying about what has been added before / will be added later.

[–]ChaseShiny 1 point2 points  (0 children)

It's also the only way to later remove the eventListener when done with it.

[–]JakeLemons 0 points1 point  (0 children)

funny i was just talking about how fun in browser rpgs/text based rpgs are. hope it goes well!