Checking if object coordinates equal to certain coordinates
#1

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!
Reply
#2

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);
    }
Reply
#3

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.
Reply
#4

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
Reply
#5

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.
Reply
#6

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.
Reply
#7

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
Reply
#8

Set a boolean variable and it worked
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)