How to getobjectpos by model id?
#1

I want to make a public phone system and i want to check if player near object 1216(public phone stand),player can use /phone

GetObjectPos is has object id not model id. How do i get it?


Thanks.

ps. sorry for my bad english
Reply
#2

You need define an array of phone points or use dynamic areas.
Reply
#3

Quote:
Originally Posted by Pottus
View Post
You need define an array of phone points or use dynamic areas.
yeah first time i think that but if it's can check by getmodelobjectpos it's easier

but if no way to check i think i go in game and save any coord
Reply
#4

Quote:
Originally Posted by Y_Less
View Post
You can't. That's not a one-to-one relationship. If I create five telephone boxes then do:

pawn Code:
GetObjectModelPos(1216, x, y, z);
What should the return value be? There are five possible results, and no single way to determine which one to use.
sry i forget to think about that.

if in gamemode have worldobject(telephone) and it's on this coords.

world_object_telephone[1] = posx:123.1 posy:35.5 posz:22.1;
world_object_telephone[2] = posx:222.2 posy:10.0 posz: 15.5;
world_object_telephone[3] = posx:1422.2 posy:150.0 posz: 105.5;

can script make if player go near telephone, first check by objectid and get model from objectid
second if objectmodel(objectid) == telephone_model player can use this telephone
then if i have 3 telephone,coord of any telephone not duplicate because it's has different ObjectID.

that's my think(i'm lazy to save any phone coords in game.so sorry)

full my plan code
Code:
stock IsPlayerNearModelID(pid,modelid)
{
    new Float:x,Float:y,Float:z;
    for(new i=0;i<MAX_OBJECTS;i++)
    {
        if(GetObjectModel(i) == modelid)
        {
            GetObjectPos(i,x,y,z);
            if(PlayerToPoint(5.0,pid,x,y,z)
            {
                 //do something
                 break;
             }
        }
    } 
}
but it's no way to do this i cannot leave to save coords in game OMG!!!


ps.sorry for my bad english and my bad script
Reply
#5

Quote:
Originally Posted by Y_Less
View Post
Yeah, you aren't going to get very far programming if you are lazy about it.
yep i think so. it's time to fighting

Y_Less i have one question can i view my server memory/cpu resource usage for samp-server only not full pc resource usage? i worried about the time to calculate my some function(eg.mutilevel loop)
Reply
#6

Looping through your objects and finding out if the player is near (within a few meters) of each object is the only way to detect if the player is near any of your phonebooths.
Just like the code you already presented.

As for calculating the time a specific function takes to execute, you could add 2 variables at the start of your function, get the current time in milliseconds and afterwards calculate the difference.
pawn Code:
new StartTime, StopTime, Msg[128];

// Get the current time in milliseconds
StartTime = GetTickCount();

// Your code here to be tested

// Get the current time in milliseconds
StopTime = GetTickCount();

// Inform the admin by printing to the server console
printf("Your function takes %i milliseconds to execute", StopTime - StartTime);
// OR
// Inform the player in-game by sending a client message
format(Msg, sizeof(Msg), "Your function takes %i milliseconds to execute", StopTime - StartTime);
SendClientMessage(playerid, 0xFFFFFFFF, Msg);
Reply
#7

Well lets see what responses we have so far:
Quote:
Originally Posted by Pottus
You need define an array of phone points or use dynamic areas.
Oh wait... that's all we really needed.

-----

So lets take a deeper insight to things. If you are using these coords:
Quote:

world_object_telephone[1] = posx:123.1 posy:35.5 posz:22.1;
world_object_telephone[2] = posx:222.2 posy:10.0 posz: 15.5;
world_object_telephone[3] = posx:1422.2 posy:150.0 posz: 105.5;

That would translate into floats like this:
pawn Code:
123.1, 35.5, 22.1
222.2, 10.0, 15.5
1422.2, 150.0, 105.5
Now if we add those to an array called 'PhoneCoords', we'll be able to organise things a bit more. So lets set a maximum of '3' phone booths for the time being.
pawn Code:
new Float:PhoneCoords[3][3]
We're creating an array called 'PhoneCoords' that stores values as floats, hence the 'Float:' tag. We will have 3 phone booths, with 3 coordinates. The 'x', 'y' and 'z' coordinates. Hence the two '[3]' sizes.

Now we can add our coordinates.
pawn Code:
new Float:PhoneCoords[3][3] = {
123.1, 35.5, 22.1
222.2, 10.0, 15.5
1422.2, 150.0, 105.5
};
But this isn't right, the only thing separating the coordinates from each other is a new line, the compiler won't understand that...

pawn Code:
new Float:PhoneCoords[3][3] = {
{123.1, 35.5, 22.1},
{222.2, 10.0, 15.5},
{1422.2, 150.0, 105.5}
};
Ahh, that looks better. But I won't go explaining why it looks the way it does, because that's very basic programming and you should be able to study that for yourself.

To see whether a player is in range of a phone booth, you can use this code in any way you want, but I'm going to put it in the form of a custom function.
pawn Code:
IsPlayerInRangeOfPhone(playerid, Float:range = 5.0)
{
    for(new i = 0; i < sizeof(PhoneCoords); i++)
    {
        if(!IsPlayerInRangeOfPoint(playerid, range, PhoneCoords[i][0], PhoneCoords[i][1], PhoneCoords[i][2])) continue;
        return 1;
    }
    return 0;
}
And that's all it takes. To use that function somewhere, all you need to do is:
pawn Code:
if(IsPlayerInRangeOfPhone(playerid)) //Player is near a phone booth. (Within 5.0 units)
else //Player is NOT near a phone booth
You can change the range by adding a second parameter, but the default value of the range is '5.0', as shown in the function by this value:
pawn Code:
Float:range = 5.0
You can change this range to however you want. Lets say you want to look for phone booths within 15.0 units of the player, so you would do:
pawn Code:
if(IsPlayerInRangeOfPhone(playerid, 15.0)) //Player is in range of a phone booth (Within 15.0 units)
else //Player is NOT within range of a phone booth
-----

This is just one method of doing what you want to do, or as Pottus suggested you can create your own dynamic phone booth system... but something tells me that you might not be able to do this just yet at your skill level, so you might need to study up a bit more.
Reply
#8

Quote:
Originally Posted by PowerPC603
View Post
Looping through your objects and finding out if the player is near (within a few meters) of each object is the only way to detect if the player is near any of your phonebooths.
Just like the code you already presented.

As for calculating the time a specific function takes to execute, you could add 2 variables at the start of your function, get the current time in milliseconds and afterwards calculate the difference.
pawn Code:
new StartTime, StopTime, Msg[128];

// Get the current time in milliseconds
StartTime = GetTickCount();

// Your code here to be tested

// Get the current time in milliseconds
StopTime = GetTickCount();

// Inform the admin by printing to the server console
printf("Your function takes %i milliseconds to execute", StopTime - StartTime);
// OR
// Inform the player in-game by sending a client message
format(Msg, sizeof(Msg), "Your function takes %i milliseconds to execute", StopTime - StartTime);
SendClientMessage(playerid, 0xFFFFFFFF, Msg);
Thanks for time to execute function i will try.


Quote:
Originally Posted by Threshold
View Post
Well lets see what responses we have so far:


Oh wait... that's all we really needed.

-----

So lets take a deeper insight to things. If you are using these coords:

That would translate into floats like this:
pawn Code:
123.1, 35.5, 22.1
222.2, 10.0, 15.5
1422.2, 150.0, 105.5
Now if we add those to an array called 'PhoneCoords', we'll be able to organise things a bit more. So lets set a maximum of '3' phone booths for the time being.
pawn Code:
new Float:PhoneCoords[3][3]
We're creating an array called 'PhoneCoords' that stores values as floats, hence the 'Float:' tag. We will have 3 phone booths, with 3 coordinates. The 'x', 'y' and 'z' coordinates. Hence the two '[3]' sizes.

Now we can add our coordinates.
pawn Code:
new Float:PhoneCoords[3][3] = {
123.1, 35.5, 22.1
222.2, 10.0, 15.5
1422.2, 150.0, 105.5
};
But this isn't right, the only thing separating the coordinates from each other is a new line, the compiler won't understand that...

pawn Code:
new Float:PhoneCoords[3][3] = {
{123.1, 35.5, 22.1},
{222.2, 10.0, 15.5},
{1422.2, 150.0, 105.5}
};
Ahh, that looks better. But I won't go explaining why it looks the way it does, because that's very basic programming and you should be able to study that for yourself.

To see whether a player is in range of a phone booth, you can use this code in any way you want, but I'm going to put it in the form of a custom function.
pawn Code:
IsPlayerInRangeOfPhone(playerid, Float:range = 5.0)
{
    for(new i = 0; i < sizeof(PhoneCoords); i++)
    {
        if(!IsPlayerInRangeOfPoint(playerid, range, PhoneCoords[i][0], PhoneCoords[i][1], PhoneCoords[i][2])) continue;
        return 1;
    }
    return 0;
}
And that's all it takes. To use that function somewhere, all you need to do is:
pawn Code:
if(IsPlayerInRangeOfPhone(playerid)) //Player is near a phone booth. (Within 5.0 units)
else //Player is NOT near a phone booth
You can change the range by adding a second parameter, but the default value of the range is '5.0', as shown in the function by this value:
pawn Code:
Float:range = 5.0
You can change this range to however you want. Lets say you want to look for phone booths within 15.0 units of the player, so you would do:
pawn Code:
if(IsPlayerInRangeOfPhone(playerid, 15.0)) //Player is in range of a phone booth (Within 15.0 units)
else //Player is NOT within range of a phone booth
-----

This is just one method of doing what you want to do, or as Pottus suggested you can create your own dynamic phone booth system... but something tells me that you might not be able to do this just yet at your skill level, so you might need to study up a bit more.
Thanks i think so but anyone have fastest way to get all public phone coords.i don't know how many it's spawn on map
Reply
#9

Oh... you mean you're not 'creating' the phone objects? You're using ones that are already on the map...?

Wow, you ARE lazy.
Reply
#10

Quote:
Originally Posted by Threshold
View Post
Oh... you mean you're not 'creating' the phone objects? You're using ones that are already on the map...?

Wow, you ARE lazy.
yes i'm not create any object

i just think if it's possible way to do from my function.

In real thing i go in game and save areas of public phone object it's so more place and i think i missed some place.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)