From void* to different data types.

Started by
3 comments, last by Programmer16 20 years, 3 months ago
I have an event class that looks like this:

class CEvent
{
	void* m_pData;
	EVENT_TYPE m_etEventType;
	D3DXVECTOR2 m_vPos;
public:
	void SetData(void* pData){	m_pData = pData;	}
	void SetEventType(EVENT_TYPE etEventType){	m_etEventType = etEventType;	}
	void SetPosition(D3DXVECTOR2 vPos){	m_vPos = vPos;	}
	void SetPosition(int x, int y) {	m_vPos = D3DXVECTOR2((float)x,(float)y);	}

	int GetX(){	return (int)m_vPos.x;	}
	int GetY(){	return (int)m_vPos.y;	}
	void* GetData(){	return m_pData;	}
};
Then I make a global instance of it named g_eMyEvent. Then I set the data using this: g_eMyEvent.SetData("Message Event test."); g_eMyEvent.SetEventType(MESSAGE); g_eMyEvent.SetPosition(0,0); And check if the player is at that position using this:

void UpdateLogic()
{
	if(g_Engine.GetPlayerX() == g_eMyEvent.GetX() && g_Engine.GetPlayerY() == g_eMyEvent.GetY())
	{
		g_bUpdateMessageArea = TRUE;
		strcpy(g_pMsgAreaTotal[g_iMsgAreaTotalIndex],(char*)g_eMyEvent.GetData());
		g_iMsgAreaTotalIndex++;
	}

	if(g_bUpdateMessageArea == TRUE)
	{
		strcpy(g_pCurrentMsgArea,g_pMsgAreaTotal[g_iMsgAreaCurrentIndex]);
		strcat(g_pCurrentMsgArea,"\n");
		strcat(g_pCurrentMsgArea,g_pMsgAreaTotal[g_iMsgAreaCurrentIndex + 1]);
		g_bUpdateMessageArea = FALSE;
	}
}
It loads and displays it, but then closes and gives me an access violation. Does anybody know what I''m doing wrong? Thanks in advance.
Advertisement
I've figured that I get the bug if I stay on the event, or if I walk over it more than once. So, I figure its probably this line:
strcpy(g_pMsgAreaTotal[g_iMsgAreaTotalIndex],(char*)g_eMyEvent.GetData());

But why would my computer have a problem with copying it more than once? Thanks again.

[edit]Update:
I tried making a derived class, called CMsgEvent and using a char* and I still get the error, so it's not the void* like I thought it was.

[edited by - Programmer16 on January 17, 2004 11:46:10 AM]
If g_MyEvent.GetData() does not have a \0 (null char) at the end of whatever it returns, then strcpy is not going to stop, since it only stops at null chars. So basically the strcpy will overwrite all your program memory and maybe someone else''s until Windows gets pissed off.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Ok, I figured it out. When I was making my array for holding my message (g_pMsgAreaTotal) I was doing it like this:
char g_pMsgAreaTotal[50][256] = {{"Welcome to Valkyria Legends."}};
And thats where my error was coming from.
Thanks Promit, I didn't even realize I didn't have that in there. One more question, about my display. I have 2 variables, one that represents the total number of lines in the array, and the current index, for scrolling up and down. I use this algorithm to increase the current index if I need to:
if(g_iMsgAreaTotalIndex % 2 == 0)
g_iMsgAreaCurrentIndex++;
And it works the first 2 or 3 times, but after that it doesn't. Any ideas? Thanks again.

[edit] : Nevermind, I figured out that all I have to do is:

if(g_iMsgAreaTotalIndex > 2)
g_iMsgAreaCurrentIndex++;

lol. Thanks for all the help!

[edited by - Programmer16 on January 17, 2004 12:37:06 PM]

This topic is closed to new replies.

Advertisement