I'm working on a simple Pygame project, and in the options menu I have a couple of toggle boxes for switching the sounds and animations off. The boolean variables for these are stored in a separate Settings class.
Although it's not really necessary, I thought I'd try to make a ToggleButton class that takes the following arguments:
ToggleButton(game_instance, boolean, text, xpos, ypos, width, height)
I've given it a draw() method and a toggle() method. I'd want to pass in my self.settings.sounds_on or self.settings.animations_on booleans from my main game program, so that switching the toggle box on and off would switch the sounds and animations on and off.
In testing this, I've made two instances of my ToggleButton class:
ToggleButton(self, self.game_instance, self.settings.sounds_on, "Sounds", 50, 100, 25, 25)
ToggleButton(self, self.game_instance, self.settings.animations_on, "Animations", 50, 150, 25, 25)
The toggle boxes draw the screen, and they can be interacted with.
Image of toggle boxes on screen
However, while the toggle() method changes the value of the boolean inside the instance of the class, it doesn't seem to be connecting to the original boolean that was passed into the instance as an argument (I hope I'm using the correct terminology here, I'm still learning). So while the appearance of toggle box changes depending on whether the boolean is True or False, it doesn't actually have any effect at all on the settings for sound and animations.
Here is a link to the code for the button, and also a short example program that just draws a single toggle box to the screen and each time it's clicked, it prints the status of the original boolean, and the boolean inside the instance. You can clearly see that the original boolean is always False, whereas the self.boolean parameter of the toggle box changes.
https://github.com/ElJuanito82/ToggleBox/blob/main/example
So it seems that when the button is instantiated, it takes a copy of the boolean value in its current state, rather than creating a direct link so that when one changes so does the other. Do I have the right idea here?
Is there something I'm missing here that will allow this to work?