Hey all, sorry if this is a little basic, but I'm working on separating my code -- which was all in main.lua -- into multiple files for different object classes etc. In general I'm trying to keep the files as self-contained as possible and have them all the game logic handled by main.lua, however I've run into instances where it makes sense for these class files to be aware of the state of things outside their class.
For example, I'm working on a soccer game and I want the player class (in the player.lua file) to be aware of the ball position. I suppose I could have the update loop in main.lua send the new position to every player every frame, but logically I feel it would make more sense for the players to "watch" the ball position themselves. One option would be to just make the ball position a global variable, but I know global variables can be a risky business.
I do have the ball object as its own class (in the ball.lua file) so I could import it into the player class, but I'm unsure the best way to do that. Would I import ball.lua into main.lua? Or directly into player.lua? What if they both need to access it? Or is there a better programming pattern for monitoring variables like this?
Any help is greatly appreciated!