Game Programming and Development Tools

Undefined Number of Classes – Mene-Mene

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
I'm writing a Multiplayer-Role-Playing-Game. Don't worry, I don't plan to have everyone on at the same time, but more of an organizer, and records keeper. I'm writing it in C++ to give myself a little challenge, and learn. One of the awesome benifits of BM is the undefined arrays with an unlimited number of elements openly changeable.

I'd like to in my game allow my Users (players playing the game) to have an unlimited amount of Characters, but as far as my knowledge you can't even have an array of Classes, let alone an undefined array (not sure if possible in C++) of Classes. Is there a way to make this possible?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ
"I would much rather say that every time you make a choice you are turning the central part of you, the part of you that chooses, into something a little different from what it was before." -C.S. Lewis, Mere Christianity

I reserve the full right to change my views/theories at any time.

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
Hmm strange, I posted a reply to this thread earlier... Test 123...

------------------
1Jo 2:9-10 He that saith he is in the light, and hateth his brother, is in darkness even until now. 10 He that loveth his brother abideth in the light, and there is none occasion of stumbling in him.
Joh 17:26 And I have declared unto them thy name, and will declare it: that the love wherewith thou hast loved me may be in them, and I in them.

[VoHW] (Help needed) [Blog] - Truedisciple (mp3)

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
okay seems to work... now to you answer your question mene-mene, it should be possible using some container class like STL's list. Like this:


#include <list>
using namespace std;
list<Character*> lCharacters;
lCharacters.push_back(new Characters());

Where Character is the c++ class.

------------------
1Jo 2:9-10 He that saith he is in the light, and hateth his brother, is in darkness even until now. 10 He that loveth his brother abideth in the light, and there is none occasion of stumbling in him.
Joh 17:26 And I have declared unto them thy name, and will declare it: that the love wherewith thou hast loved me may be in them, and I in them.

[VoHW] (Help needed) [Blog] - Truedisciple (mp3)

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
Thanks for the help, not entirely sure what you mean by your example. What about undefinesd size arrays? I'm considering just making the limit 24/23.

Edit: nvm. I'll just make the limit of player characters to 24, shouldn't have more than that anyway.

Edit 2: I'm recieving a weird error saying that, "44 D:\My Documents\Computer Programing\C++\SW PNR II.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)' ". Here's a cut-up part of the code,


int User::NewUserInfo() {
cout << "What's your name?";
cin >> UserName;
cout << endl;
ofstream UserFolder ( UserName );

The last line is the one thats generating the error.

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ
"I would much rather say that every time you make a choice you are turning the central part of you, the part of you that chooses, into something a little different from what it was before." -C.S. Lewis, Mere Christianity

I reserve the full right to change my views/theories at any time.

[This message has been edited by Mene-Mene (edited February 25, 2007).]

[This message has been edited by Mene-Mene (edited February 25, 2007).]

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
Yeah ostream is bit silly because it doesnt take string but const char*
So you have to do:

ofstream UserFolder ( UserName.c_str() );

to get the "const char*" (pointer to the string)

------------------
1Jo 2:9-10 He that saith he is in the light, and hateth his brother, is in darkness even until now. 10 He that loveth his brother abideth in the light, and there is none occasion of stumbling in him.
Joh 17:26 And I have declared unto them thy name, and will declare it: that the love wherewith thou hast loved me may be in them, and I in them.

[VoHW] (Help needed) [Blog] - Truedisciple (mp3)

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
how about a vector??

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

luke

Member

Posts: 311
From: I use your computer as my second Linux box
Registered: 10-30-2005
This may be beating a dead horse now, but dead horses are easy beatn heres a top off on
ArrayList (Java class, but the concept will do)
LinkedList (Ever heard of it? std::list)

Alrighty, arraylist is essentially an array which 'dynamically resizes' itself when it needs more space. What this means is that when it runs out of space, it makes a new array of size = oldSize + minAdditionalLength; and then copies over the data. If you are dealing with large amounts of data, you want to avoid those copy operations.
Worst case access: O(1); (its an array)
But it can 'randomly' slow down while copying data.

Linked list: Is essentially defined as:
template< class T >
class ListNode
{
ListNode* nextNode;
T* data;
}

Think about that and you will come to two conclusions.
A: A linked list class provides an interface to manipulate listNodes.
B: This is a very efficient form of dynamic memory storage. BUT access time is O(n) which can be very bad with large amounts of data.

For your specific case, only an addict should have more than 10 characters or some other reasonable number, so I would say LinkedList is your bet. The C++ equivalent of LinkedList is std::list, as suggested above.

------------------
Life is merely something from which none of us will escape alive

[This message has been edited by luke (edited February 26, 2007).]