Understanding "Sounds Plus"

I’m still struggling with a neat way to call a bunch of buttons that derive their names and functions from a list of values in a table. I know I can do it longhand, but that’s definitely not the way to go.

Since this is exactly what the “Sounds Plus” project does, that comes with Codea, I have really been trying to get under its skin and work it out. I find it very difficult to follow, but I’ve cracked most of it, and it’s taught me a lot about how buttons can work in practice. The one thing I have not been able to work out is how the SoundButton function gets initialised for each of the SoundButtons. I can see that the relevant function (e.g. SoundButton(SOUND_EXPLODE) is stored in the table “self.buttons” on the BasicMenu tab, and I can see that ultimately a draw function is called for each of these, under function BasicMenu:draw() and BasicMenu:drawButtons(btns) but I really can’t see where all those buttons have been initialised. Logically that comes first, right?!

So I’m either consistently overlooking a bit of code or misunderstanding how classes work. Either’s possible. I hope someone can help me out. Thanks for your patience with me so far. I’m finding Codea completely addictive, so I doubt this will be my last question.

The self.sounds table is a list of sound names, and these names are also the labels used by Codea to refer to specific sounds.

The self.buttons table is a set of SoundButton objects, one per sound name in self.sounds. If you look in the SoundButton tab, under the init function which runs when each SoundButton object is created, you’ll see the action property is defined as a function that runs sound(self.soundName), ie it plays the sound.
(if you don’t understand how this function definition works, google “coolcodea anonymous functions” for more).

When you touch a button, Codea checks all the button positions, and if it finds a button you have touched, it runs its action property (line 59 of Button tab), and that plays the sound.

So the function in the action property is what you were looking for.

I understood the actions, it was the bit before I was looking for. So it’s the fact that every time a SoundButton object is merely created (by putting it in the table?) the relevant init function is called. That might be where I’m going wrong. At the moment my table is full of strings which relate to the relevant buttons I intend to call elsewhere, rather than being a button object which would then automatically get initialised.

I had assumed that the only way classes got initialised was either explicitly calling the initialise function (so SoundButton:init(v)) or by creating an instance of it (e.g. ExplodeButton= SoundButton(v)). But it seems I can add another way to do it: simply creating the object in a table.

A little knowledge is a dangerous thing, so right now I’m lethal!

@epicurus101 - I think you may need to understand classes better

Try this and this.

And if you really want to understand the difference between tables and classes, try this and this

And yes, you can create a class instance by including it in a table.

Thanks again Ignatz. I’d gone through 6 and 7 but clearly it hadn’t all sunk in! 169 looks extremely helpful as well, I may go back and reorganise my tables in a way that makes more sense.

Don’t worry, I also find all this stuff takes time to absorb, and there aren’t too many good examples or tutorials around