SA-MP Forums Archive
GetObjectPos returns 0 - 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: GetObjectPos returns 0 (/showthread.php?tid=524203)



GetObjectPos returns 0 - DiGiTaL_AnGeL - 05.07.2014

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?


Re : GetObjectPos returns 0 - S4t3K - 05.07.2014

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.


Re: Re : GetObjectPos returns 0 - DiGiTaL_AnGeL - 05.07.2014

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.


Re : GetObjectPos returns 0 - S4t3K - 05.07.2014

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++) { } 



Re: GetObjectPos returns 0 - DiGiTaL_AnGeL - 05.07.2014

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


Re : GetObjectPos returns 0 - S4t3K - 05.07.2014

May I show the new code ?


Re: GetObjectPos returns 0 - Konstantinos - 05.07.2014

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


Re: GetObjectPos returns 0 - DiGiTaL_AnGeL - 05.07.2014

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



Re: GetObjectPos returns 0 - Threshold - 06.07.2014

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;
}



Re: GetObjectPos returns 0 - DiGiTaL_AnGeL - 06.07.2014

@Threshold: Thanks, works now.