Question about multiple classes and calling functions from them

So I am currently making an app. Obviously or else I wouldn’t be here. Right now I am making my game with several different classes, currently at 7. But I am running into issues where I have to call methods from other classes in order to use the variable:remove() function, but can’t because they are not module classes. I have thought about making changes to the code to change a lot of it into functions to allow me to call into other classes, but the code that I have seen doesn’t seem to work.

For instance, when I press the mute button on the main menu, I am trying to call the music object from the main.lua into another class to use the audio.stop, but whatever I try doesn’t seem to work. I’ve tried the:

local M = {}; function M.mute() audio.stop(test) end return M;

local muteButton = require("main") muteButton.mute()

The code above is typed in the main.lua, and would be called into the mainMenu.lua which loads the interface. Each iteration that I try doesn’t seem to work. Can anyone tell what I’m missing?

Also, is it wise to use a lot of classes? I know that if I had my entire main menu into one class, I wouldn’t really have this problem. But wouldn’t that increase the memory load?

Thanks for reading everyone. Any information would be helpful!

This is a potential cause of the problem:

local muteButton = require("main")

You’re requiring main.lua inside main.lua, instead of requiring “mainMenu”.

To answer your question about using classes, generally it’s a good thing to use them. It’s much easier to debug an error in a small class than in a single 10000 line main.lua file, and it also makes it easier to make reusable object types.

That second line of code is located inside the mainMenu class, not the main. The first code example is in the main. Sorry, I should have clarified better. I am trying to call the method in a touch event but comes up with a boolean error or if I try to move the code I get syntax errors.

Great, that’s what I was thinking as well. Thanks for the clarification. Besides, I don’t think my OCD could take 10000 lines of code

I once (professionally) had to debug about 4,000 lines of ‘C’ written in Fortran (a,b,c,d,e variables), two comments for the whole file, and it was split into two functions. I rewrote it :slight_smile:

rBurns629, you are confusing slightly objects and modules ; what you really have is a module there, and it will only ever have one instance. This doesn’t matter for a mutebutton but if it was (say) a slider control things could get confusing.

What would your recommendation be for handing audio files if you don’t mind me asking? I am trying to figure out a method to call the method any time to either play the file, mute, or stop. Maybe even remove. Should I just make a new class, use a constructor, and use boolean true or false statements to control something like a mute button?

Thanks for your time!

Yes, something like that. I have a Music object and an Audio object (for background music and sfx) and a control that talks to them via a messaging system - reusable code that you can plug in - like your idea but proper objects.

This is a potential cause of the problem:

local muteButton = require("main")

You’re requiring main.lua inside main.lua, instead of requiring “mainMenu”.

To answer your question about using classes, generally it’s a good thing to use them. It’s much easier to debug an error in a small class than in a single 10000 line main.lua file, and it also makes it easier to make reusable object types.

That second line of code is located inside the mainMenu class, not the main. The first code example is in the main. Sorry, I should have clarified better. I am trying to call the method in a touch event but comes up with a boolean error or if I try to move the code I get syntax errors.

Great, that’s what I was thinking as well. Thanks for the clarification. Besides, I don’t think my OCD could take 10000 lines of code

I once (professionally) had to debug about 4,000 lines of ‘C’ written in Fortran (a,b,c,d,e variables), two comments for the whole file, and it was split into two functions. I rewrote it :slight_smile:

rBurns629, you are confusing slightly objects and modules ; what you really have is a module there, and it will only ever have one instance. This doesn’t matter for a mutebutton but if it was (say) a slider control things could get confusing.

What would your recommendation be for handing audio files if you don’t mind me asking? I am trying to figure out a method to call the method any time to either play the file, mute, or stop. Maybe even remove. Should I just make a new class, use a constructor, and use boolean true or false statements to control something like a mute button?

Thanks for your time!

Yes, something like that. I have a Music object and an Audio object (for background music and sfx) and a control that talks to them via a messaging system - reusable code that you can plug in - like your idea but proper objects.