Game Development Community

Material Naming Quirk

by William Todd Scott · in Torque Game Engine Advanced · 12/14/2006 (10:55 pm) · 3 replies

Hi,

So, I just spent a bit of time trying to figure out why a material I had wasn't rendering. It turns out that if you name a material with the same name as some other simobject, there is a good chance that the material will not function.

The issue is in the MaterialList::mapMaterials() function. The function calls sim::findobject() to find the material and then uses a dynamic_cast to cast it into a Material object. However, if the material is named the same as some other simobject that is not a material, then the findobject() function could return the non-material object. If that happens then the material will not be mapped in the material instance list. The result is that the material wont render.

This isn't really a bug, so much as a requirement that materials have unique names. However, it would be nice that if this problem occurs then we at least get a console error. So, I added the following console error message to the MaterialList::mapMaterials() function:

if(entry && entry->materialName)
      {
         Material *mat = dynamic_cast<Material*>( Sim::findObject( entry->materialName ) );
         if( mat )
         {
            MatInstance * matInst = new MatInstance( *mat );
            mMatInstList[i] = matInst;
         }
        else
        {  //ADD THIS
           mMatInstList[i] = NULL;
           Con::errorf("Did not find material %s.  This is usually caused by a material having the same name as some other asset!", entry->materialName);  //ADD THIS
         }  //ADD THIS
      }

edited for clarity

#1
12/15/2006 (1:06 am)
Yes, thats the case across the board for more than Materials. Anything that uses Sim::findObject will exhibit that issue. Theres a possibility of making a Material Group and adding every Material to it, then just doing a find in that, which might fix it... not sure though
#2
12/15/2006 (2:07 am)
I actually forgot that all materials are already added into their own SimSet, that could be searched inside of instead of a global findObject. This "should" allow naming of Materials and other objects to be the same
#3
12/15/2006 (9:30 am)
Yeah,

Using the material list instead of the global search would be quicker too.

But as a rule, I think it would be nice if we print an error on cases like this where the dynamic cast comes back NULL. Naming errors are almost inevitable and the warning message really makes it easy to fix.

Todd