1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

Vehicle door locks with keyless remotes

Discussion in 'Snippets' started by Icaro, Nov 21, 2016.

  1. Icaro

    Icaro Active Member

    Messages:
    59
    Likes Received:
    36
    Joined:
    Oct 20, 2016
    Client:

    Code:
    API.onServerEventTrigger.connect(function(name, args) {
        if (name == "UPDATE_VEHICLELOCK") {
            setVehLocked(args[0]);
        }
    });
    
    API.onEntityStreamIn.connect(function(ent, entType) {
        if (entType == 1) {
            setVehLocked(ent);
        }
    });
    
    function setVehLocked(ent) {
        if (API.isVehicle(ent) &&
            (API.getEntityData(ent, "Locked") == true))
        {
            API.callNative("SET_VEHICLE_DOORS_LOCKED", ent, 2);
        }
        else
        {
            API.callNative("SET_VEHICLE_DOORS_LOCKED", ent, 0);
        }
    }
    Server, as you can see it's just a copypaste from my gamemode, your implementation will be different:

    Code:
           public class VKey : Item
            {
                public VKey(API api, int id, string name, string description) : base(api, id, name, description)
                {
                }
    
                public void Use(Client c)
                {
                    foreach (NetHandle Vehicle in Vehicles)
                    {
                        if (IsInRangeOf(c.Position, api.getEntityPosition(Vehicle), 20f) && api.getEntityData(Vehicle, "KeyID") == ID)
                        {
                            if (api.getEntityData(Vehicle, "Locked") == true)
                            {
                                api.sendNotificationToPlayer(c, "Unlocked");
                                api.setEntityData(Vehicle, "Locked", false);
                            }
                            else
                            {
                                api.sendNotificationToPlayer(c, "Locked");
                                api.setEntityData(Vehicle, "Locked", true);
                            }
                            api.triggerClientEventForAll("UPDATE_VEHICLELOCK", Vehicle);
                        }
                    }
                }
            }
    
            public static bool IsInRangeOf(Vector3 playerPos, Vector3 target, float range)
            {
                var direct = new Vector3(target.X - playerPos.X, target.Y - playerPos.Y, target.Z - playerPos.Z);
                var len = direct.X * direct.X + direct.Y * direct.Y + direct.Z * direct.Z;
                return range * range > len;
            }
    
     
    TroloveCro, WeirdestYeti and StreetGT like this.
  2. Icaro

    Icaro Active Member

    Messages:
    59
    Likes Received:
    36
    Joined:
    Oct 20, 2016
    Ok with the new API this workaround with natives isn't needed anymore, you can just use API.setVehicleLocked
     
    Uncreative likes this.
  3. Electro957

    Electro957 Member

    Messages:
    27
    Likes Received:
    0
    Joined:
    Dec 29, 2016
    where we should paste this code? please answer fastest you can!
     
  4. Bitdevil2016

    Bitdevil2016 Member

    Messages:
    24
    Likes Received:
    0
    Joined:
    May 14, 2017
    My Vehicle is personal Vehicle and the Range function is not Working.

    What can i do?

    And the next question this close only 1 door from Vehicle with press key G come in the Vehicle
     

Share This Page