SA-MP Forums Archive
Checking if object coordinates equal to certain coordinates - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Checking if object coordinates equal to certain coordinates (/showthread.php?tid=479423)



Checking if object coordinates equal to certain coordinates - Jonesy96 - 05.12.2013

Hi all,

I need to check if the objects coordinates are equal to a certain set of coordinates. However, it's not working for some reason. Code and error are below:

Code:
if(GetObjectPos(leftSdDoor, x, y, z) == 244.88681, 72.58778, 1002.63953){
	        //Doors closed
	        MoveObject(leftSdDoor, 243.9668, 72.5878, 1002.6395, 2.00);
	    	MoveObject(rightSdDoor, 248.8093, 72.5921, 1002.6395, 2.00);
		}else{
		    //Doors Open
		    MoveObject(leftSdDoor, 244.8868, 72.5878, 1002.6395, 2.00);
	    	MoveObject(rightSdDoor, 247.8693, 72.5921, 1002.6395, 2.00);
		}
Sorry if the indentations have gone messed up

Error (Warning): C:\Users\Rhys\Desktop\scripting\gamemodes\sheriffs .pwn(374) : warning 206: redundant test: constant expression is non-zero

Thanks in advance if you can help!


Respuesta: Checking if object coordinates equal to certain coordinates - [CG]Milito - 05.12.2013

Try like this

pawn Code:
new Float:X, Float:Y, Float:Z;
    GetObjectPos(leftSdDoor, X, Y, Z);
    if(X == 244.88681 && Y ==  72.58778 && Z == 1002.63953)
    {
        //Doors closed
        MoveObject(leftSdDoor, 243.9668, 72.5878, 1002.6395, 2.00);
        MoveObject(rightSdDoor, 248.8093, 72.5921, 1002.6395, 2.00);
    }
    else
    {
    //Doors Open
    MoveObject(leftSdDoor, 244.8868, 72.5878, 1002.6395, 2.00);
    MoveObject(rightSdDoor, 247.8693, 72.5921, 1002.6395, 2.00);
    }



Re: Checking if object coordinates equal to certain coordinates - Threshold - 05.12.2013

You need the exact float value, so the above will only work once.
pawn Code:
new Float:X, Float:Y, Float:Z;
    GetObjectPos(leftSdDoor, X, Y, Z);
    if(X == 244.8868 && Y ==  72.5878 && Z == 1002.6395)
    {
        //Doors closed
        MoveObject(leftSdDoor, 243.9668, 72.5878, 1002.6395, 2.00);
        MoveObject(rightSdDoor, 248.8093, 72.5921, 1002.6395, 2.00);
    }
    else
    {
        //Doors Open
        MoveObject(leftSdDoor, 244.8868, 72.5878, 1002.6395, 2.00);
        MoveObject(rightSdDoor, 247.8693, 72.5921, 1002.6395, 2.00);
    }
EDIT: I would recommend using a new variable that determines whether the doors are open or not, rather than getting the position of the object and checking it against the values given.


Re: Checking if object coordinates equal to certain coordinates - Jonesy96 - 05.12.2013

Thanks for the suggestions. I'll try them both out tonight when i get home from work and let you know how I got on


Re: Checking if object coordinates equal to certain coordinates - Vince - 05.12.2013

Actually, due to the way floating point values work and how they are stored, an exact comparison usually isn't recommended. If one of the values retrieved from GetObjectPos is off by 0.0001 the code will cease to work. You should check whether the object is in the approximate area.


Re: Checking if object coordinates equal to certain coordinates - Pottus - 05.12.2013

Quote:
Originally Posted by Vince
View Post
Actually, due to the way floating point values work and how they are stored, an exact comparison usually isn't recommended. If one of the values retrieved from GetObjectPos is off by 0.0001 the code will cease to work. You should check whether the object is in the approximate area.
It's true I would also add as a rule of thumb when working with floats use Less than or equal to <= and/or >= greater than or equal to when making comparisons.


Re: Checking if object coordinates equal to certain coordinates - Jonesy96 - 05.12.2013

I think I'll just go with the more reliable suggestion and set a boolean variable to true on door open and to false on door close. If that doesnt work out for me, I'll look at other options. Thanks guys


Re: Checking if object coordinates equal to certain coordinates - Jonesy96 - 05.12.2013

Set a boolean variable and it worked