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.

Useful code snippets

Discussion in 'Snippets' started by MasterZero, Oct 20, 2016.

  1. MasterZero

    MasterZero Member

    Messages:
    42
    Likes Received:
    22
    Joined:
    Oct 4, 2016
    Collection thread: Share your useful code snippets! :=)

    Write position to a file:
    Example: /save LSPD
    Code:
    [Command("save", GreedyArg = true)]
    public void Command_Save(Client sender, string name = "Unknown")
    {
        var pos = API.getEntityPosition(sender);
        var angle = API.getEntityRotation(sender);
        File.AppendAllText("savepos.txt", string.Format("{0}: {1} {2} {3} {4}", name, pos.X, pos.Y, pos.Z, angle));
        API.sendNotificationToPlayer(sender, string.Format("Position saved as: {0}", name), true);
    }
    
     
    Last edited: Oct 21, 2016
    DaniSteptu, kova98, SkyLaGer and 3 others like this.
  2. MasterZero

    MasterZero Member

    Messages:
    42
    Likes Received:
    22
    Joined:
    Oct 4, 2016
    Easy hash handling. ;-)
    Usage:
    Code:
    HashFunctions.IS_CONTROL_ENABLED
    Example glue.cs:
    Code:
    Old:
    if (API.fetchNativeFromPlayer<bool>(sender, 0x17FFC1B2BA35A494, sender.CharacterHandle, targetVehicle))
    
    New:
    if (API.fetchNativeFromPlayer<bool>(sender, HashFunction.IS_ENTITY_TOUCHING_ENTITY, sender.CharacterHandle, targetVehicle))

    Class: http://pastebin.com/sEpu6nHA
     
    Last edited: Nov 18, 2016
    DaniSteptu, Chenko, Blitzer and 3 others like this.
  3. Icaro

    Icaro Active Member

    Messages:
    59
    Likes Received:
    36
    Joined:
    Oct 20, 2016
    Code:
                new ATM(API, new Vector3(-1109.797f, -1690.808f, 4.375014f), 122.9616f);
                new ATM(API, new Vector3(-821.6062f, -1081.885f, 11.13243f), 29.3056f);
                new ATM(API, new Vector3(-537.8409f, -854.5145f, 29.28953f), 182.9156f);
                new ATM(API, new Vector3(-1315.744f, -834.6907f, 16.96173f), 311.8347f);
                new ATM(API, new Vector3(-1314.786f, -835.9669f, 16.96015f), 310.3952f);
                new ATM(API, new Vector3(-1570.069f, -546.6727f, 34.95547f), 215.2224f);
                new ATM(API, new Vector3(-1571.018f, -547.3666f, 34.95734f), 218.438f);
                new ATM(API, new Vector3(-866.6416f, -187.8008f, 37.84286f), 123.5083f);
                new ATM(API, new Vector3(-867.6165f, -186.1373f, 37.8433f), 120.8281f);
                new ATM(API, new Vector3(-721.1284f, -415.5296f, 34.98175f), 268.9118f);
                new ATM(API, new Vector3(-254.3758f, -692.4947f, 33.63751f), 159.0533f);
                new ATM(API, new Vector3(24.37422f, -946.0142f, 29.35756f), 339.1346f);
                new ATM(API, new Vector3(130.1186f, -1292.669f, 29.26953f), 300.0509f);
                new ATM(API, new Vector3(129.7023f, -1291.954f, 29.26953f), 303.6002f);
                new ATM(API, new Vector3(129.2096f, -1291.14f, 29.26953f), 298.3779f);
                new ATM(API, new Vector3(288.8256f, -1282.364f, 29.64128f), 271.0125f);
                new ATM(API, new Vector3(1077.768f, -776.4548f, 58.23997f), 186.3605f);
                new ATM(API, new Vector3(527.2687f, -160.7156f, 57.08937f), 272.5496f);
                new ATM(API, new Vector3(-867.5897f, -186.1757f, 37.84291f), 117.088f);
                new ATM(API, new Vector3(-866.6556f, -187.7766f, 37.84278f), 118.6207f);
                new ATM(API, new Vector3(-1205.024f, -326.2916f, 37.83985f), 117.6146f);
                new ATM(API, new Vector3(-1205.703f, -324.7474f, 37.85942f), 117.3035f);
                new ATM(API, new Vector3(-1570.167f, -546.7214f, 34.95663f), 216.3378f);
                new ATM(API, new Vector3(-1571.056f, -547.3947f, 34.95724f), 213.4567f);
                new ATM(API, new Vector3(-57.64693f, -92.66162f, 57.77995f), 295.8091f);
                new ATM(API, new Vector3(527.3583f, -160.6381f, 57.0933f), 271.9661f);
                new ATM(API, new Vector3(-165.1658f, 234.8314f, 94.92194f), 90.9362f);
                new ATM(API, new Vector3(-165.1503f, 232.7887f, 94.92194f), 94.44688f);
                new ATM(API, new Vector3(-2072.445f, -317.3048f, 13.31597f), 269.9654f);
                new ATM(API, new Vector3(-3241.082f, 997.5428f, 12.55044f), 40.48888f);
                new ATM(API, new Vector3(-1091.462f, 2708.637f, 18.95291f), 44.16092f);
                new ATM(API, new Vector3(1172.492f, 2702.492f, 38.17477f), 359.9989f);
                new ATM(API, new Vector3(1171.537f, 2702.492f, 38.17542f), 359.9672f);
                new ATM(API, new Vector3(1822.637f, 3683.131f, 34.27678f), 207.7707f);
                new ATM(API, new Vector3(1686.753f, 4815.806f, 42.00874f), 272.3396f);
                new ATM(API, new Vector3(1701.209f, 6426.569f, 32.76408f), 65.12852f);
                new ATM(API, new Vector3(-95.54314f, 6457.19f, 31.46093f), 46.20586f);
                new ATM(API, new Vector3(-97.23336f, 6455.469f, 31.46682f), 49.50279f);
                new ATM(API, new Vector3(-386.7451f, 6046.102f, 31.50172f), 315.2239f);
                new ATM(API, new Vector3(-1091.42f, 2708.629f, 18.95568f), 46.68598f);
                new ATM(API, new Vector3(5.132f, -919.7711f, 29.55953f), 250.4304f);
                new ATM(API, new Vector3(-660.703f, -853.971f, 24.484f), 180.1663f);
                new ATM(API, new Vector3(-2293.827f, 354.817f, 174.602f), 115.5668f);
                new ATM(API, new Vector3(-2294.637f, 356.553f, 174.602f), 113.6837f);
                new ATM(API, new Vector3(-2295.377f, 358.241f, 174.648f), 110.682f);
                new ATM(API, new Vector3(-1409.782f, -100.41f, 52.387f), 109.271f);
                new ATM(API, new Vector3(-1410.279f, -98.649f, 52.436f), 112.71f);
    ATM Coordinates
     
  4. TroloveCro

    TroloveCro Member

    Messages:
    28
    Likes Received:
    13
    Joined:
    Nov 12, 2016
    Code:
        public void sendCloseMessage(Client player, float radius, string sender, string msg)
        {
            List<Client> nearPlayers = API.getPlayersInRadiusOfPlayer(radius, player);
            foreach (Client target in nearPlayers)
            {
                API.sendChatMessageToPlayer(player, sender, msg);
            }
        }
        public void OnPlayerChat(Client player, string message, CancelEventArgs e)
        {
            sendCloseMessage(player, 15.0f, "~#ffffff~", API.getPlayerName(player) + " says: " + message);
            e.Cancel = true;
            return;
        }
    
    Code:
        [Command("me", GreedyArg = true)] // help command 
        public void Command_me(Client sender, string message)
         {
             sendCloseMessage(sender, 15.0f, "~#C2A2DA~", API.getPlayerName(sender) + " " + message + ".");
         }
         [Command("do", GreedyArg = true)] // do command 
         public void Command_do(Client sender, string message)
         {
            sendCloseMessage(sender, 15.0f, "~#C2A2DA~", "(( " + message + ". )) " + API.getPlayerName(sender));
        }
         [Command("b", GreedyArg = true)] // ooc chat command
         public void Command_b(Client sender, string message)
         {
             sendCloseMessage(sender, 15.0f, "~#ffffff~", API.getPlayerName(sender) +  ": " + "(( " + message + " ))");
         }
         [Command("s", Alias = "shout", GreedyArg = true)] // shout command
         public void Command_s(Client sender, string message)
         {
             sendCloseMessage(sender, 25.0f, "~#ffffff~", API.getPlayerName(sender) + " shout: " + message + "!");
         }
         [Command("w", Alias = "whisper", GreedyArg = true)] // whisper command
         public void Command_w(Client sender, string message)
         {
             sendCloseMessage(sender, 7.5f, "~#ffffff~", API.getPlayerName(sender) + " whisper: " + message + ".");
         }
    
    Simple /me /do /b /s /w cmds
     
    DaniSteptu, EN3RGGY, GroX0r and 3 others like this.
  5. PrideGame

    PrideGame Member

    Messages:
    28
    Likes Received:
    3
    Joined:
    Nov 27, 2016
    Simple /veh command.
    Code:
    [Command("veh")]
      public void spawnVehicle(Client player, string veh, int col1, int col2)
      {
          API.sendChatMessageToPlayer(player, "~g~spawned vehicle");
          Vector3 vehPos = API.getEntityPosition(player);
          Vector3 vehRot = API.getEntityRotation(player);
          VehicleHash myVehicle = API.vehicleNameToModel(veh);
          API.createVehicle(myVehicle, vehPos, vehRot, col1, col2); //Spawned vehicle is visible in all Dimensions
      }
    
     
    Last edited: Dec 25, 2016
    DaniSteptu likes this.
  6. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    40
    Likes Received:
    21
    Joined:
    Oct 27, 2016
    Hello,

    As the community is getting bigger and a lot of people are trying to improve their scripting skills,
    I got to understand that such topic could really help.

    Here you can share your own functions and code snippets.
    Please use the CODE tag. Also specify which language is your code / wether it's for server or client side scripts.

    I'll start with a few for serverside with C#:
    Code:
        public bool IsValidSkin(string name)
        {
            return Enum.IsDefined(typeof(PedHash), name);
        }
        public bool IsValidVehicle(string name)
        {
            return Enum.IsDefined(typeof(VehicleHash), name);
        }
        public bool IsValidWeapon(string name)
        {
            return Enum.IsDefined(typeof(WeaponHash), name);
        }
        public bool IsValidPickup(string name)
        {
            return Enum.IsDefined(typeof(PickupHash), name);
        }
        public bool IsValidWeather(int weather)
        {
            return weather >= 0 && weather <= 13;
        }
    
    Usage example:
    Code:
        [Command("s", Description = "Sets your skin", GreedyArg = true)]
        public void Command_Skin(Client player, string name)
        {
            if (!IsValidSkin(name))
            {
                player.sendChatMessage("~r~Error:~w~ Not a valid skin.");
                return;
            }
            var s = API.pedNameToModel(name);
            API.setPlayerSkin(player, s);
            API.sendNotificationToPlayer(player, "Skin set to ~b~" + s.ToString());
        }
    
     
    MasterZero and StreetGT like this.
  7. DaGaXeR

    DaGaXeR Member

    Messages:
    30
    Likes Received:
    6
    Joined:
    Dec 20, 2016
    Simple "Role Play name". Removes underscore from nickname.
    Code:
    public string playerName(Client player)
        { 
            return System.Text.RegularExpressions.Regex.Replace(API.getPlayerName(player), "_", " ");
        }
     
    GroX0r and Amit_B like this.
  8. Bweff

    Bweff Member Donator

    Messages:
    18
    Likes Received:
    4
    Joined:
    Oct 3, 2016
    Code:
        static class VehicleFlags
        {
            static public int IS_IN_DRIVER_SEAT = -1;
            static public int IS_IN_PASSENGER_SEAT = 0;
            static public int IS_IN_LEFT_BACK_SEAT = 1;
            static public int IS_IN_RIGHT_BACK_SEAT = 2;
        }
    Example (check if player is in drivers seat)

    Code:
    if (player.isInVehicle)
    {
        if (player.vehicleSeat == VehicleFlags.IS_IN_DRIVER_SEAT)
        {
            // Your code here
        }
    }
     
    Chumillas likes this.
  9. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    40
    Likes Received:
    21
    Joined:
    Oct 27, 2016
    I suggest to change these to constants.
     
  10. Electro957

    Electro957 Member

    Messages:
    27
    Likes Received:
    0
    Joined:
    Dec 29, 2016
    where we need to paste this code?
     
  11. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    PrideGame likes this.
  12. Float

    Float Member

    Messages:
    29
    Likes Received:
    2
    Joined:
    Dec 2, 2016
    This works as well
    Code:
     [Command("vehicle")]
            public void CreateVehicleCommand(Client player , VehicleHash vehicle , int c1 , int c2)
            {
                API.createVehicle(vehicle, player.position, player.rotation, c1, c2, 0);
            }
     
    kova98 likes this.
  13. Jack8814

    Jack8814 Member

    Messages:
    10
    Likes Received:
    4
    Joined:
    Jan 5, 2017
    Couldn't you also just do player.name.replace("_", " ")?
     
  14. DaGaXeR

    DaGaXeR Member

    Messages:
    30
    Likes Received:
    6
    Joined:
    Dec 20, 2016
    I'm new in c#, and nobody faster pasted something like that. ;-)
     
  15. batman

    batman Member

    Messages:
    6
    Likes Received:
    10
    Joined:
    Jan 9, 2017
    getVehicleClass classes and their int value:
    Code:
    0: Compact
    1: Sedans
    2: SUVs
    3: Coupe
    4: Muscle
    5: Sport Classics
    6: Sports
    7: Super
    8: Motorcycles
    9: Offroad
    10: Industrial
    11: Utility
    12: Vans
    13: Cycle
    14: Boat
    15: Heli
    16: Planes
    17: Service
    18: Emergency
    19: Military
    20: Commercial
    21: Train
    22: Trailer
     
    Xenius, Amit_B, Jack8814 and 2 others like this.
  16. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    40
    Likes Received:
    21
    Joined:
    Oct 27, 2016
    Code:
            public enum VehicleClass
            {
                Compact = 0,
                Sedans = 1,
                SUVs = 2,
                Coupe = 3,
                Muscle = 4,
                SportClassics = 5,
                Sports = 6,
                Super = 7,
                Motorcycles = 8,
                Offroad = 9,
                Industrial = 10,
                Utility = 11,
                Vans = 12,
                Cycle = 13,
                Boat = 14,
                Heli = 15,
                Planes = 16,
                Service = 17,
                Emergency = 18,
                Military = 19,
                Commercial = 20,
                Train = 21,
                Trailer = 22
            }
            public VehicleClass GetVehicleClass(VehicleHash v)
            {
                return (VehicleClass)API.getVehicleClass(v);
            }
    
     
  17. Ruffo

    Ruffo Member

    Messages:
    11
    Likes Received:
    3
    Joined:
    Dec 26, 2016
    Hey there,
    here's a nice Code snippet to get a Online PlayerName from only a piece of his name. And a snippet to get the searched player Client directly.
    (ffo = Ruffo)
    Code:
    /// <summary>
    /// Searching if the name of any online-player will match the entered.
    /// </summary>
    /// <param name="PPieceName">Piece of the searched Playername</param>
    /// <returns>Playername String</returns>
    public string getPlayerNameFromStr(string PPieceName) {
        string returnStr = "";
        //Upper the PieceName
        PPieceName = PPieceName.ToUpper();
        //First check if the name is Complete.
        foreach (Client Player in API.getAllPlayers())
        {
            if (Player.name.ToUpper() == PPieceName)
            {
                returnStr = Player.name;
                break;
            }
        }
    
        foreach (Client Player in API.getAllPlayers())
        {
            if (Player.name.ToUpper().Contains(PPieceName))
            {
                returnStr = Player.name;
                break;
            }
        }
        return returnStr;
    }
    
    and getPlayerFromStr() returns Client
    Code:
    /// <summary>
    /// Searching if the name of any online-player will match the entered.
    /// </summary>
    /// <param name="PPieceName">Piece of the searched Playername</param>
    /// <returns>Client Player, null if not found</returns>
    public Client getPlayerFromStr(string PPieceName) {
        Client returnClient = null;
        //Upper the PieceName
        PPieceName = PPieceName.ToUpper();
        //First check if the name is Complete.
        foreach (Client Player in API.getAllPlayers())
        {
            if (Player.name.ToUpper() == PPieceName)
            {
                returnClient = Player;
                break;
            }
        }
    
        if (returnClient == null)
        {
            foreach (Client Player in API.getAllPlayers())
            {
                if (Player.name.Contains(PPieceName))
                {
                    returnClient = Player;
                    break;
                }
            }
        }
        return returnClient;
    }
    
    Edit:
    2. Performance optimization
    1. Added function that returns the PlayerObject directly
     
    Last edited: Jan 17, 2017
    kova98 and dnx like this.
  18. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    Why not just return the Client object itself? You could do a lot more than just the name with that.

    It would also be more efficient to call ToUpper on the input string only once instead of every iteration.
     
  19. Ruffo

    Ruffo Member

    Messages:
    11
    Likes Received:
    3
    Joined:
    Dec 26, 2016
    Perfomance.
    We use this function always to Displaying results or searching in Database. So the Client is not needed in this case.

    yea, thats true. (Gonna editing it)
     
  20. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    It's not like you're copying the client, in fact returning a string can be seen as slower than returning the client object, because I believe the string is copied on return while the client object is just a reference :=P
     
    Ruffo likes this.

Share This Page