Jump to content

  • Log In with Google      Sign In   
  • Create Account

OOP with vectors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Gravious   Members   -  Reputation: 124

Like
0Likes
Like

Posted 15 February 2013 - 08:57 AM

First post, I'm making my first game, building a framework and making some progress.

 

I've created a class for my entities, and i've created a std::vector of pointers to the instances so that i can loop through them to trigger a member fuction to update their logic

 

entities[i].updateLogic();

 

what i want to do though, is inside that updateLogic() member function, i want to cycle through every other member of the entity vector, but the problem i have, is i can't define the entity vector inside the entity class definition?

 

help!

 

(Thanks)



Sponsor:

#2 rip-off   Moderators   -  Reputation: 8340

Like
3Likes
Like

Posted 15 February 2013 - 09:12 AM

While it would be possible to make this work, you probably have a design flaw if you want to make every entity aware of every other entity.  Why do you want to do this?



#3 nobodynews   Crossbones+   -  Reputation: 1934

Like
3Likes
Like

Posted 15 February 2013 - 09:17 AM

There are almost certainly better solutions to your problem than what I'm about to suggest, but the quickest and dirtiest solution is to pass in a reference (I'd suggest by const) to the same vector of entities:

 

::updateLogic(const std::vector<Entity *> &entities)  { ... }

 

Or just std::vector<Entity> (you say a vector of pointers to instances, but the code you show is just a vector of instances so I'm not sure what you're actually doing). There are pitfalls to this method and you'll probably want to check that the entity which is being acted upon is not the same as the entity you're currently cycling through in the function body.

 

All that said, I highly suggest you explain *why* you want to do this and we can suggest better solutions.


C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#4 KnolanCross   Members   -  Reputation: 1317

Like
0Likes
Like

Posted 15 February 2013 - 09:22 AM

Pass the vector by reference to the method, but it is VERY likely that there is some flaw in your design if every instance need to go check every other instance.

 

EDIT: Started writing the answer before nbodynews, didn't see he had replied.


Edited by KnolanCross, 15 February 2013 - 09:23 AM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


#5 HappyCoder   Members   -  Reputation: 2670

Like
0Likes
Like

Posted 15 February 2013 - 05:47 PM

As others have pointed out, you don't want every object to know about every other object.

Consider passing reference to only the objects that need them when you create you game world. For example, if you have a switch that controls a door, then give the switch a reference that it can use to control the door.

If you need the objects for collision detection then create what I like to call a collision space. A collision space contains a collection of collision objects. Each collision object has a position and a shape. If you want to know what objects you are touching you simply ask the collision space to return a list of contacts of objects touching your object, or if you have many objects moving all the time you can have the collision space find any overlaps then alert each individual object of the overlaps that are occurring. The key here is the logic for collision detection is in its own independent module. Then you game logic doesn't have to worry about colliding anything, it just gets a list what objects it touches determines what to do based on that. That has worked well for me in the past.

#6 Gravious   Members   -  Reputation: 124

Like
0Likes
Like

Posted 16 February 2013 - 01:53 AM

Thanks for the input.  

 

The base entity class is further inherited to become the various objects my world interacts with, and i have various bools set to say wether an object is interactive, or active at all, i had thought that it would be ok to make a for() loop and cycle through the entities and only apply the logic based on the individual objects relevance to the currently running instance of the class, but i'm guessing what you're suggesting is i have some kind of external class that governs logic and sweeps through my vector and applies logic externally, instead of the entities being in control of their own logic?

 

I am new at game internal design, this is a design i thought would be ok, but i'm not married to it :)



#7 rip-off   Moderators   -  Reputation: 8340

Like
0Likes
Like

Posted 16 February 2013 - 09:10 AM

... i had thought that it would be ok to make a for() loop and cycle through the entities and only apply the logic based on the individual objects relevance to the currently running instance of the class, but i'm guessing what you're suggesting is i have some kind of external class that governs logic and sweeps through my vector and applies logic externally, instead of the entities being in control of their own logic?

It really depends on your definition of "own logic".

 

A simple example is collision. Some beginners will handle collision by getting each object to iterate through the object list, testing for collision. This heavily couples the object's logic to the list of other objects in the game. For example, you may end up littering your code with tests for objects that are "inactive" or otherwise uninteresting. If you add a new object type - for example a "trigger" object which can be used to activate a cut scene when a character enters a certain area, now you might have several loops you need to inspect to ensure that, for example, you Ogre doesn't choose the trigger as its next meal.

 

Advanced games will totally separate collision from the entities involved, usually via a physics library. An entity can own certain physics bodies, and the appropriate callback mechanism can be used to tie game logic to physics events such as collision. A middle ground for a small project is to have the calling code iterate through the list and notify the objects when a collision occurs.

 

It also depends on the scope of your game. A pong game doesn't warrant this complexity. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS