I am a c++ Beginner -

Im looking to store a "Location" for a later use in this function and cant figure out how to do it still after hours of research.

if(pUnit)
	{
		if(GetUnitDist(Me, pUnit) <=10)
		{
                      //Right Here need to store GetOldPlayerX()

			if(pUnit->dwMode == PLAYER_MODE_CAST)
			{
				if(GetOldPlayerX() != GetPlayerX())
				{
					Print("Success!");
				}
			}
		}
	}

Basically im trying to detect if someone moved by storing his first position, waiting for the cast event, then checking if the new position (GetPlayerX()) is different from the OldPlayerX() but I need to learn how to store OldPlayerX() in the function.

Any help would be most appreciated.

Recommended Answers

All 4 Replies

Is GetPlayerX() and GetOldPlayerX() two methods of the same c++ class? If yes then you could just store the position as a member of that class. If not then use a global (ugg!) variable.

Is there an msdn thread somewhere I can use to get some more information?

WORD GetPlayerX() {
LPUNITANY pUnit = GetUnit(v_Players[v_CurrentTarget]->UnitId, UNIT_TYPE_PLAYER);
if(pUnit!=NULL)
return pUnit->pPath->xPos;
return 0;
}
WORD GetOldPlayerX() {
LPUNITANY pUnit = GetUnit(v_Players[v_CurrentTarget]->UnitId, UNIT_TYPE_PLAYER);
if(pUnit!=NULL)
return pUnit->pPath->xPos;
return 0;
}

Or if someone has a better approach to this problem, Im all ears.

Your best option would be to use classes with private variables, but if you insist on using functions, how about this:

#include<iostream>

bool isPrevious(int pos) {
    static int prev_pos = 0;
    bool is_same = (prev_pos == pos);
    prev_pos = pos;
    return is_same;
}

int main()
{
    std::cout << "1 is same as previous? : " << isPrevious(1) << '\n';
    std::cout << "1 is same as previous? : " << isPrevious(1) << '\n';

    std::cout << "2 is same as previous? : " << isPrevious(2) << '\n';
    std::cout << "2 is same as previous? : " << isPrevious(2) << '\n';
    std::cout << "2 is same as previous? : " << isPrevious(2) << '\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.