Remove exception from Renderable::getCustomParameter()

What it says on the tin: a place to discuss proposed new features.
Post Reply
User avatar
_tommo_
Gnoll
Posts: 677
Joined: Tue Sep 19, 2006 6:09 pm
x 5
Contact:

Remove exception from Renderable::getCustomParameter()

Post 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... :?
User avatar
Falagard
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 2060
Joined: Thu Feb 26, 2004 12:11 am
Location: Toronto, Canada
x 4
Contact:

Post 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.
User avatar
_tommo_
Gnoll
Posts: 677
Joined: Tue Sep 19, 2006 6:09 pm
x 5
Contact:

Post 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...
User avatar
Numsgil
Gremlin
Posts: 197
Joined: Sun Jan 29, 2006 10:20 pm

Post 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.
Darwinbots, leading amateur artificial life simulator.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Post 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.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
_tommo_
Gnoll
Posts: 677
Joined: Tue Sep 19, 2006 6:09 pm
x 5
Contact:

Post 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.
Post Reply