Game Development Community

Solution for SimGroup-caused bugs

by Bryan Edds · in Torque Game Engine · 09/18/2005 (2:40 am) · 4 replies

Notice how SimGroup's one-owner rule makes for a LOT of insideous bugs when you add one object to a second SimGroup, thus removing it from the first SimGroup without noticing? Here's some code to make the console spit out an error when this happens. Replace the whole SimGroup::addObject() function with this one -

void SimGroup::addObject(SimObject* obj)
{
   if (obj->mGroup != this) {
      if (obj->mGroup)
      { /// CIB BY BRYAN EDDS - SimGroup Warning
         if(!obj->mGroup->objectName || /// CIB BY BRYAN EDDS - SimGroup Warning
			obj->mGroup->objectName && /// CIB BY BRYAN EDDS - SimGroup Warning
			dStrcmp(obj->mGroup->objectName, "GuiGroup") != 0 && /// CIB BY BRYAN EDDS - SimGroup Warning
			dStrcmp(obj->mGroup->objectName, "RootGroup") != 0) /// CIB BY BRYAN EDDS - SimGroup Warning
			Con::evaluate("error(\"Warning!!! SimGroup member changed membership!!!\");"); /// CIB BY BRYAN EDDS - SimGroup Warning
         obj->mGroup->removeObject(obj);
      } /// CIB BY BRYAN EDDS - SimGroup Warning
      nameDictionary.insert(obj);
      obj->mGroup = this;
      objectList.push_back(obj); // force it into the object list
                                 // doesn't get a delete notify
      obj->onGroupAdd();
   }
}

Now you'll know when this happens and this wil keep those SimGroup bugs from wasting hours of your time.

Happy coding!

#1
09/18/2005 (3:37 pm)
Just realize that removal from the previous simGroup is desired behaviour (it's what makes a simGroup separate and distinct from a SimSet).

As long as all you are doing is posting a warning, that's fine!
#2
09/18/2005 (4:15 pm)
Oh yeah, it's definitely meant as a warning. There are times when this behavior is desired. For me personally, the effects of the defined behavior is more often accidental and problematic than not. I don't generally use SimGroups for their single-association properties. I mostly use them for their auto-deletion capabilities. Their single-assocation property becomes more of a liability side-effect for me when I forget the collection is a Group and not a Set.

Could just be me though.
#3
09/19/2005 (6:44 am)
Yes, but the auto-deletion capability only exists because of the single-association property! If an object was in more than one simGroup, then it would be much more difficult to allow for auto-deletion of objects within the Group when the Group is deleted.
#4
09/19/2005 (7:48 am)
That's true. It's just nice to have the above warning for when you accidentally make mistakes with it. I've made many :)