Attaching player to an entity functions

Started by NicusorN5, Sep 07, 2018, 12:37 PM

Previous topic - Next topic

NicusorN5

Attaching player to an entity functions

   I've tried to make a system that attaches a player to an entity (car, object, pickup or other player). Good for some events and stuff, for example if you try making an arrest system like in Brasil Real RPG, when the player 'follows' the cop, you can find this usefull. You can adapt the script to attach an object to an car , an pickup to a car, etc...

Functions:
[spoiler]
player.Attach( type, entity, offset);

Attaches a player to the specified entity with an offset.
Parameters:
type : string, to define what kind of entity is the player attached on.
entity: can be a player, object, vehicle or pickup instance.
offset: Vector, defines the offset the player is attached to the center of vehicle.

Example: FindPlayer(0).Attach("CObject",FindObject(10),Vector(0,10,4.5))
player.Deattach();
De-ataches the player from the entity he was attached before.
Parameters: none.

Example: FindPlayer(0).DeAttach()[/spoiler]

Video [BAD QUALITY]:
https://www.youtube.com/watch?v=HeGTsvJV_ZE


Download:
[spoiler]http://www.mediafire.com/file/hd5fjba5ikxd1ad/Athanatos_VCMP_Ataching_system.rar/file[/spoiler]

Installing:
Just put the 'attach_sys.nut' file from the downloaded archive in your /scripts/ folder then :
function onScriptLoad()
{
dofile("scripts/attach_sys.nut");
}

!

#1
It can be improved by using squirrel table
Usage:
object & entity can be any of these types player/pckup/vehicle/object
attachEntity(instance object,instance entity,vector offset);
deattachEntity(instance object);

Snippet
g_Attach <- {}

function attachEntity(object,entity,offset)
{
if( g_Attach.rawin( object.ID ) ) print( "Object is already attached to an entity" );
g_Attach.rawset( object.ID, {
"Object": object,
"Entity": entity
"Offset": offset,
} );
}
function deattachEntity( object )
{
if( !g_Attach.rawin( object.ID ) ) print( "Object is not attached to any entity" );
g_Attach.rawdelete( object.ID );
}
function AttachUpdate()
{
foreach( id, attach in g_Attach )
{
if ( attach["Object"].Pos.x == 0 )
{
g_Attach.rawdelete( id );
continue;
}

if ( attach["Entity"].Pos.x == 0 )
{
g_Attach.rawdelete( id );
continue;
}

attach["Object"].Pos = attach["Entity"].Pos + attach["Offset"];
}
}

ATTACH_TIMER <- NewTimer("AttachUpdate",100,0);

Usage Example
/attach, /deattach
function onPlayerCommand(player, command, arguments)
{
switch( command )
{
case "attach":
if ( !arguments ) return MessagePlayer( "[#2add9b]Usage: /attach <player/pickup/vehicle/object> <id>", player );

local args = split( arguments, " " );
if ( args.len() < 7 ) return MessagePlayer( "[#2add9b]Usage: /attach <objType> <objid> <entityType> <entityID> <offsetX> <offsetY> <offsetZ> (Type: player/pickup/vehicle/object)", player );

local
objTp = args[0],
objID = args[1].tointeger(),
entTp = args[2],
entID = args[3].tointeger(),
offSetX = args[4],
offSetY = args[5],
offSetZ = args[6],
objIns,
entIns;
switch( objTp )
{
case "player": objIns = FindPlayer( objID ); break;
case "pickup": objIns = FindPickup( objID ); break;
case "vehicle": objIns = FindVehicle( objID ); break;
case "object": objIns = FindObject( objID ); break;
}
switch( entTp )
{
case "player": entIns = FindPlayer( entID ); break;
case "pickup": entIns = FindPickup( entID ); break;
case "vehicle": entIns = FindVehicle( entID ); break;
case "object": entIns = FindObject( entID ); break;
}

if ( !objIns || !entIns ) return MessagePlayer( "[#2add9b]One of the instance is not found.", player );
attachEntity( objIns, entIns, Vector( offSetX.tointeger(), offSetY.tointeger(), offSetZ.tointeger() ) );
MessagePlayer( "Attached object to entity.", player );
break;
case "deattach":
if ( !arguments ) return MessagePlayer( "[#2add9b]Usage: /detach <objType> <objid> (Type: player/pickup/vehicle/object)", player );

local args = split( arguments, " " );
if ( args.len() < 2 ) return MessagePlayer( "[#2add9b]Usage: /detach <objType> <objid> (Type: player/pickup/vehicle/object)", player );

local
objTp = args[0],
objID = args[1].tointeger(),
objIns;
switch( objTp )
{
case "player": objIns = FindPlayer( objID ); break;
case "pickup": objIns = FindPickup( objID ); break;
case "vehicle": objIns = FindVehicle( objID ); break;
case "object": objIns = FindObject( objID ); break;
}

if ( !objIns ) return MessagePlayer( "[#2add9b]The instance is not found.", player );
deattachEntity( objIns );
MessagePlayer( "Deattached object from entity.", player );
break;
}
}
:edit:
Forget to add 'return' before 'MessagePlayer'

Discord: zeus#5155

MEGAMIND

A suggestion, make it using if & else statement for other player to make it understandable instead if switch case

Xmair

People should learn using switch case then.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

NicusorN5


MEGAMIND


D4rkR420R

This is something I had in mind. ^-^ Nice one, pleb. You made my day.

=RK=MarineForce

how can i add ? when player type
 if ( cmd == "flagattach" )
{
player attack PickedID = 6000 how can i do this like simple Player.Attach.PickupID = 6005;
}

attach player ID

MEGAMIND

Quote from: =RK=MarineForce on Sep 26, 2018, 12:31 PMhow can i add ? when player type
 if ( cmd == "flagattach" )
{
player attack PickedID = 6000 how can i do this like simple Player.Attach.PickupID = 6005;
}

attach player ID

player attack PickedID -> y the f* ur attacking ?

=RK=MarineForce

megamind attach not attack lel. i want code that when player took the flag flag pickup id will attach to him how

umar4911

Quote from: =RK=MarineForce on Sep 27, 2018, 05:26 AMmegamind attach not attack lel. i want code that when player took the flag flag pickup id will attach to him how
you want to make bike like VW?

NicusorN5

@=RK=MarineForce You can remove the pickup and then create a flag object then attach it to an player.

!

#12
Quote from: =RK=MarineForce on Sep 26, 2018, 12:31 PMhow can i add ? when player type
 if ( cmd == "flagattach" )
{
player attack PickedID = 6000 how can i do this like simple Player.Attach.PickupID = 6005;
}

attach player ID

If you have installed mine version you can attach any of the instance[ player, pickup, vehicle, object ] to any of instance[ player, pickup, vehicle, object ]
means for attaching pickup to bike
local instancePickup = FindPickup( integer pickupID );
local instanceVehicle = FindVehicle( integer vehicleID );

attachEntity( instancePickup, instanceVehicle,Vector( 0, 0, 3 ) );

 :edit: For the original snippet
Usage if I am not wrong
Will not work better use the above one. Clk
local instancePickup = FindPickup( integer pickupID );
local instancePlayer = FindPlayer( integer playerID );

instancePlayer.Attach( "CPickup", instancePickup, Vector( 0, 0, 3 ) );

Discord: zeus#5155

NicusorN5

It will attach the player to the pickup, so the player will remain stuck.