Return value of c_trace?

Posted By: Loremaster

Return value of c_trace? - 02/05/13 01:33

I use the following line of code to evaluate whether there is a line of sight between player and NPC.

Code:
if (c_trace(
	my.x,
	player.x,
IGNORE_ME|IGNORE_PASSENTS|IGNORE_PASSABLE|IGNORE_SPRITES) == 0)
        {
	FancyNPCFunction()
	}



I assumed if the trace hits the player unhindered, the return value would be 0. My if-statement therefore triggers the NPC-action if it is 0.

It does not trigger it. What do I do wrong?
Posted By: WretchedSid

Re: Return value of c_trace? - 02/05/13 02:30

c_trace returns 0 when nothing is hit, otherwise the return value is non-zero. Note though that hit could mean that anything was hit, not just the player, so you should check wether you is equal to the player.
Posted By: Loremaster

Re: Return value of c_trace? - 02/05/13 02:43

Got it. There is a me, there is a you. The latter was unknown to me. laugh
Posted By: rayp

Re: Return value of c_trace? - 02/05/13 06:15

BTW.: To avoid "empty pointer" errors please check YOU before using it fex.:
Code:
c_trace
 if (you) if (you == player)

Posted By: WretchedSid

Re: Return value of c_trace? - 02/05/13 06:36

That's not needed Rayp, you can compare a nullpointer to any other pointer just fine, you just can't dereference it.
Posted By: rayp

Re: Return value of c_trace? - 02/05/13 06:42

Ah ok.
so this is ok:
Code:
if (you == player)



and the check is only needed here
Code:
if (you) you.health -= 10;

Posted By: WretchedSid

Re: Return value of c_trace? - 02/05/13 06:56

If you can guarantee that player is always unequal to NULL, you can omit the second check and just access you directly (given that both if() are related obviously). This would also work if you don't know wether player is NULL or not:

Code:
if(you && you == player)
{
   you->health -= 10;
}

Posted By: rayp

Re: Return value of c_trace? - 02/05/13 07:07

Code:
if (you) if (you == player) you.health-=10;

This is how i use to make it (almost ever). Guess its the most secured way.
Posted By: WretchedSid

Re: Return value of c_trace? - 02/05/13 07:09

It's not though. Like I said, if you know that player is always unequal to NULL, you can simply remove the check wether you is NULL or not, because if it is equal to player, it's definitely not NULL. If you don't know if player is NULL or not, you can put the check wether you is NULL or not together into the same if and make your code cleaner and easier to read
Posted By: rayp

Re: Return value of c_trace? - 02/05/13 07:24

I just wanted to say if hes not sure if you != 0 he should check it before.
But i got your points, thank you.
© 2024 lite-C Forums