Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums! (92,322 visits to this link)

    2. News

    1. GTA Online

      1. Find Lobbies & Players
      2. Guides & Strategies
      3. Vehicles
      4. Content Creator
      5. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption 2

    2. Red Dead Redemption

    3. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

jedijosh920

[C#/.NET] Simple Helpful Functions

Recommended Posts

jedijosh920
    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.

 

 

Share this post


Link to post
Share on other sites
Agent44
    /// <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.

Share this post


Link to post
Share on other sites
Eddlm

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

Share this post


Link to post
Share on other sites
jedijosh920

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

Share this post


Link to post
Share on other sites
Eddlm

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

Share this post


Link to post
Share on other sites
CamxxCore

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();    }

example:

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

Share this post


Link to post
Share on other sites
Eddlm
    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

Share this post


Link to post
Share on other sites
qiangqiang101

Display Help Text on Screen

 

VB.NET

    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

C#.NET

    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);    }

example

                    DisplayHelpText("Press ~INPUT_CONTEXT~ to enter Premium Deluxe Motorsport.")
Edited by qiangqiang101

Share this post


Link to post
Share on other sites
jedijosh920
    float GetRandomFloatInRange(float min, float max)    {        return Function.Call<float>(Hash.GET_RANDOM_FLOAT_IN_RANGE, min, max);    }

Helpful for getting random floats.

Share this post


Link to post
Share on other sites
qiangqiang101

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.

Share this post


Link to post
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

×

Important Information

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