Multiple players - not multiplayer (yet)

Hello!

The title may sound mysterious, but it is very simple. The human player controls their objects that they own and their children. The player is a child of a level root script.

There are also other players who have their own objects that have their own children, but they may well be AI. Think of a chess game with 2 players. The other player may be human (eventually) but right now I just want them to behave as scripted, so an AI.

My problem - I need to limit the interactions a player can have so they can only control their own children - sure, that’s straight forward enough by just checking references. However when one of my player’s objects calls OnGUI, that happens for all players. When one player wins the game, he gets the victory and defeat messages at the same time. This particular problem is probably going to appear elsewhere in the game as well.

So what’s the best policy / technique to manage this? Eventually I would like to be able to make the game truly multiplayer, but I don’t really want to get into the network nitty gritty yet if I can help it.

Thanks for the help! :slight_smile:

Well, there’s your first problem - two problems really.

  1. OnGUI is thoroughly obsolete for in-game UI - use Unity UI instead, unless you’re supporting huge amounts of legacy OnGUI code. OnGUI is ugly, it’s hard to customize, and it is molasses slow by comparison.

  2. your player object shouldn’t call the GUI. In a well-structured game, the in-game objects shouldn’t even know that a UI exists - they should expose public members and methods that let the UI get the access needed to do its job, but at no point should the YourPlayerScript object touch the UI. If you completely scrap and rebuild your UI, you shouldn’t have to touch a line of code on YourPlayerScript.

Instead, the UI watches a particular YourPlayerScript object, and that’s how this problem is solved.

Hi StarManta,

That’s good advice - yes I will do that. It gives a lot more power on making the GUI too. I will effectively databind the relevant GUI to a GameObject which will then behave appropriately on its own.

However, I don’t see how that actually solves my issue. Assume the “enemy player” is interacting with his pieces - would the current player not see his interactions as well?