I am making a game which has a global (static) class called MedGameController. In my class I have two arrays of fifteen objects each which hold the logic for each players units. In the game the player’s go to a form where they see their already created units and have buttons to create more units. I don’t know how to check if current units exist though so I can’t even create a single unit. How do I check if the instance of my unit class exists in the array? The array of units have to have a scope which incompasses two maybe three forms so that is why I created them in a global class. Here is the global class:
static class MedGameController
{
static int ply1pts;
static int ply2pts;
Squad[] ply1squads = new Squad[15];
Squad[] ply2squads = new Squad[15];
public static int SetPly1Pts
{
get { return ply1pts; }
set { ply1pts = value; }
}
public static int SetPly2Pts
{
get { return ply2pts; }
set { ply2pts = value; }
}
public static int SquadSetUp
{
get { return squadcreation; }
set { squadcreation = value; }
}
public static void Player1Squads
{
This is where I think i'm supposed to check if they exist then if it doesn't then I create the instance of the squad class
}
}
}
Try this:
You’ve already declared
ply1Squadsto have 15Squadobjects, so it’s just a matter of initializing the objects at that point.You can do the same thing for
ply2Squads.BTW, you need to declare
ply1Squadsandply2Squadsasstaticas well, as static classes can’t have instance members:By default, these will be private variables, so you’ll want properties for these two arrays as well:
Additional Thoughts
If your design goal is to have
MedGameControllerhandle all the information related to the squads, I’d move away from what you appear to be doing – creating duplicate methods for eachSquadarray (i.e.,Player1Squads). Instead, pass in a flag of some sort telling the controller which array to use, like this:That’s a (very small) refactoring step. I would come up with a list of things you want the controller to do, and then figure out how to avoid a 1-1 mapping of every function to both arrays.
Hopefully this makes some sense and gives you a direction to go in. I have some more ideas tickling the back of my brain, but I can’t pull them out to the front yet. If I do, I’ll add more if you’re interested (what’s another edit or two after the first half-dozen, right? 🙂 ).