Gamestudio Links
Zorro Links
Newest Posts
WFO Training with parallel cores Zorro64
by Martin_HH. 02/23/26 10:49
ZorroGPT
by TipmyPip. 02/21/26 19:15
Camera always moves upwards?
by clonman. 02/21/26 09:29
Zorro version 3.0 prerelease!
by TipmyPip. 02/20/26 13:22
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 02/19/26 13:22
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
3 registered members (TipmyPip, alx, Martin_HH), 6,129 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
alx, ApprenticeInMuc, PatrickH90, USER0328, Sfrdragon
19199 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
if(player) versus while(player == 0) #294136
10/16/09 14:52
10/16/09 14:52
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Pappenheimer Offline OP
Senior Expert
Pappenheimer  Offline OP
Senior Expert

Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Can anybody tell me, why the first thing works, but the latter not?
The first is waiting while the pointer 'player' is empty.
The second and third version tests whether 'player' is valid.
What's wrong with that?
Code:
while(player == 0){wait(1);}
	while(1)
	{
//		if(player == 1)
//		{
			if(vec_dist(player.x, my.x) < 200)
			{
				sound();
			}	
//		}
		wait(1);
	}



Code:
//while(player == 0){wait(1);}
	while(1)
	{
		if(player == 1)
		{
			if(vec_dist(player.x, my.x) < 200)
			{
				sound();
			}	
		}
		wait(1);
	}



Code:
//while(player == 0){wait(1);}
	while(1)
	{
		if(player)
		{
			if(vec_dist(player.x, my.x) < 200)
			{
				sound();
			}	
		}
		wait(1);
	}



Re: if(player) versus while(player == 0) [Re: Pappenheimer] #294146
10/16/09 15:54
10/16/09 15:54
Joined: Jan 2007
Posts: 1,619
Germany
Scorpion Offline
Serious User
Scorpion  Offline
Serious User

Joined: Jan 2007
Posts: 1,619
Germany
player is a pointer and a pointer is invalid (=not set), if it's 0 (NULL).

When it is set, then it becomes something different to zero and pretty unlikely exactly 1. And there is the mistake. A condition (as in a while or if) is false exactly then, when it's zero, if the variable has any other value it's true.

you(player==1) will probably return false and so not execute the branch, because the variable is with a high probability not 1.

Hope you got the point
Scorpion

Re: if(player) versus while(player == 0) [Re: Scorpion] #294148
10/16/09 16:20
10/16/09 16:20
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Pappenheimer Offline OP
Senior Expert
Pappenheimer  Offline OP
Senior Expert

Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Thanks for your reply!
Originally Posted By: Scorpion

you(player==1) will probably return false and so not execute the branch, because the variable is with a high probability not 1.

This was the reason, why I tried the third version:

'if(player)' - isn't any value above zero true?

Re: if(player) versus while(player == 0) [Re: Pappenheimer] #294150
10/16/09 16:26
10/16/09 16:26

F
Fear411
Unregistered
Fear411
Unregistered
F



Try it like this:

Code:
while(1)
	{
		if(player != NULL)
		{
			if(vec_dist(player.x, my.x) < 200)
			{
				sound();
			}	
		}
		wait(1);
	}



Re: if(player) versus while(player == 0) [Re: ] #294152
10/16/09 16:46
10/16/09 16:46
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Pappenheimer Offline OP
Senior Expert
Pappenheimer  Offline OP
Senior Expert

Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Hm, it seems that all the trouble is because of my 'fuzzy' memory.

I would enjoy to have a more precisely one.
I tried your version, and my third again, and both worked - maybe a second mistake of mine interfered to make me think that it didn't work either.
Sorry to have bothered you! Thanks!


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1