Page 1 of 1

Remove exception from Renderable::getCustomParameter()

Posted: Tue Oct 23, 2007 9:22 pm
by _tommo_
as the title says, I'm very annoyed by this Exception because i have a code like this:

Code: Select all


for(...)
{
//move the prev matrix to the next positions
					subEnt->setCustomParameter(4, subEnt->getCustomParameter(0) );
					subEnt->setCustomParameter(5, subEnt->getCustomParameter(1) );
					subEnt->setCustomParameter(6, subEnt->getCustomParameter(2) );
					subEnt->setCustomParameter(7, subEnt->getCustomParameter(3) );					

					//then fill the first 4 indexes with the current matrix
					subEnt->setCustomParameter(0, Vector4( currWVP[0] ));
					subEnt->setCustomParameter(1, Vector4( currWVP[1] ));
					subEnt->setCustomParameter(2, Vector4( currWVP[2] ));
					subEnt->setCustomParameter(3, Vector4( currWVP[3] ));
}
Well, this code is executed for each entity in a list, but i can't know when i will get a subEntity that hasn't set the parameters 0,1,2,3.
So, if i try an "if(subEnt->getCustomParameter(0))" it crashes when false, and if i leave it like now it will crash at startup when the parameters aren't initialised...
I prefer to avoid storing a temp matrix for performance reasons.

IMHO, the exception should be removed or there should be another method to know if a parameter exists... :?

Posted: Tue Oct 23, 2007 9:52 pm
by Falagard
You could try/catch the exception and it wouldn't crash when it happens.
It's less than ideal (in case you want to turn on breaking on exceptions to catch errors) but it works.

Posted: Tue Oct 23, 2007 10:00 pm
by _tommo_
I thought at catching but i discarded the idea because i have to execute this every frame and for all the visible subentities... and i thought that try/catch is much slower than an if.

A suitable alternative could be a listener that tells me when an entity is added in the SceneManager to initialise there the values, but i couldn't find a thing like that.

I think that i will overwrite the createEntity method in my scenemanager...

Posted: Wed Oct 24, 2007 12:40 am
by Numsgil
C# has a similar problem when you're converting a string to a number (parsing it). It solves it with a "try" function. So maybe an additional function:

subEnt->tryCustomParameter(4, subEnt->getCustomParameter(0) );

That "tries" to input the custom parameter, and returns a boolean representing success or failure for any reason.

Posted: Wed Oct 24, 2007 3:51 am
by xavier
_tommo_ wrote:I thought at catching but i discarded the idea because i have to execute this every frame and for all the visible subentities... and i thought that try/catch is much slower than an if.
Use the try block before assuming that it's unacceptably slower than an if. It's only a problem if it excepts all of the time, in which case you should probably figure out another method of performing the task. Constant branch prediction misses on failed conditionals aren't that much faster.

Posted: Wed Oct 24, 2007 11:54 am
by _tommo_
Thanks for all the replies, i solved the problem initializing the 4 values directly in my scenemanager's createEntity method :P .
This way it works perfectly and i avoid any conditional branching.

Now i agree with you that a try/catch is the best solution, but still sometimes a "bool Renderable::isCustomParameterInitialised(index)" could be useful.