[C++] Simple help on decreasing int.

Experienced Elementalist
Joined
May 30, 2009
Messages
298
Reaction score
8
I can't really think of how to do this, I know it is plain simple.
Here is my little code:

Code:
if(startMode == true)
		{
            Sleep (500);
            float *myPos = GetPos(pChar());
            D3DXVECTORS[0] = myPos[0];
            D3DXVECTORS[1] = myPos[1];
            D3DXVECTORS[2] = myPos[2];
	   ZPostRequestSpawnWorldItem(NewCHAR, **** D3DXVECTOR);
           Sleep (4500);
	   SetHP(??);

Sorry about the spacing, using Dev-C++ on this project.

Anyways, I am trying to get SetHP(); (with an int value) to decrease, until it reaches 0.

I could use:

Code:
SetHP(80);
Sleep(3500);
SetHP(60);

But I won't do that, because of it won't detect if my char picked up a health, and I will do some extra work I don't feel like doing. :tongue:
 
Couldn't you simply create a global value in your DLL, and decrease it every time before it's called? Or is that not what you are trying to accomplish?
 
I have tried that, but the result was my character being killed instantly.
Maybe because I passed a NULL through the int value in SetHP()?
 
Thanks, I will try that.

Edit: I don't think I can use the post/pre decrements for this function.
Code:
//Set HP
void SetHP(int hp)
{
    ZChar = pChar();
    ARG = hp;
    __asm
    ( "MOVL _ZChar,%ECX\n\t"
      "MOVL _SetHPAdd,%EAX\n\t"
      "PUSH _ARG\n\t"
      "CALL *%EAX\n\t"
     );
}
 
It should work fine, using the SetHP(GetHP() - decrement)

I assume you also have Gunz's GetHP function if so, in your loop where you are decrementing, you should just call the SetHP(GetHP() - decrement)

It does not matter if the function uses inline assembler at all, because how it would look when compiled is like this:

Code:
CALL GetHP
MOV [EBP-8], EAX ;the -8 makes it a local variable by most C compilers, with EAX returning
SUB EAX, 10h ;This would be whatever you decrement by
PUSH EAX
LEA ECX, ZChar
MOV EAX, SetHPAddr
CALL EAX

It should still function regardless.
 
As a side note, when private data is encapsulated so that you're supposed to use selectors/mutators to work with them, it's usually wise to let those functions control how the data actually is stored. In this case, if you don't know (and you shouldn't - the whole point of encapsulation) how the functions work on the data, it's not safe to keep your own HP counter elsewhere, because you can never be sure its value matches what's kept private. I don't know the project you're working on, so if the HP value is only meant to be written to, obviously what I said doesn't apply here.
 
Back