void InputHandler_StageDebug::WriteEntityTreeToFile( boost::shared_ptr<CStage> pStage )
{
	using namespace gregorian;

	char dest_filename[512], time_str[64];

	ulong current_time_ms = GlobalTimer().GetTimeMS();

	if( 500 < (current_time_ms - m_EntityTreeFileLastOutputTime) )	// don't output more than once in half a second
	{
		EntityManager* pEntSet = pStage->GetEntitySet();

		sprintf( time_str, "%.3f", (double)current_time_ms / 1000.0 );

		string stage_script_name = pStage->GetScriptName();
		replace_chars( stage_script_name, '/', '-' );
		replace_chars( stage_script_name, '\\', '-' );

		// create the directory for entity tree files (YYYYMMDD)
		filesystem::path entity_tree_directory = "./debug/entity_trees-" + to_iso_string(day_clock::local_day());
		boost::filesystem::create_directories( entity_tree_directory );

		sprintf( dest_filename, "entity_tree-%s[%s].txt", stage_script_name.c_str(), time_str );

		filesystem::path dest_filepath = entity_tree_directory / dest_filename;

		// save the entity tree to disk
		pEntSet->WriteEntityTreeToFile(dest_filepath.string());

		m_EntityTreeFileLastOutputTime = current_time_ms;
	}
}