Jump to content
  • Advertisement
Sign in to follow this  
n1rium

Creating objects of a class associated with map-arrays?

This topic is 2892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!