Jump to content

[C#/.NET] Simple Helpful Functions


Recommended Posts

    void RagdollPed(Ped ped, int duration)    {        Function.Call(Hash.SET_PED_CAN_RAGDOLL, ped, true);        Function.Call(Hash.SET_PED_TO_RAGDOLL, ped, duration, duration, 0, 0, 0, 0);    }

This ragdoll function needs two parameters: the pedestrian that will ragdoll and how long they will ragdoll for.


Example: RagdollPed(Game.Player.Character, 5000); - This will ragdoll the player for 5 seconds.

    void ChangePlayerModel(string Model)    {        Model PedModel = Model;        PedModel.Request(500);        if (PedModel.IsInCdImage && PedModel.IsValid)        {            while (!PedModel.IsLoaded) Script.Wait(100);            Function.Call(Hash.SET_PLAYER_MODEL, Game.Player, PedModel.Hash);;        }        PedModel.MarkAsNoLongerNeeded();    }

This player model function will allow you to change your player model with ease, all you need is the model name.


Example: ChangePlayerModel("a_c_cop"); - This will change you into Chop.



I will be posting some simple functions I made that combine multiple natives together to get an easy and simple result, feel free to post any that you have and I will update.



  • Like 3
Link to comment
Share on other sites

  • 1 month later...
    /// <summary>    /// Gets the id of the player    /// </summary>    private int GetPlayerId()    {        return Function.Call<int>(Hash.PLAYER_ID);    }
    /// <summary>    /// Gets the ped handle of the player    /// </summary>    private Ped GetPlayerPed()    {        //return Game.Player.Character;        return Function.Call<Ped>(Hash.PLAYER_PED_ID);    }
    /// <summary>    /// Gets player's group id    /// </summary>    private int GetPlayerGroup()    {        return Function.Call<int>(Hash.GET_PED_GROUP_INDEX, GetPlayerPed());    }

These are probably the most used natives you will always end up needed what they return.

Link to comment
Share on other sites

  • 3 weeks later...

This one returns if the ped is in combat with any other ped. I know shv.net has a check for this, but it doesn't work for the player, while this one does.

    bool IsInCombat(Ped ped)    {        foreach (Ped target in World.GetAllPeds())        {            if (target.IsAlive && GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_COMBAT, target, ped))            {                return true;            }        }        return false;    }

Useful for any entity you need to touch, checks if the entity isn't null, then if the entity itself exists in the world.

    bool CanWeUse(Entity entity)    {        return entity != null && entity.Exists();    }

Renames blips.

void SetBlipName(Blip blip, string text)    {        GTA.Native.Function.Call(GTA.Native.Hash._0xF9113A30DE5C6670, "STRING");        GTA.Native.Function.Call(GTA.Native.Hash._ADD_TEXT_COMPONENT_STRING, text);        GTA.Native.Function.Call(GTA.Native.Hash._0xBC38B49BCB83BC9B, blip);    }

Returns the player waypoint coords. If the coords aren't valid, it returns the closest -valid- point to the waypoint.

    Vector3 GetWaypointCoords()    {           Vector3 pos = Function.Call<Vector3>(Hash.GET_BLIP_COORDS, Function.Call<Blip>(Hash.GET_FIRST_BLIP_INFO_ID, 8));        if (Function.Call<bool>(Hash.IS_WAYPOINT_ACTIVE) && pos != null || pos != new Vector3(0, 0, 0))        {            Vector3 WayPos = ToGround(pos);            if (WayPos.Z == 0 || WayPos.Z == 1)            {                WayPos = World.GetNextPositionOnStreet(WayPos);                return WayPos;            }        }        return Vector3.Zero;    }    private Vector3 ToGround(Vector3 position)    {        position.Z = World.GetGroundHeight(new Vector2(position.X, position.Y));        return new Vector3(position.X, position.Y, position.Z);    }

Teleports a list of peds on a vehicle.

    void GetSquadIntoVehicle(List<Entity> Squad, Vehicle Vehicle)    {        int max_seats = GTA.Native.Function.Call<int>(GTA.Native.Hash.GET_VEHICLE_MAX_NUMBER_OF_PASSENGERS, Vehicle);        for (int i = -1; i < max_seats; i++)        {            if (i == Squad.Count - 1)            {                break;            }            if (GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_VEHICLE_SEAT_FREE, Vehicle, i) && (CanWeUse(Squad[i + 1])))            {                GTA.Native.Function.Call<bool>(GTA.Native.Hash.TASK_ENTER_VEHICLE, Squad[i + 1], Vehicle, -1, i, 2.0, 16, 0);            }        }    }
Edited by Eddlm
  • Like 1
Link to comment
Share on other sites

This one is especially useful, Rockstar uses it in all if not most their scripts.

    int GetRandomIntInRange(int min, int max)    {        Random rand = new Random();        return rand.Next(min, max + 1);    }
Edited by jedijosh920
Link to comment
Share on other sites

But... but... but...

    int Random(int min,int max)    {        return Function.Call<int>(Hash.GET_RANDOM_INT_IN_RANGE, min, max);    }

(There is another native for a random float, too)

Edited by Eddlm
Link to comment
Share on other sites

Here are some extensions for picking random items out of collections (arrays, lists enums etc.)
Personally I use these all the time.

    /// <summary>    /// Extension for getting a random item from a collection    /// </summary>    /// <typeparam name="T">Type of collection</typeparam>    /// <param name="items"></param>    /// <returns></returns>    public static T GetRandomItem<T>(this IEnumerable<T> items)    {        if (items.Count() < 1) return default(T);        var random = new Random(Guid.NewGuid().GetHashCode());        return (T)(object)items.ToArray()[random.Next(0, items.Count())];    }    /// <summary>    /// Extension for getting a random enum item    /// </summary>    /// <typeparam name="T">Type of enum</typeparam>    /// <param name="items"></param>    /// <returns></returns>    public static T GetRandomItem<T>(this Enum items)    {        var types = Enum.GetValues(typeof(T));        return types.Cast<T>().GetRandomItem();    }


var randomHash = default(VehicleHash).GetRandomItem<VehicleHash>();new List<int>{ 1, 2, 3}.GetRandomItem();Enumerable.Range(0, 1000).GetRandomItem();
Edited by CamxxCore
Link to comment
Share on other sites

  • 2 weeks later...
    bool AnyCopNear(Vector3 pos, float radius)    {        return Function.Call<bool>(Hash.IS_COP_PED_IN_AREA_3D, pos.X - radius, pos.Y - radius, pos.Z - radius, pos.X + radius, pos.Y + radius, pos.Z + radius);    }

Pretty self explanatory.

    Vehicle GetLastVehicle(Ped RecieveOrder)    {        Vehicle vehicle = null;        if (GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, true) != null)        {            vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, true);            if (vehicle.IsAlive)            {                return vehicle;            }        }        else        {            if (GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, false) != null)            {                vehicle = GTA.Native.Function.Call<Vehicle>(GTA.Native.Hash.GET_VEHICLE_PED_IS_IN, RecieveOrder, false);                if (vehicle.IsAlive)                {                    return vehicle;                }            }        }        return vehicle;    }

This function returns the current vehicle the ped is in, or, if the ped isn't in any vehicle, the last vehicle the ped used.

Edited by Eddlm
Link to comment
Share on other sites


Display Help Text on Screen



    Private Sub DisplayHelpText(ByVal [text] As String)        Dim arguments As InputArgument() = New InputArgument() {"STRING"}        Native.Function.Call(Hash._0x8509B634FBE7DA11, arguments)        Dim argumentArray2 As InputArgument() = New InputArgument() {[text]}        Native.Function.Call(Hash._0x6C188BE134E074AA, argumentArray2)        Dim argumentArray3 As InputArgument() = New InputArgument() {0, 0, 1, -1}        Native.Function.Call(Hash._0x238FFE5C7B0498A6, argumentArray3)    End Sub


    private void DisplayHelpText(string text)    {        InputArgument[] arguments = new InputArgument[] { "STRING" };        Native.Function.Call(Hash._0x8509B634FBE7DA11, arguments);        InputArgument[] argumentArray2 = new InputArgument[] { text };        Native.Function.Call(Hash._0x6C188BE134E074AA, argumentArray2);        InputArgument[] argumentArray3 = new InputArgument[] {0,0,1,-1};        Native.Function.Call(Hash._0x238FFE5C7B0498A6, argumentArray3);    }


                    DisplayHelpText("Press ~INPUT_CONTEXT~ to enter Premium Deluxe Motorsport.")
Edited by qiangqiang101
Link to comment
Share on other sites

    float GetRandomFloatInRange(float min, float max)    {        return Function.Call<float>(Hash.GET_RANDOM_FLOAT_IN_RANGE, min, max);    }

Helpful for getting random floats.

Link to comment
Share on other sites


This will load the MP Maps to SP.

    Public Shared Sub LoadMPDLCMap()        Native.Function.Call(Hash._LOAD_MP_DLC_MAPS)        Native.Function.Call(Hash._ENABLE_MP_DLC_MAPS, New Native.InputArgument() {1})    End Sub

This will load the Interiors from MP to SP like Apartments/Garages

    Public Shared Sub SetInteriorActive(X As Single, Y As Single, Z As Single)        Dim interiorID As Integer = Native.Function.Call(Of Integer)(Hash.GET_INTERIOR_AT_COORDS, X, Y, Z)        Native.Function.Call(Hash._0x2CA429C029CCF247, New InputArgument() {interiorID})        Native.Function.Call(Hash.SET_INTERIOR_ACTIVE, interiorID, True)        Native.Function.Call(Hash.DISABLE_INTERIOR, interiorID, False)    End Sub

Sorry Functions are in VB, convert yourself.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.