C#: Creating Object Arrays?

Started by
8 comments, last by C PLUSPLUS Learner 19 years, 2 months ago
I need to make an object array for an random NPC creator. This way, each time you call this method, it increments up the array for preperation for the next NPC to be randomly created. Any help is appreciated. Phil Current code:
 
        //static long npcNum = 0;   
        Common[] npcNum = new Common[100];

        public void createRandomNPC()
        {
            npcNum[0].whatever();  // this works now. Will each increment be static?
            npcNum++;
        }
Advertisement
Public Class NPC
{
Common[] NPCArray = new Common[100];
int NPCNum=0;

Public Void CreateRandomNPC()
{
NPCArray[NPCNum].Create();
NPCNumm++
}
}

this way you make one instance of the NPC class that is good for up to 100 NPC's
you call it like this in your base class/game loop

NPC NonPlayer = new NPC();
NonPlayer.CreateRandomNPC();

you can then also create functions within the NPC class to govern the NPC's and it can contain your AI for the NPC's in a nice easy to manage place.

¡Munky!
-- there are only 10 types of people in this world, those who know binary and those who dont.
Why not use an ArrayList?

System.Collections.ArrayList

That seems to me what you're looking for.
Yeah use an arraylist as its open ended rather than a limited array.
Even if your engine needs to be limited for practicality you can enforece this on your array list as it is then open to expansion.
OR if the player can type in text rather than choosing it from a list during interaction you might want to use a Hashtable and use the NPC's names as the Hashkeys so you can find a specific one within the list by name?
I don't understand this. We have a working method here. It looks like it would work. But does it? Nah. It's giving a bug here...

// Object reference not set to an instance of an object.
npcArray[npcNum].setName(sNPCName);

    class Game    {         // createRandomNPC()          Common[] npcArray = new Common[100];        static int npcNum = 0;        Random randNum = new Random();             public void createRandomNPC(string sNPCName)        {            // Object reference not set to an instance of an object.            npcArray[npcNum].setName(sNPCName);                         npcNum++;         }     } 
Common[] npcArray = new Common[100];
This only creates an array of 100 references to Common, it does not create 100 instances of Common.

Right before:
npcArray[npcNum].setName(sNPCName);
Do:
npcArray[npcNum] = new Common(); // or however you instanciate that class
Nevermind. Creating constructors with initialized defaults are a good thing, but also it can create random NPCs as well. Just pass on a few details, and let the constructor handle the rest of the details for you :)

Lantz, I like that, too. I wonder which is better to use. I was trying to access those objects outside the class, but I can't access them.
Quote:Original post by philvaira
Nevermind. Creating constructors with initialized defaults are a good thing, but also it can create random NPCs as well. Just pass on a few details, and let the constructor handle the rest of the details for you :)

Lantz, I like that, too. I wonder which is better to use. I was trying to access those objects outside the class, but I can't access them.

You don't have to instanciate the class like I suggested, my point was just that you need to do it somewhere. And to access them you need to make the array public or access them by some method/property of the class.
I did it! I made different constructors to the NPC class, and one allows you to fully define your NPC by choice. The other one allows you to enter a few details (his name, age, race, class), and the constructor handles the rest of the details for you :) I love it! Thanks for your input, too! I may need it for other things soon.
Cool! What are you making? A game? I have just bee interested in C#(Sharp I think it is) just wondering what it is... is it more powerful than c++. If it is, no, I am not going to try to learn it yet. I am just wondering. The reason I asked if you were making game is, NPC is Non-Playable-Character, so I was just wondering.

This topic is closed to new replies.

Advertisement