Jump to content
  • Advertisement
Sign in to follow this  
RawBeen

c++ objects. when to create them?

This topic is 3571 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

Maybe I'm sitting upon a misconception here. But lets say I have a 2d map that is partially automaticly generated. On this map, there is enemies, or somthing. Should I have each enemy be an instance of an Enemy-class? Or should there be like an array that contain the enemies? My question is pretty much: how do you go about when having alot of stuff that should behave individually alike? i.e enemies. Same goes for shots from guns.

Share this post


Link to post
Share on other sites
Advertisement
I am not completely sure about what your question is, but i will try to answer.

All the enemies could inherit from a class, lets call it Enemy. Then you can make an array(or vector) of pointers to Enemies. These pointers can contain any thing that is inheriting from enemy, but can only call methods that enemy has. So if you want the behaviour of the enemys to be inside their own object, then make a virtual function called something like act(), and overwrite it in the classes that is inheriting from enemy. then iterate through all enemies, and call their act() method in every frame. (i think this is called polymorphism)

Normally you would not only store Enemies, but every actor in the game in that array/vector, so you could call the base clase actor instead.

Sorry for my bad english

Share this post


Link to post
Share on other sites
Quote:
Original post by RawBeen
Should I have each enemy be an instance of an Enemy-class? Or should there be like an array that contain the enemies?

Those suggestions aren't mutually exclusive.

Share this post


Link to post
Share on other sites
I'm totally BFF with bad english.

That sounds totally like something that I'd wanna try doing. Thanks.

I didn't really mean anything specific code-wise, but conceptually what I wanna know is how not to need to declare a new enemy for every new enemy.

And that sounds like what you described. next: googleton, I guess

Share this post


Link to post
Share on other sites
Quote:
Original post by RawBeen
Should I have each enemy be an instance of an Enemy-class? Or should there be like an array that contain the enemies?

Very probably, both.

It's difficult to give a precise answer without knowing the specific requeriments of your application.

But, generally speaking, you probably have a base Entity class and your levels probably contain a list [1] of entities in the world (so that you have a way to access all these entities in a uniform way, which is very useful for both logic and rendering purposes). When loading a level file --and assuming that file contains the data for every entity in the level--, you read through the entity data and repeatedly call your object factory [2] to create an entity of the relevant type (following your example, an Enemy) and store it in your list.

Similar entities definitely share some common state. What you need is to divide the data for each entity into data that is common to all the instances of a given entity type (for example, the geometry and animation data) and instance-specific data (for example, the position data). You abstract away the common data, storing it only once. Then, your entities contain only the entity-specific data and a reference to the common data that is shared between similar entities. [3]

[1] Be it a std::vector, a std::list, or a totally different data structure, depending on the concrete needs of your application.

[2] See: factory method pattern

[3] See: flyweight pattern

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!