Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

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

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

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

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

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

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

    2. Forum Support

    3. Suggestions

[Lua] [C#] Finding Peds or Orbjects around


Aktarus
 Share

Recommended Posts

Hello,

 

I am trying to convert this piece of code (Lua) into C#. Any help?

extra natives component with entity iteration nativesusage example:```lualocal handle, ped = FindFirstPed()local successrepeat	local pos = GetEntityCoords(ped)	DrawMarker(1, pos.x, pos.y, pos.z - 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.8, 2.0, 255, 255, 0, 75, 0, 0, 2, 0, 0, 0, 0)	success, ped = FindNextPed(handle)until not successEndFindPed(handle)
Link to comment
Share on other sites

I have tried that and it only works for the first ped (the Player) I don't know why the loop doesn't work. I suspect the "FIND_NEXT_PED" functions.

Any help?

I precise that this is for a Fivem project

            var handle = 0;            var success = false;            var entityOut = new OutputArgument();            Function.Call<int>(Hash.FIND_FIRST_PED, entityOut);            int ped = entityOut.GetResult<int>();            do            {                Vector3 Pos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, ped);                API.DrawMarker(1, Pos.X, Pos.Y, Pos.Z - 1f, 0f, 0f, 0f, 0f, 0f, 0f, .8f, 8f, 2f, 255, 255, 0, 75, false, false, 2, false, null, null, false);                var nextEntityOut = new OutputArgument();                success = Function.Call<bool>(Hash.FIND_NEXT_PED, handle, nextEntityOut);                ped = nextEntityOut.GetResult<int>();            }            while (success == true);            Function.Call(Hash.END_FIND_PED, handle);
Edited by Aktarus
Link to comment
Share on other sites

Where did you get

Function.Call<int>(Hash.FIND_FIRST_PED, entityOut);

and

Function.Call<bool>(Hash.FIND_NEXT_PED, handle, nextEntityOut);

from?

 

Because as far as I can see, they don't exist on here. http://www.dev-c.com/nativedb/ and if they're not in there, then they don't exist.

 

You can't just create Natives, they have to be valid Native calls and those don't appear to be valid. That means they are probably LUA implementations, which won't work for anything except LUA.

 

If there is nothing that matches, then you are going to have to write the functions that mimic that behaviour, using the Natives that do exist.

Edited by Guest
Link to comment
Share on other sites

@LeeC2202 it actually exist in fivem extra native. Here: https://runtime.fivem.net/doc/reference.html#_0xfb012961

Most of the docuentation are for LUA, that's frustrating as I am trying to code in C#. That is why I am trying to convert the original code.

I understand the principle but it doesn't work.

By the way I have tried some other functions like "GET_PED_AROUND_PEDS" but nothing works.

Edited by Aktarus
Link to comment
Share on other sites

Ah, I apologise for my question then. I didn't realise that FiveM had it's own API extensions. I have only used ScriptHookVDotNet.

 

I have no idea how those FiveM functions work, so I am afraid I can't help you with those.

Link to comment
Share on other sites

Wait, do you use ScriptHookVDotNet (never used FiveM so I'm not sure how its API works)? If so, use World.GetAllPeds. Also, consider using unsafe code instead of output arguments.

Edited by Unknown_Modder
Link to comment
Share on other sites

I have tried :

World.GetAllPeds();

Unfortunately, FiveM has deactivated this function (commented)

here is the file: world.cs

/// Gets an <c>array</c>of all <see cref="Ped"/>s in the World./// </summary>/// <param name="models">The <see cref="Model"/> of <see cref="Ped"/>s to get, leave blank for all <see cref="Ped"/> <see cref="Model"/>s.</param>public static Ped[] GetAllPeds(params Model[] models){	return Array.ConvertAll<int, Ped>(MemoryAccess.GetPedHandles(ModelListToHashList(models)), handle => new Ped(handle));}
Link to comment
Share on other sites

Also, consider using unsafe code instead of output arguments.

 

Sorry, why should I use unsafe code? I don't understand.

Link to comment
Share on other sites

Output arguments are just ugly and require more code.

LOL, that's the worst argument I have seen against using a coding method... it's ugly. You should have seen a straight-lined 6502 smooth-scroll routine on the C64 in assembler if you wanted to see ugly code. :D

 

As for the more code part, I have this to get the ground height, perhaps you could elaborate with an equivalent "unsafe" version that uses less code.

	OutputArgument groundZ = new OutputArgument();	Function.Call(Hash.GET_GROUND_Z_FOR_3D_COORD, position.X, position.Y, position.Z + zoffset, groundZ, 0);	return groundZ.GetResult<float>();
Link to comment
Share on other sites

@LeeC2202, why I am pretty sure you can help me? :lol:

Please just check this:

The problem with these function (FindFirstPed() and FindNextPed() ) is that they return multiple values. In Lua it's easy to grab them:

local handle, ped = FindFirstPed()

the first value is stored in the variable handle and the second in the variable ped.

This code works very well.

But in C#... I am not an expert... I am stuck :miranda:

Link to comment
Share on other sites

The problem is, that FiveM code is just incredibly vague. Where is it collecting the peds based on, i.e. what is the origin point? What radius is it collecting the peds over?

 

I mean the typical SHVDN way would be something like this.

    Ped[] CollectedPeds = World.GetNearbyPeds(Game.Player.Character, 20f);    foreach (Ped collectedPed in CollectedPeds)    {        // Do Code here...    }

That would collect all peds within 20f of the player.

Link to comment
Share on other sites

Yeah, you are right. Fivem is totally confusing, unfortunately...

Thanks anyway to everyone

Edited by Aktarus
Link to comment
Share on other sites

Just a last question: How can I call a function and avoid “Value does not fall within the expected range” error, in a tick?

For example:

public async Task OnTick(){        foreach (Ped ped in new PedsPool())        {            FunctionToApplyToEachPed(ped);        }}FunctionToApplyToEachPed(Ped currentPed){           Function.Call(Hash.SET_PED_COMBAT_ATTRIBUTES, currentPed, 5, true);           Function.Call(Hash.SET_PED_COMBAT_MOVEMENT, currentPed, 3);           Function.Call(Hash.SET_PED_COMBAT_RANGE, currentPed, 2);}

If I put the each "Function.Call" directly in the foreach loop, that works. Like this for example:

public async Task OnTick(){        foreach (Ped ped in new PedsPool())        {            Function.Call(Hash.SET_PED_COMBAT_ATTRIBUTES, ped , 5, true);            Function.Call(Hash.SET_PED_COMBAT_MOVEMENT, ped , 3);            Function.Call(Hash.SET_PED_COMBAT_RANGE, ped , 2);        }}

Thanks for your reply

 

Edited by Aktarus
Link to comment
Share on other sites

I have found the error. It was this function:

void SET_PED_AS_NO_LONGER_NEEDED(Ped* ped);

Everything works when it is commented.

Edited by Aktarus
Link to comment
Share on other sites

Hi Guys,

 

I would like to get the coordinates of the Peds arround me.

Game.Player.Character.Position gives me the coordinates of Player(Me) but i don't know how to get the coordinates for others. :facedesk:

 

Thanks alot and sorry for posting here i don't know if its allowed

Edited by husseinh
Link to comment
Share on other sites

Hi Guys,

 

I would like to get the coordinates of the Peds arround me.

Game.Player.Character.Position gives me the coordinates of Player(Me) but i don't know how to get the coordinates for others. :facedesk:

 

Thanks alot and sorry for posting here i don't know if its allowed

Vector3 Pos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, ped);

Then you can get the coordinates at pos.X, pos.Y and pos.Z

Edited by Aktarus
Link to comment
Share on other sites

 

Hi Guys,

 

I would like to get the coordinates of the Peds arround me.

Game.Player.Character.Position gives me the coordinates of Player(Me) but i don't know how to get the coordinates for others. :facedesk:

 

Thanks alot and sorry for posting here i don't know if its allowed

Vector3 Pos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, ped);

Then you can get the coordinates at pos.X, pos.Y and pos.Z

 

Thank you! Got it:)

Link to comment
Share on other sites

 

 

Hi Guys,

 

I would like to get the coordinates of the Peds arround me.

Game.Player.Character.Position gives me the coordinates of Player(Me) but i don't know how to get the coordinates for others. :facedesk:

 

Thanks alot and sorry for posting here i don't know if its allowed

Vector3 Pos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, ped);

Then you can get the coordinates at pos.X, pos.Y and pos.Z

 

Thank you! Got it:)

 

 

Or there is also:

foreach (Ped ped in peds){     ped.Position}
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
 Share

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