Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: NicusorN5 on Sep 07, 2018, 12:37 PM

Title: Attaching player to an entity functions
Post by: NicusorN5 on Sep 07, 2018, 12:37 PM
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");
}
Title: Re: Attaching player to an entity functions
Post by: ! on Sep 07, 2018, 07:20 PM
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'
Title: Re: Attaching player to an entity functions
Post by: MEGAMIND on Sep 08, 2018, 06:14 AM
A suggestion, make it using if & else statement for other player to make it understandable instead if switch case
Title: Re: Attaching player to an entity functions
Post by: Xmair on Sep 08, 2018, 05:48 PM
People should learn using switch case then.
Title: Re: Attaching player to an entity functions
Post by: NicusorN5 on Sep 08, 2018, 06:12 PM
Quote from: Xmair on Sep 08, 2018, 05:48 PMPeople should learn using switch case then.
Totally agree.
Title: Re: Attaching player to an entity functions
Post by: MEGAMIND on Sep 08, 2018, 07:38 PM
Quote from: Athanatos on Sep 08, 2018, 06:12 PM
Quote from: Xmair on Sep 08, 2018, 05:48 PMPeople should learn using switch case then.
Totally agree.

Agreed
Title: Re: Attaching player to an entity functions
Post by: D4rkR420R on Sep 08, 2018, 08:56 PM
This is something I had in mind. ^-^ Nice one, pleb. You made my day.
Title: Re: Attaching player to an entity functions
Post by: =RK=MarineForce on Sep 26, 2018, 12:31 PM
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
Title: Re: Attaching player to an entity functions
Post by: MEGAMIND on Sep 26, 2018, 01:30 PM
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 ?
Title: Re: Attaching player to an entity functions
Post by: =RK=MarineForce on Sep 27, 2018, 05:26 AM
megamind attach not attack lel. i want code that when player took the flag flag pickup id will attach to him how
Title: Re: Attaching player to an entity functions
Post by: umar4911 on Sep 27, 2018, 09:26 AM
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?
Title: Re: Attaching player to an entity functions
Post by: NicusorN5 on Sep 27, 2018, 04:16 PM
@=RK=MarineForce You can remove the pickup and then create a flag object then attach it to an player.
Title: Re: Attaching player to an entity functions
Post by: ! on Sep 27, 2018, 05:20 PM
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 (https://forum.vc-mp.org/?topic=6224.msg43116#msg43116)
local instancePickup = FindPickup( integer pickupID );
local instancePlayer = FindPlayer( integer playerID );

instancePlayer.Attach( "CPickup", instancePickup, Vector( 0, 0, 3 ) );
Title: Re: Attaching player to an entity functions
Post by: NicusorN5 on Sep 27, 2018, 06:41 PM
It will attach the player to the pickup, so the player will remain stuck.