Trying to create dynamic actors

I am making a simulation with dozens and eventually thousands of persistent simple actors. They need to be made dynamically, because I have no idea how many there will be when the program ends. When two actors meet, they may produce a third and possibly fourth actor. I thought new and delete was the way to go. But I can't figure it out. I've even looked at some rpg source and have come up with bupkis. Is there someone who understands what I am trying to do and can be a guide and sounding board for this problem? This program will be my masterpiece and teacher. I've finally come up with a program that will challenge me and keep my interest. I am just a beginner/semi-beginner. And programming is not my job career...yet.

I guess a little background would be helpful.
I am fairly confident with basic use of the following-
variables, functions, class/struct, namespaces, references, pointers, control structures(if-else;switch; etc...), static, extern, constant, mathematical operators, arrays, function overloading, and some others like these. I don't know if this will be enough to solve my problem but I am willing to try anyway. I've been told that trying things I don't know how to do is the best way to learn to do it. And I believe it. I wasn't told, however, that it would be so mind bending.

Problematically yours
-C.K.
A.K.A. lare26
Last edited on
Well, you surely need dynamic memory here. But every time you use new it returns a pointer which needs to be stored somewhere. The solution would be to have a dynamic array, but that may be a little difficult to write. Luckily you have STL for that. As I don't know what your problem is, I can't say which container you need. If you're not familiar with STL, see http://cplusplus.com/reference/stl/ . It is really useful.
Thanks for the heads up hamsterman. I am looking into it now. I will return with a better description of my problem/question, soon.
This is part of my class declaration. I hope this helps understand my problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Actor.h

//there will be many "people" simulated
//actors will interact, interactions will adjust each variable through an equation
/*
the greater the difference between each actors personality variables there is
the greater the mutual adjustment to bring said variable closer to matching
*/

#ifndef ACTOR_H
#define ACTOR_H

class Actor
{
    public:
        Actor(void);
        virtual ~Actor(void);
        //(inventive / curious vs. consistent / cautious). Appreciation for art, emotion, adventure,
        //unusual ideas, curiosity, and variety of experience.
        int GetOpenness(void) { return m_Openness; }
        void SetOpenness(int val) { m_Openness = val; }
        //(sensitive / nervous vs. secure / confident). A tendency to experience unpleasant emotions
        //easily, such as anger, anxiety, depression, or vulnerability.
        int GetNeuroticism(void) { return m_Neuroticism; }
        void SetNeuroticism(int val) { m_Neuroticism = val; }
        //actors will eventually die of old age
        int GetAge(void) { return m_Age; }
        void SetAge(int val) { m_Age = val; }
        //male or female
        int GetGender(void) { return m_Gender; }
        void SetGender(int val) { m_Gender = val; }
        //females in relationships with males may become pregnant, add actor(s) to simulation
        bool GetPregnant(void) { return m_Pregnant; }
        void SetPregnant(bool val) { m_Pregnant = val; }
        //each actor needs an id number to track it
        int GetActor_Number(void) { return m_Actor_Number; }
        void SetActor_Number(int val) { m_Actor_Number = val; }
    protected:
    private:
        int m_Openness;
        int m_Neuroticism;
        int m_Age;
        int m_Gender;
        bool m_Pregnant;
        int m_Actor_Number;
};


I need this class to spawn new actors when two existing actors have a kid together. Actors will also need to be removed from the program when they die. Their emotional stats will adjust when interaction occurs with each actor. I read vector faqs and only have come up with:

 
Actor * first = new Actor();


But it looks like the program will need every actor object to be declared before runtime. Am I not understanding vector containers well enough? Please give me some kind of hint. Because I cannot understand how to use vectors to create class objects dynamically. Am I going about it all wrong?
Since there will be a lot of inserting and erasing, you should probably use std::list, not vector. But that's not hte point. a quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::list<Actor> my_list;

for(int i = rand()%10+1; i > 0; i--){
    Actor a;
    a.SetAge(rand()%90+1);
    my_list.push_back(a);
}//add a random number of actors to the list

std::list<Actor>::iterator it, tt;
for(it = my_list.begin(); it != my_list.end(); it++){
    std::cout << "There is an " << it->GetAge() << " year old actor\n";
    if(it->GetAge() > 80){
        std::cout << "He/she died\n";
        it = my_list.erase(it);//erase returns an iterator to the next element.
        if(it == my_list.end()) break;//if this element already was the last one, it will return my_list.end().
        //in that case it++ will fail, so I break out of here.
    }
}


As for describing the problem, the interesting part is the 'meeting'. I really don't know what you mean by that. Will you just take random pairs of actors any apply a function to them? Or will you take every possible pair?
Thanks for your help, hamsterman.

They will travel along a grid and when two actors of opposite genders come to the same location they will 'mate' if they are compatible and the female will become pregnant. A time after she will 'birth' an 'infant' actor. Any two actors will interact when they are at the same location causing 'personality' changes. There may even be murder among some actors if they are psychotic enough. The actors will essentially be living a virtual 'life' on a simplistic grid like map.

I am unsure of a few things, though.
Can you explain the elements of this code to me. I understand most parts. But I don't get some parts and how they act together to form the instructions. For example:

I got the for loop down pat. I don't understand line nine.
std:: I understand how namespaces work.
list<Actor> I think this initializes a list for an Actor object?
::iterator I think this calls some kind of function? I've seen the word in the vector reference.
it, tt; I'm not sure if these are objects being created or just variables?
And I think line ten starts "iterating" through all the objects of class Actor.
it->GetAge() This is how the program would call or use a member function. Correct?

Can you explain the iterator for me? I am not entirely sure how it works. Please use human english. LOL. Thanks ahead of time.

P.S. Please understand, I had to teach myself all I know about C++(My only language).
Last edited on
I think this initializes a list for an Actor object?
I suppose 'declares' would be a better word (not that it would make a difference..). std::list (like (std::)vector, deque, set, map and etc.) is a template class (STL = standard template library). Actor is the template parameter.

I think this calls some kind of function? I've seen the word in the vector reference.
itrator is a class name. Are you confused be multiple :: ? That's a scope operator. Consider a example:
1
2
3
4
5
6
7
8
9
10
namespace n{
   int var0;
   void func0(){};

   class cl0{
      static void func1(){};

      class cl1{};
   };
}
Then the following are valid:
1
2
3
4
5
n::var0 = 5;
n::func();
n::cl0 object0;
n::cl0::func1();
n::cl0::cl1 object2;


I'm not sure if these are objects being created or just variables?
objects are variables..

And I think line ten starts "iterating" through all the objects of class Actor. <..> This is how the program would call or use a member function. Correct?
Yes. You see, iterators pretend to be pointers. Example:
1
2
3
Actor arr[5];
Actor* it;
for(it = &arr[0]; it != &arr[4]; it++) cout << it->GetAge();
Here &arr[0] (like my_lit.begin() ) marks te beginning of the array, and &arr[4] (like my_list.end() ) marks the end. As for ->, this is how you call a member function (or get a variable) when you have a pointer (or an iterator, in this case). If you have an object, use . operator.
Last edited on
I'll definitly have to look through your posts to understand it all well enough to use it effectively. Thanks for all your help.

Would you like to be apart of the project as an advisor? I could sure use the help. I'll give you the contact details upon your acceptance. I promise it will be painless...mostly. :)

Thanks again, hamsterman.
Don't you think it would be wiser to post your problems here? Other people may know better solutions to some problems..
I put my project on source forge. Do you think that was a bad idea?
not at all..
Your invited to join if you want.
Topic archived. No new replies allowed.