Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was suggested on StackOverflow to ask this question here.

I'm working on a simple game and I was thinking of adding multiplayer feature but I'm a bit stuck. I'm not sure what approach should I take, keeping in mind good programming practices.

I have a Player object which is created for each player but then I have many other classes that would have to be able to access them. The thing is, if I initialise them in, let's say my main method, then I can't relate to different instances of player class from other classes. The only solution to my problem that comes to my mind is using some form of global objects but afaik using anything globally in apps is usually not a good practice. Any suggestions/ideas?

share|improve this question
1  
What does your class-organization problem have to do with multiplayer? –  Liosan Jul 1 '13 at 9:17
    
Like I said if I initialise multiple instances of Player in let's say main method then the only way that comes to my mind which would allow me to relate to them from other classes is by somehow making them globally accessible. However this doesn't seem like a good practice therefore I was wondering what's the elegant solution for this. I have read about passing whole objects as arguments to different methods but it got me even more confused. –  Ninetou Jul 1 '13 at 10:09
3  
uh... still have no idea what multiplayer has to do with anything. The same criteria for players apply as for bullets, enemies or items. If "passing whole objects as arguments to different methods" then maybe you should learn a bit more programming before trying gamedev... or just go with your gut and see what happens. No use discussing a problem you don't have. –  Liosan Jul 1 '13 at 11:31
    
I don't really understand the organization of your program. Creating players in the main loop is fine, you just need to pass to the player list to any method that needs to reference them. Alternatively you could keep a static list or a static class that manages creating and deleting players as well as returning references. –  Benjamin Danger Johnson Jul 1 '13 at 23:49
3  
"The only solution to my problem that comes to my mind is using some form of global objects but afaik using anything globally in apps is usually not a good practice. Any suggestions/ideas?" Suggestion: get over your fear of solutions that goes against the "wisdom" some academic who's never written a game in his life. If globals are the easiest and most direct solution, use them. There are many potential problems with globals, yes. If you're not running into those problems but are running into problems they solve, use them. –  Sean Middleditch Aug 30 '13 at 22:36

1 Answer 1

You are only presenting a very narrow view on your whole object hierarchy, but usually you have one object, let's call its class GameState, which in turn is a composite of all other objects that are currently found in the game, like the enemies, bullets and players.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.