Quantcast

Jump to content

» «
Photo

[C#/.NET] Simple Helpful Functions

9 replies to this topic
jedijosh920
  • jedijosh920

    ⭐⭐⭐⭐⭐

  • Members
  • Joined: 01 Mar 2012
  • United-States

#1

Posted 06 August 2015 - 07:27 PM

    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.

 

 

  • FowardElement, Konijima and decoy1212 like this

Agent44
  • Agent44

    Player Hater

  • New Members
  • Joined: 08 Jul 2015
  • United-States

#2

Posted 02 October 2015 - 04:42 PM

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


Eddlm
  • Eddlm

    Newie

  • Members
  • Joined: 24 Aug 2015
  • Spain

#3

Posted 18 October 2015 - 12:18 AM Edited by Eddlm, 18 October 2015 - 12:20 AM.

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);
            }
        }
    }
  • jedijosh920 and LeeC2202 like this

jedijosh920
  • jedijosh920

    ⭐⭐⭐⭐⭐

  • Members
  • Joined: 01 Mar 2012
  • United-States

#4

Posted 22 October 2015 - 06:55 PM Edited by jedijosh920, 22 October 2015 - 06:55 PM.

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

Eddlm
  • Eddlm

    Newie

  • Members
  • Joined: 24 Aug 2015
  • Spain

#5

Posted 24 October 2015 - 11:11 PM Edited by Eddlm, 24 October 2015 - 11:11 PM.

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)


CamxxCore
  • CamxxCore

    Mark Chump

  • Members
  • Joined: 19 Oct 2013
  • Canada

#6

Posted 27 October 2015 - 01:29 AM Edited by CamxxCore, 27 October 2015 - 01:30 AM.

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




Eddlm
  • Eddlm

    Newie

  • Members
  • Joined: 24 Aug 2015
  • Spain

#7

Posted 05 November 2015 - 02:09 AM Edited by Eddlm, 05 November 2015 - 02:12 AM.

    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.

  • decoy1212 likes this

qiangqiang101
  • qiangqiang101

    I'm Not MentaL

  • Members
  • Joined: 02 Feb 2010
  • Malaysia

#8

Posted 06 November 2015 - 08:26 AM Edited by qiangqiang101, 06 November 2015 - 08:30 AM.

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.")

jedijosh920
  • jedijosh920

    ⭐⭐⭐⭐⭐

  • Members
  • Joined: 01 Mar 2012
  • United-States

#9

Posted 06 November 2015 - 09:06 PM

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

Helpful for getting random floats.


qiangqiang101
  • qiangqiang101

    I'm Not MentaL

  • Members
  • Joined: 02 Feb 2010
  • Malaysia

#10

Posted 08 November 2015 - 11:31 AM

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.

  • froggz19 likes this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users