PDA

View Full Version : Improving Entity::PrintTreeAsText( int indent /*= 0*/ )



BigBMan
08-14-2011, 10:49 PM
This bit of extra code (see #ifdefs) helped me debug a lot of UI code and better understand how Entity/Component layouts work. Hope it helps you!

In Entity.cpp:


void Entity::PrintTreeAsText( int indent /*= 0*/ )
{
#ifndef _DEBUG
return;
#endif
string us;

for (int i =0; i < indent; i++)
{
us += " "; //make the tree have branches more visually
}
us += m_name;

#ifdef _DEBUG
// Print pos/size of Entity
CL_Vec2f vPos = GetVarWithDefault("pos2d", CL_Vec2f(0, 0))->GetVector2();
CL_Vec2f vSize = GetVarWithDefault("size2d", CL_Vec2f(0, 0))->GetVector2();
us += "[" + toString(vPos.x) + "," + toString(vPos.y) +
"," + toString(vSize.x) + "," + toString(vSize.y) + "]";
#endif

if (!m_components.empty())
{
us += " (";
ComponentListItor itor = m_components.begin();
for (;itor != m_components.end(); itor++)
{
if (itor != m_components.begin()) us +=", ";
us += (*itor)->GetName();

#ifdef _DEBUG
// Print pos/size of Entity
vPos = (*itor)->GetVarWithDefault("pos2d", CL_Vec2f(0, 0))->GetVector2();
vSize = (*itor)->GetVarWithDefault("size2d", CL_Vec2f(0, 0))->GetVector2();
us += "[" + toString(vPos.x) + "," + toString(vPos.y) +
"," + toString(vSize.x) + "," + toString(vSize.y) + "]";
#endif
}

us +=")";
}

LogMsg(us.c_str());

EntityListItor itor = m_children.begin();

while (itor != m_children.end())
{
(*itor)->PrintTreeAsText(indent+1);
itor++;
}
}

Seth
08-15-2011, 12:25 AM
Nice! Although there is really no reason to print the size2d/pos2d from the entities' components (referring to the second part, not the first), I think in all cases they connect to their parent entity variable, and don't create one at the component level. So they should always be zero.

BTW, If you'd like to see ALL the internal variables listed, you can do:

<ent or component>.GetShared().Print(); on an entity or component.