Creating objects of a class associated with map-arrays?

Started by
4 comments, last by n1rium 13 years, 6 months ago
Hi again.

My problem is that I want to create an object out of my entity class when the map array contains certain numbers:

void Draw_Map(int map[]){    for(int i = 0; i <= COLS; i++)    {        for(int t = 0; t <=ROWS; t++)        {            if(map[i*ROWS+t] == 1)            {                // Create an entity and push it onto a list.            }        }    }}


Now I have lots of "1" in my array and everytime the nested for-loop detects a "1" a new object out of the entity class should be created. The same goes for 2, 3, 4, 5 etc.. But different object with other behaviours.

How would I do this? I can't just create (entity *Name = new entity; can I?

I hope you know what I mean. I really want to solve this one out.

Cheers!
Advertisement
I'm not what the problem is in your question. Is it that you don't know how to store different types of objects into a single list, or are you just not sure how to map the number to the object?
I don't know how to create a new entity every number the for-loop finds.

int map1[24*32] = {1,1,1,1,1,1,1,1,1,1,..........};

In the loop, everytime it finds a 1 I wanna create an entity NewEntity and then push it back to a list. If the loop finds a 2 I wanna create an entity NewEntity yet again and push it to perhaps another list.

At my first post, all I really need help with is how to create a new entity from the class when it finds numbers.

int map1[2*2] = {1,1                 2,1};


That would be four entities. Three "walls" (1) and one "enemy" (2).

Something like that.
You could use a Switch Statement which maps to each number and then creates the entity you want.

For example:
Entity* entity;switch (entity_id){case 0: { entity = new Wall(); break; }case 1: { entity = new Enemy(); break; }default: break;}objectlist.push(entity);


I assume that Wall and Enemy inherit from the Entity base class. If you want, you can push to separate lists per case. Just make sure that comes before the break statement as the break statement breaks out of the switch.
Quote:Original post by enigmatix
You could use a Switch Statement which maps to each number and then creates the entity you want.

For example:
*** Source Snippet Removed ***

I assume that Wall and Enemy inherit from the Entity base class. If you want, you can push to separate lists per case. Just make sure that comes before the break statement as the break statement breaks out of the switch.


what would entity_id represent? Yes they inherit from same class.

So by creating entity* entity it can then create an unlimited amount of entites from that pointer?
I think you misunderstood me.

There is only one class. Class entity

every number should create a new object from that class.

There is no "new wall();" or "new enemy();" just a new entity.

This topic is closed to new replies.

Advertisement