The value of my variables are nil on certain functions and not others when using OOP

Hi ! I've been trying to develop a small application for my Playdate since yesterday. So I'm really beginning, and I guess I'm just doing something wrong, but I witness a weird behavior with my variables when I try to do OOP-style programming (I'm on macOS btw, and you can see the screenshots at the end of the post) :

Basically, I start by creating a simple class (here it's named Test). I'm then calling the init() function (I decided to not call the super.init() function, but my problem happens if I do call it too).
I then declare some variables :

  • A variable called testBool with the value of false
  • A variable called testValue with the value of 0

I then call the update method (in this example I do call the super.update() method, but my problem happens if I don't call it too). Btw I have no idea if I should call the super func here or in the init() func ?

In this update method, my testBool variable clearly has a value of false as intended, and my testValue variable has a value of 0 as intended too.

Then, I call another method called checkControls() in the update, because I want to check if the player is pressing a button, and if so, I want to change the values of both variables.
We can clearly see that the values of testBool and testValue are both nil in my checkControls() method. I have no idea why.

If I press a button, the code recognize that I did so and seems to create new variables and give them the value intended (so true and 1), and we can see in the screenshot that it's not the variable I created in the init() function that has been modified. It's new variables that have the same name that are created.

This problem always happen when I try to do anything using OOP, so I'm guessing I do something wrong. Some of the code in the main function has nothing to do with my problem, I just created a new class called Test to explain the problem more easily to you guys, the other functions and stuff are basically my "real code" and have the exact same issue.
I'm not used to do OOP programming, sorry if my question is kind of dumb...

Thanks for your help!


On line 31 you’re calling checkControls directly on the Test class (Test:checkControls). What you mean to do is to call this on self (self:checkControls) so you act on the current instance, just as your debug print checks do.

Also, you should always call super.init.

Thanks a lot!
Out of curiosity, can I ask why I should always call super.init()? (I guess, the same goes for super.update() and other functions of this kind)
Is it because I have to make sure all the stuff made in the original init() function I'm overriding are still done before I do my own stuff?

Thanks again for your help! :slight_smile: