Haunted Light – ObjectManager

This week we’ve been working hard on getting an alpha product done for the presentation that will be held in from of the class.

I’ve made some finishing touches on the light engine so that it e.g. no longer have transparent shadows. This was accomplished by using blend modes. I’m quite familiar with using blend-modes because of my previous projects when I used to program a lot inside the game engine Game Maker. But I’ve gotten more and more used to how SFML works now and it feels great!

But enough of that. Today I’m going to write about how Haunted Light handles all it’s different objects.

Haunted Light - ObjectManager

A screencapture of the class; “ObjectManager” which this post will cover.

First off, all objects inside Haunted Light except the player gets an ID assign to itself when it’s added to the ObjectManager class after it’s creation. This ID is a static int which has a starting value of 0. That value’s incremented by one each time it’s used to assign it’s value to an object. This prevents any object from having the same ID as another one.

Next up I’ll go through all the functions, arguments and member variables which the ObjectManager class contains and what each function’s purpose is.


  • ObjectManager()
  • void Add()
  • int getObjects()
  • bool setActiveDepth()
  • void Draw()
  • void CheckCollision()
  • int atPosition()
  • void destroy()
  • void Cleanup()

Member Variables

  • std::map<int,GameObject> m_objects
  • int m_max_z
  • int m_max_z
  • static int ID

How it all works

  • The function “ObjectManager()“, is the first function that’s called when the ObjectManager object gets created. It does not have any argument and it’s currenly only a placeholder if I want to have anything happen when the ObjectManager object’s created. This function is called a default contructor and makes it possible for the object to be created. Without it you wouldn’t be able to create an object out of a class.
  • The next funtion “Add()“, takes two arguments. The first one is a pointer named “_object” of the type GameObject which is the oject that you want to add (hence the name). The second one is of the type integer and is named”_depth“, this argument defines at what z-index i.e layer that the object will be drawn on.
  • getObjects()” is a simple function which returns the amount of objects that’s stored in the ObjectManager.
  • The “setActiveDepth()” function is an intresting one. This function takes two arguments of the type integer; “_min” and “_max“. Those two values define the range which the ObjectManager’s allowed to draw objects in. E.g. if you want to only draw the floors of the level which has a depth-value of 5 , then you’d use the “setActiveDepth()” function with the two arguments being “5”.
  • Draw()” is a simple function that only have one task; rendering all the objects which the ObjectManager contains.
  • The “CheckCollision()” function is a leftover from when we didn’t have a collision-manager, which we now do. And it’s no longer in use since all the collisions are handled by the CollisionManager class.
  • This useful function; “atPosition()” have one argument that’s the position on which you want to check if there is an object. If there is, then that objects ID is returned. If there’s no object at that particular position, then the special value; “-1” is returned.
  • If you want to have an object deleted, then this next function; “destroy()” is of great use. This function takes one argument of the type integer. This value is the object you want to delete’s ID. The function performs a linear search alghoritm and finds what object you want to have deleted and deletes it from the manager.
  • Since the ObjectManager is state-specific then you’d want it to clean up when it’s done. That’s what this function’s made for. When it’s called, it goes through the member variable; “m_objects” and deletes each object from memory.

Last week I covered our SpriteManager and now you also know how our object handling works. Next week I’ll cover another part of our development!

~Lead Programmer, Per “Gimmic” Johansson

One thought on “Haunted Light – ObjectManager

  1. Hello Per!
    I can see that you´ve have have put a lot of effort into making this object manager dynamic. At first I could not see why you would store the objects in a map and not an array or a vector. But then when I say your delete method I knew what you meant.
    But wouldn’t it be more efficient if instead of giving an identifer to each object, you give each object its very own type. Now you can delete a whole group of a different type.
    Or if you still consider the STL map way of doing this, slope the static property since (from what I read) you only use it to increment added objects unique id. To find out the unique id, take the last index in your map and increment it by one. Wouldn’t that be the same result?
    A smart thing to do is making your class drawable and call draw on each object that can be drawn. This will draw all of your objects the right way.

    And now onto some feedback.
    It would have been great if we could see some examples of how to use this object manager.
    I couldn’t not find an explanation to why the object manager is even needed and why the different methods and techniques are implemented.

    Is it clear what is done? Yes
    Is it clear how its done? Yes
    Is it clear why its done? Not so much.

    It would also be great if you would show an up-to-date image or sample of the game progress.
    It is great to see an another student thoughts in this subject. I struggled a lot with this artifact as well and I gave it up in the end. But that is what Lipponen said to us that if you get a unusual large count of objects, an object manager would only make your life much more “pain in the ass”-er but from what I can see, you have no trouble at all and that is very interesting.
    Do you have the source code on Github or somewhere that I can take a look at it?

    Good luck with your project and may the force be with you.
    Anthon Fredriksson

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s