I've been writing some code for my unity project, where I am connecting players to a game and adding them to a dictionary, which is in a Database class.
I have a simple method which is adding them to the database and if successful, it then instantiates the relevant game objects into the game.
The thing that made me wonder if it was a bad design was the way I was checking if the player is the local client or not.
This is my code:
public bool SetConnection(byte connectionID)
{
Agent agent;
if (Core.Instance.isLocalPlayer)
agent = new Agent_Local();
else
agent = new Agent_Enemy();
if (Core.Instance.Database.Add(connectionID, agent)) // fails if room is full
{
if (Core.Instance.isLocalPlayer) //Checking this again : maybe bad design?
Core.Instance.LocalHandler.SetPlayer(agent);
else
Core.Instance.SetEnemy(agent);
return true;
}
return false;
}
So you will notice I am checking if the player is local twice, and I feel that I shouldn't need to check if I was smarter about the way I went about setting this up. Is this considered as bad or redundant coding or perhaps someone could suggest a better approach?