|
|
Simple comparision question
#424539
06/18/13 16:52
06/18/13 16:52
|
Malice
Unregistered
|
Malice
Unregistered
|
The manual says you should not do a Boolean check on a pointer and comp it's member(s) values in the same if(). Is this still true. Example below.
if ( pan_temp && pan_temp.pos_x != 1024 ) // The manual says don't do this.
if ( pan_temp ) // Manual say do this.
{
if ( pan_temp.pos_x != 1024 );
}
Also does this apply if you are doing the Boolean check on the contents of a VAR and not it's pointer.
var I = 999;
if ( I && I > 500 )
|
|
|
Re: Simple comparision question
[Re: rayp]
#424548
06/18/13 17:32
06/18/13 17:32
|
Malice
Unregistered
|
Malice
Unregistered
|
@3Run I am checking if the vars content is != 0. Content not pointer. I am trying to see if it has a value not if it was declared.
if ( I ) is same as if(I != 0)
But I might to totally wrong in my understand of what I did above. @rayp Thank you for this reply. That's is where my code is breaking.
Last edited by Malice; 06/18/13 17:34.
|
|
|
Re: Simple comparision question
[Re: rayp]
#424551
06/18/13 17:36
06/18/13 17:36
|
Malice
Unregistered
|
Malice
Unregistered
|
@rayp Yes that is what I'm asking. Does the same rule apply as pointers here
if( i && i != 500) // Bad ?
if(i) // Good?
{
if(i != 500)
}
I have to stop using i because of autocaps for i
Last edited by Malice; 06/18/13 17:38.
|
|
|
Re: Simple comparision question
[Re: ]
#424556
06/18/13 17:48
06/18/13 17:48
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
The manual says that this is "good" because the program checks the whole comparision before deciding if it is true or false.
ENTITY * ent;
void main()
{
if(ent && ent.x > 5)
ent.x = 0;
}
this will result in an error. ent doesn't exist so if you check if(ent) it is true. but in the comparison above the program doesn't abort it but checks the next comparison (ent.x > 5) but ent doesn't exist and this causes an error. edit: note that this only applies for pointers.
Last edited by Kartoffel; 06/18/13 17:51.
POTATO-MAN saves the day! - Random
|
|
|
Re: Simple comparision question
[Re: ]
#424557
06/18/13 17:49
06/18/13 17:49
|
Joined: May 2009
Posts: 5,377 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,377
Caucasus
|
Why don't you use something like this:
var checkVar(var CHECK){
if(CHECK != 0){ return(1); }
if(CHECK == 0){ return(0); }
}
Hope, it helps  Greets
|
|
|
Re: Simple comparision question
[Re: ]
#424559
06/18/13 17:56
06/18/13 17:56
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
Member
|
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
EDIT: sorry, only read your first post when i wrote it ^^ The manual says you should not do a Boolean check on a pointer and comp it's member(s) values in the same if(). Is this still true. Yes. Also does this apply if you are doing the Boolean check on the contents of a VAR and not it's pointer. It depends of what you want to do. variables in if's evaluate to "false" if they are 0 and to "true" if they are non-zero. Here are some examples:
i=10;
if(i) -> if(true) -> will execute
i = -20;
if(i) -> if(true) -> will execute
i = 0;
if(i) -> if(false) -> will not execute
so your second example will work only if I > 500 ("I &&" is useless, because if(i) means if i is not 0. But your second contion I>500 includes that i is not null). Why do you need to check it with poitners in an if? 3dgs evaluates all conditions in the if. If you have something like the following:
pan_temp = NULL;
if( pan_temp && pan_temp.pos_x != 1024 )
It will evaluate to the following:
if(NULL && NULL.pos_x != 1024 )
And because NULL has no property pos_x, the compiler will show an error.
Last edited by krial057; 06/18/13 17:56.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|