GetObjectPos returns 0
#1

This is quite a weird problem, can't find anything wrong in the script, yet I can't fix it either. I've scripted something to place the player to the closest chair to him, but the object's coordinates are not returned.
A part of the command:
pawn Код:
new cid = GetNearest_Chair(playerid, 5.0);
            printf("%d %d", cid, tamHQ[23]);
            if(IsChair(cid))
            {
                if(sitting[playerid] == false)
                {
                    new Float:x, Float:y, Float:z;
                    new Float:rx, Float:ry, Float:rz;
                    GetObjectPos(cid, x, y, z);
                    printf("%f %f %f", x, y, z);
                    GetObjectRot(cid, rx, ry, rz);
                    SetPlayerPos(playerid, x, y, z);
                    SetPlayerFacingAngle(playerid, ry+270);
                    TogglePlayerControllable(playerid, 0);
                }
                else return SendError(playerid, "You're already sitting on a chair!");
            }
Prints:
Код:
28 28
0.000000 0.000000 0.000000
Functions:
pawn Код:
stock IsChair(objectid)
{
    if(objectid == tamHQ[23]) return 1;
    else return 0;
}

stock GetNearest_Chair(playerid, Float:distance)
{
    new
        Float:xX,
        Float:yY,
        Float:zZ,
        retElement = -1
    ;
    for(new i = 0; i < MAX_OBJECTS; i++)
    {
        if(IsValidObject(i) && IsChair(i)) continue;
        GetObjectPos(i, xX, yY, zZ);
        new Float:odist = GetPlayerDistanceFromPoint(playerid, xX, yY, zZ);
        if (retElement == -1 && IsChair(i))
        {
            retElement = i;
            distance = odist;
        }
        else if (odist < distance && IsChair(i))
        {
            retElement = i;
            distance = odist;
        }
    }
    return retElement;
}
So the object ID is returned correctly, but GetObjectPos is not returning its coords. What would be the problem?
Reply
#2

There's something I can't understand : why this line ?

pawn Код:
if(IsValidObject(i) && IsChair(i)) continue;
From GetNearestChair.

Actually, if the object is valid (so if it exists) and if it's a chair, you shouldn't skip his ID.
Reply
#3

Quote:
Originally Posted by S4t3K
Посмотреть сообщение
There's something I can't understand : why this line ?

pawn Код:
if(IsValidObject(i) && IsChair(i)) continue;
From GetNearestChair.

Actually, if the object is valid (so if it exists) and if it's a chair, you shouldn't skip his ID.
EDIT:
Changed it to
pawn Код:
if(!IsValidObject(i) && IsChair(i)) continue;
Now it returns:
Код:
-1 28
So the GetNearest_Chair is the problem.
Reply
#4

Actually, I think that "chairs" are objects. Correct me if I'm wrong.

Thus (still on the same line as above) if the object isn't valid, it won't go forward and will do what you put in your condition (so here, the loop will go to the next step). But if the object isn't valid, it will check that the object is a chair, and if it is, this will continue indefinately.

So your loop executes as this (in my opinion)

PHP код:

for(new 0MAX_OBJECTSi++) { } 
Reply
#5

Deleted the IsValidObject check, still returns "-1" as the object ID.
Reply
#6

May I show the new code ?
Reply
#7

Assuming it's per-player objects, use GetPlayerObjectPos instead.
Reply
#8

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Assuming it's per-player objects, use GetPlayerObjectPos instead.
I can't believe it, the fix was so obvious...I used CreateDynamicObject, totally forgot. I'll use the function from streamer. Thank you all.

EDIT: Same problem. Code:
pawn Код:
if(!IsChair(i)) continue;
GetDynamicObjectPos(i, xX, yY, zZ);
Prints:
Код:
28 28
0.000000 0.000000 0.000000
Reply
#9

pawn Код:
//
            new cid = GetNearest_Chair(playerid, 5.0);
            printf("%d %d", cid, tamHQ[23]);
            if(IsChair(cid))
            {
                if(sitting[playerid]) return SendError(playerid, "You're already sitting on a chair!");
                new Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz;
                GetDynamicObjectPos(cid, x, y, z);
                printf("%f %f %f", x, y, z);
                GetDynamicObjectRot(cid, rx, ry, rz);
                SetPlayerPos(playerid, x, y, z);
                SetPlayerFacingAngle(playerid, ry + 270); //Shouldn't it be 'rz + 270'? :l
                TogglePlayerControllable(playerid, 0);
            }

//
GetNearest_Chair(playerid, Float:distance)
{
    new Float:xX, Float:yY, Float:zZ, Float:odist, retElement = -1;
    for(new i = 0; i < MAX_OBJECTS; i++)
    {
        if(!IsChair(i)) continue;
        GetDynamicObjectPos(i, xX, yY, zZ);
        odist = GetPlayerDistanceFromPoint(playerid, xX, yY, zZ);
        if(retElement == -1 || odist < distance)
        {
            retElement = i;
            distance = odist;
        }
    }
    return retElement;
}
Reply
#10

@Threshold: Thanks, works now.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)