Problem with loop

Started by
7 comments, last by JimboC 21 years, 7 months ago
I've been looking at this for hours and I can't figure out what I'm doing wrong. Everything works fine until one of my objects goes off the screen. When the ToBeKilled flag is set to 1, the following code kills off the object like it's suppoed to, but then it kills of every object for the rest of the loop as well. Can anyone see what I'm doing wrong?

for ( int Num = 0; Num < g_nNumberOfGameObjects; Num++ )
{
	if ( m_pGameObject[Num]->ToBeKilled == 1 )
	{
		m_pGameObject[Num] = NULL;
		m_AddObject ( Num );
		m_pGameObject[Num]->m_Spawn ( );
	}
	m_pGameObject[Num]->m_Transform ();
        m_pGameObject[Num]->m_Render ();
}  
- m_pGameObject is an instance of a class to keep track of the game objects. - m_AddObject(Num) populates a game object with a mesh - m_Spawn() resets all variables to 0 and then picks new values. I've definitely isolated the problem to this loop, but can't figure out beyond that what I've done wrong. Using VC++.NET and DirectX 8.1. Thanks. Edit - From looking at the values, it appears that whatever values are present when the if staement gets triggered are then used for all the objects remaing in the loop. [edited by - JimboC on September 1, 2002 11:56:55 AM]
Advertisement
Dunno if this is the whole problem, but if tobekilled is true, then you set the object pointer to null. After the if(), though, you call functions in an object that you just set to null.

---
John Hattan
The Code Zone
Sweet software for a saturnine world

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

quote:Dunno if this is the whole problem, but if tobekilled is true, then you set the object pointer to null. After the if(), though, you call functions in an object that you just set to null.
I should've cleaned that up before I posted. While trying different things I've added the m_pGameObject[Num] = NULL; line to the loop and added m_pGameObject[Number] = new CObject3D; to the AddObject method. I know this will be slow, but I'm just trying to get it working right now. Didn't make any difference to the problem though. Should I post more of the code?


[edited by - JimboC on September 1, 2002 12:23:35 PM]
post code for your array object ... maybe there is something there.
Here''s the code for the CObject3D Class. (m_Transform and m_Render are near the bottom):

  //////////////////////////////////////////////////////////////////////// CObject3D.cpp//////////////////////////////////////////////////////////////////////#include "Global.h"#include "CObject3D.h"//--------------------------------------------------------------------// Constructor//--------------------------------------------------------------------CObject3D::CObject3D ( ){	int ToBeKilled = 0;	// Position	float m_XLoc = 0.0f;	float m_YLoc = 0.0f;	float m_ZLoc = 0.0f;	// Rotation	float m_XRotation = 0.0f;	float m_YRotation = 0.0f;	float m_ZRotation = 0.0f;		// Temp Move Holder	float m_XMoveValue = 0.0f;	float m_YMoveValue = 0.0f;	float m_ZMoveValue = 0.0f;	// Total Movement per second	float m_XChange = 0.0f;	float m_YChange = 0.0f;	float m_ZChange = 0.0f;	// SetError ( "Constructor Done." );}//--------------------------------------------------------------------// Destructor//--------------------------------------------------------------------CObject3D::~CObject3D(){	if ( m_pMeshMaterials )		delete m_pMeshMaterials;	if(m_pMeshTextures != NULL)	{		for(DWORD i = 0; i < m_dwNumMaterials; i++)		{			if(m_pMeshTextures[i])			{				m_pMeshTextures[i]->Release ();			}		}	}	if ( m_pMeshTextures )		delete m_pMeshTextures;	if ( m_pMesh )		m_pMesh->Release ();}//--------------------------------------------------------------------// CObject3D::m_LoadObject ( LPSTR pFilename, DWORD dwKeyColor )//--------------------------------------------------------------------void CObject3D::m_LoadObject ( LPSTR pFilename, DWORD dwKeyColor ){		LPD3DXBUFFER pMaterialsBuffer = NULL;	if ( FAILED ( D3DXLoadMeshFromX ( pFilename, D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL, 									  &pMaterialsBuffer, &m_dwNumMaterials, &m_pMesh ) ) )	{		SetError ( "CObject3D: Mesh Failed to Load!" ) ;		return;	}    D3DXMATERIAL* matMaterials = (D3DXMATERIAL*)pMaterialsBuffer->GetBufferPointer();	m_pMeshMaterials = new D3DMATERIAL8[m_dwNumMaterials];    m_pMeshTextures  = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];    for(DWORD i = 0; i < m_dwNumMaterials; i++)    {        m_pMeshMaterials[i] = matMaterials[i].MatD3D;		m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;		if( FAILED ( D3DXCreateTextureFromFileEx( g_pd3dDevice, matMaterials[i].pTextureFilename, 0, 0, 0, 0,												  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,												  D3DX_DEFAULT, dwKeyColor, NULL, NULL, &m_pMeshTextures[i] ) ) )        {            m_pMeshTextures[i] = NULL;        }    }    pMaterialsBuffer->Release ();	D3DXComputeNormals(m_pMesh, NULL);}//--------------------------------------------------------------------// CLaserTurret::m_Render ( )//--------------------------------------------------------------------void CObject3D::m_Render ( ){	if( m_pMesh != NULL )	{		for( DWORD i = 0; i < m_dwNumMaterials; i++)		{			g_pd3dDevice->SetMaterial ( &m_pMeshMaterials[i] );            g_pd3dDevice->SetTexture ( 0, m_pMeshTextures[i] );			g_pd3dDevice->SetTextureStageState ( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );        			m_pMesh->DrawSubset ( i );		}	}	return;}//--------------------------------------------------------------------// CObject3Dt::m_Transform ( )//--------------------------------------------------------------------void CObject3D::m_Transform ( ){	D3DXMATRIX	matMove, matXRotate, matYRotate, matZRotate,				matRotTemp,	matRotate, matObject;	ToBeKilled = 0;		if ( m_XRotation != 0.0f )		D3DXMatrixRotationX ( &matXRotate, timeGetTime()/ m_XRotation );	if ( m_YRotation != 0.0f )		D3DXMatrixRotationY ( &matYRotate, timeGetTime()/ m_YRotation );	if ( m_ZRotation != 0.0f )		D3DXMatrixRotationZ ( &matZRotate, timeGetTime()/ m_ZRotation );	D3DXMatrixMultiply ( &matRotTemp, &matXRotate, &matYRotate );	D3DXMatrixMultiply ( &matRotate, &matRotTemp, &matZRotate );	if ( g_fElapsedTime != 0 )	{        m_XMoveValue = ( m_XChange * g_fElapsedTime );		m_XLoc += m_XMoveValue;		        m_ZMoveValue = ( m_ZChange * g_fElapsedTime );		m_ZLoc += m_ZMoveValue;	}	D3DXMatrixTranslation ( &matMove, m_XLoc, m_YLoc, m_ZLoc );	D3DXMatrixMultiply ( &matObject, &matRotate, &matMove );	g_pd3dDevice->SetTransform( D3DTS_WORLD, &matObject );	if ( (m_ZLoc < -50.0f) || (m_XLoc > 175.0f) || (m_XLoc < -175.0f) )	{		ToBeKilled = 1;		SetError ( "================= ToBeKilled =================" );		SetNumError ( &m_ZLoc );		SetNumError ( &m_XLoc );	}	else	{		ToBeKilled = 0;	}}//--------------------------------------------------------------------// CObject3D::m_Spawn ( )//--------------------------------------------------------------------void CObject3D::m_Spawn (){	// Zero Object	ToBeKilled = 0;	m_XLoc = 0.0f;	m_YLoc = 0.0f;	m_ZLoc = 0.0f;	m_XRotation = 0.0f;	m_YRotation = 0.0f;	m_ZRotation = 0.0f;	m_XMoveValue = 0.0f;	m_YMoveValue = 0.0f;	m_ZMoveValue = 0.0f;	m_XChange = 0.0f;	m_YChange = 0.0f;	m_ZChange = 0.0f;			m_XLoc =(150.0f * (float)(rand()/(double)RAND_MAX) ) - ( 150.0f * (float)(rand()/(double)RAND_MAX) );	m_ZLoc = 350.0f;	m_XChange =(25.0f * (float)(rand()/(double)RAND_MAX) ) - ( 25.0f * (float)(rand()/(double)RAND_MAX) );	m_ZChange =-(10.0f * (float)(rand()/(double)RAND_MAX) + 15 );					float RateOfRotation = 0.0f;	RateOfRotation = ( 1000.0f * (float)(rand()/(double)RAND_MAX) ) + 500;	if ( (float)(rand() / (double)RAND_MAX ) < 0.5f )	RateOfRotation = -(RateOfRotation);	m_XRotation = RateOfRotation;		RateOfRotation = ( 1000.0f * (float)(rand()/(double)RAND_MAX) ) + 500;	if ( (float)(rand() / (double)RAND_MAX ) < 0.5f )	RateOfRotation = -(RateOfRotation);	m_YRotation = RateOfRotation;	RateOfRotation = ( 1000.0f * (float)(rand()/(double)RAND_MAX) ) + 500;	if ( (float)(rand() / (double)RAND_MAX ) < 0.5f )	RateOfRotation = -(RateOfRotation);	m_ZRotation = RateOfRotation;}  


If all you are doing when killing an object is re-intializing it, all you should need is


  for ( int Num = 0; Num < g_nNumberOfGameObjects; Num++ ){	        if ( m_pGameObject[Num]->ToBeKilled == 1 )	{		       m_pGameObject[Num]->m_Spawn();	        }	        m_pGameObject[Num]->m_Transform();        m_pGameObject[Num]->m_Render();}    
quote:Original post by Deyja
If all you are doing when killing an object is re-intializing it, all you should need is
I''m also picking a new type of meteor when the old one dies.

I did take out the line that picks a new meteor type and the loop problem went away. Unfortunately, now it''s only displaying the first couple of meteors that are initialized.

Methinks I need to look a little closer at what''s going on when I initialize a new meteor. Thanks for the suggestions so far, I''m going to try and figure it out for myself from here.

Very odd.

I moved the exact same code to it''s own method and everything started working.

Well, that''s 7 hours of my life I''ll never get back...
I wonder if you forgot to do a "Rebuild All?"

Usually that fixes those type of problems where the old code is still in the cache.

-G|aD-

This topic is closed to new replies.

Advertisement