[Tutorial] How to use SetPlayerAttachedObject [Beginners]
#1

Using SetPlayerAttachedObject
  • In this Tutorial, I will be showing you guys how to use SetPlayerAttachedObject, using this code is easy, but as it shows in the wiki, you might get confused, so this is the right way to use it.
Step 1 - Defining
  • As you all know, in the previous SA-MP versions, we had SetPlayerHoldingObject, Now it was removed from SAMP version 0.3c, as stated in the wiki.
Quote:

Warning: This function was removed in SA-MP 0.3c

  • The current way of using SetPlayerAttachedObject is this way:
PHP код:
SetPlayerAttachedObject(playeridindexmodelidboneFloat:fOffsetXFloat:fOffsetYFloat:fOffsetZFloat:fRotXFloat:fRotYFloat:fRotZFloat:fScaleXFloat:fScaleYFloat:fScaleZ); 
  • In order to use it like SetPlayerHoldingObject
PHP код:
SetPlayerHoldingObject(playeridmodelbone); 
  • You will need the following defines, posted on the top of your script, under the includes.
PHP код:
#define SetPlayerHoldingObject(%1,%2,%3,%4,%5,%6,%7,%8,%9) SetPlayerAttachedObject(%1,MAX_PLAYER_ATTACHED_OBJECTS-1,%2,%3,%4,%5,%6,%7,%8,%9)
#define StopPlayerHoldingObject(%1) RemovePlayerAttachedObject(%1,MAX_PLAYER_ATTACHED_OBJECTS-1)
#define IsPlayerHoldingObject(%1) IsPlayerAttachedObjectSlotUsed(%1,MAX_PLAYER_ATTACHED_OBJECTS-1) 
Step 2
  • After defining it, it'll be able to be used this way:
PHP код:
SetPlayerAttachedObject(playeridindexmodelbone); 
Step 3 - Usage
  • Now you want to use it in a CMD of yours, For example a Redcone, I'll be giving an example of using it with ZCMD, and STRCMP.
For ZCMD:

PHP код:
CMD:redcone(playeridparams[])
{
           
SetPlayerAttachedObject(playerid,312386); // Bone 6 means Right handed
           
SendClientMessage(playeridCOLOR_GREEN"You are holding a Redcone.");
           return 
1;

For STRCMP:

PHP код:
public OnPlayerCommandText(playeridcmdtext[])
{
   if(!
strcmp(cmdtext"/redcone"true))
    {
        
SetPlayerAttachedObject(playerid312386);
        
SendClientMessage(playeridCOLOR_GREEN"You are holding a Redcone");
        return 
1;
     }
     return 
0;

Step 4 - Destroying it
  • You can destroy the object, by simply using RemovePlayerAttachedObject, And you will need IsPlayerAttachedObjectSlotUsed.
PHP код:
RemovePlayerAttachedObject(playerid,index
  • Index here, refers to "slot", just like IsPlayerAttachedObjectSlotUsed.
PHP код:
IsPlayerAttachedObjectSlotUsed(playeridslot
  • For Example, when you die, the item shouldn't be attached to the player anymore, use it this way:
PHP код:
public OnPlayerDeath(playeridkilleridreason)
{
    if(
IsPlayerAttachedObjectSlotUsed(playeridslot)) RemovePlayerAttachedObject(playeridslot);
    return 
1;

Model, and Bone

Whats bone ?
  • bone is the part of the body you want to attach the object to.
Note: A Maximum of 5 objects may be attached per player.

If there's anything i missed, or you got any problems, please post.

Credits: The SA-MP Wiki.
Reply
#2

Nice tut , useful for newbies.
Reply
#3

Quote:
Originally Posted by Kitten
Посмотреть сообщение
Nice tut , useful for newbies.
Yep, this is why i added [Beginners] in the name .
Reply
#4

Usefull ^^
Thanx
Reply
#5

Quote:
Originally Posted by =WoR=Bruno
Посмотреть сообщение
Usefull ^^
Thanx
You're Welcome.
Reply
#6

Very nice, well done.
Reply
#7

Quote:
Originally Posted by Basicz
Посмотреть сообщение
SUGGESTION : Use a loop to remove all attached objects, so it's code will be cleaner

So newbies will not copy paste it's code multiple times.
pawn Код:
public OnPlayerDeath( playerid, killerid, reason )
{
                                         // Comment below was taken from a_players.inc, we will use 0-4 this time.
                                         // 5 = 0-4
    for ( new i; i < 5; i ++ )  // Creates the variable 'i' and checks if it's value is lower than 5, if so, increase the value of ' i '
    {
        if ( IsPlayerAttachedObjectSlotUsed( playerid, i ) ) // If the attached slot ( it depends on the value of the 'i' variable ) of the player is used.
            RemovePlayerAttachedObject( playerid, i ); // Remove the attached object the player have, the slot is the value of the 'i' variable.
    }

    return 1;
}
SUGGESTION2 : Add the bones list. So beginners wouldn't have confused what is the bones ID

Код:
-- This is from the SA-MP wikia.

0 - Not usable (will crash the client)

1 - Spine

2 - Head

3 - Left upper arm

4 - Right upper arm

5 - Left hand

6 - Right hand

7 - Left thigh

8 - Right thigh

9 - Left foot

10 - Right foot

11 - Right calf

12 - Left calf

13 - Left forearm

14 - Right forearm

15 - Left clavicle

16 - Right clavicle

17 - Neck

18 - Jaw
Nice tutorial though, good for beginners.
Quote:

Model, and Bone

....
Reply
#8

Not very well explained, sorry :/ (I'm not acting bitchy)

You could explain what each of these do:
pawn Код:
SetPlayerAttachedObject(playerid, index, modelid, bone, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:fRotX, Float:fRotY, Float:fRotZ, Float:fScaleX, Float:fScaleY, Float:fScaleZ);
What does this do, and what is the index for?:
pawn Код:
RemovePlayerAttachedObject(playerid,index)
So what is the slot for?
pawn Код:
IsPlayerAttachedObjectSlotUsed(playerid, slot)
Reply
#9

Thanks guys, and

Quote:
Originally Posted by Basicz
Посмотреть сообщение
SUGGESTION : Use a loop to remove all attached objects, so it's code will be cleaner

So newbies will not copy paste it's code multiple times.
pawn Код:
public OnPlayerDeath( playerid, killerid, reason )
{
                                         // Comment below was taken from a_players.inc, we will use 0-4 this time.
                                         // 5 = 0-4
    for ( new i; i < 5; i ++ )  // Creates the variable 'i' and checks if it's value is lower than 5, if so, increase the value of ' i '
    {
        if ( IsPlayerAttachedObjectSlotUsed( playerid, i ) ) // If the attached slot ( it depends on the value of the 'i' variable ) of the player is used.
            RemovePlayerAttachedObject( playerid, i ); // Remove the attached object the player have, the slot is the value of the 'i' variable.
    }

    return 1;
}
SUGGESTION2 : Add the bones list. So beginners wouldn't have confused what is the bones ID

Код:
-- This is from the SA-MP wikia.

0 - Not usable (will crash the client)

1 - Spine

2 - Head

3 - Left upper arm

4 - Right upper arm

5 - Left hand

6 - Right hand

7 - Left thigh

8 - Right thigh

9 - Left foot

10 - Right foot

11 - Right calf

12 - Left calf

13 - Left forearm

14 - Right forearm

15 - Left clavicle

16 - Right clavicle

17 - Neck

18 - Jaw
Nice tutorial though, good for beginners.
I already put the model and bones with the link.

Quote:
Originally Posted by FireCat
Посмотреть сообщение
Not very well explained, sorry :/ (I'm not acting bitchy)

You could explain what each of these do:
pawn Код:
SetPlayerAttachedObject(playerid, index, modelid, bone, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:fRotX, Float:fRotY, Float:fRotZ, Float:fScaleX, Float:fScaleY, Float:fScaleZ);
What does this do, and what is the index for?:
pawn Код:
RemovePlayerAttachedObject(playerid,index)
So what is the slot for?
pawn Код:
IsPlayerAttachedObjectSlotUsed(playerid, slot)
Its obvious, since you can use the one like SetPlayerHoldingObject, you won't need me to explain what all of this does, For RemovePlayerAttachedObject, i didn't use index, i believe i said slot, and i already noted that you may only use 5 objects, which means 1-5 slots, you didn't read.
Reply
#10

Nice.
Reply
#11

Quote:
Originally Posted by Jafet_Macario
Посмотреть сообщение
Nice.
Thanks
Reply
#12

Quote:
Originally Posted by Cimbe94
Посмотреть сообщение
Nice tutorial, usefull for beginners.
Yep, Thanks.
Reply
#13

Your strcmp command: strcmp returns 0 if the strings are the same, other than that you forgot to close the parenthesis for the if condition.
Reply
#14

Quote:
Originally Posted by [NoV]LaZ
Посмотреть сообщение
Your strcmp command: strcmp returns 0 if the strings are the same, other than that you forgot to close the parenthesis for the if condition.
Yep, thanks for that, edited it, added the bracket, plus a return 0 in the end after the cmd.
Reply
#15

Nice tutorial man, 10/10
Reply
#16

Quote:
Originally Posted by PhoenixB
Посмотреть сообщение
Nice tutorial man, 10/10
Thanks man
Reply
#17

Mantabbbbb
Reply
#18

Quote:
Originally Posted by Gogon
Посмотреть сообщение
Mantabbbbb
Mantab ?, I don't understand Korean, please use English
Reply
#19

Quote:
Originally Posted by Gogon
Посмотреть сообщение
Mantabbbbb
Quote:
Originally Posted by G4M3Ov3r
Посмотреть сообщение
Mantab ?, I don't understand Korean, please use English
Mantab = awesome.
Reply
#20

Quote:
Originally Posted by varthshenon
Посмотреть сообщение
Mantab = awesome.
Oh, thanks then, lol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)