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

    3. Suggestions

[C++] Help me understand the <Ped> vector


whorse
 Share

Recommended Posts

Hello, I am having some trouble with my first ASI mod, Melee Riot. I am very new to C++, and I began my mod by editing the source code for Kokolaty's "Provocateur" mod. However, after all this time developing it, I am still not clear on how the <Ped> array works, and Kokolaty is MIA. The script declares a variable called

std::vector <Ped> peds;

Then, it goes into an infinite while loop and "purges" the peds for every iteration:

while (true){	//-----------------------------------	//Purge "peds"	for (int i = 0; i < peds.size(); i++)	{		if (!ENTITY::DOES_ENTITY_EXIST(peds.at(i))) {                         peds.erase(peds.begin() + i); break; }		else			if (canPedRiot(peds.at(i))) { ENTITY::SET_PED_AS_NO_LONGER_NEEDED(&peds.at(i));                                     peds.erase(peds.begin() + i); break; }			else				if (distanceBetween(player, peds.at(i)) > 600)  {                                     ENTITY::SET_PED_AS_NO_LONGER_NEEDED(&peds.at(i));                                     peds.erase(peds.begin() + i); break; }				else					if (PED::_IS_PED_DEAD(peds.at(i), true)) {                                                 peds.erase(peds.begin() + i); break; }					else{                                             AI::TASK (do something)                                            }			}

After that, the mod checks to see if the hotkey has been activated and does all this stuff to make them fight every x tics.

 

What does it mean to set entities as no longer needed? That they will be deleted like normal when you get too far away or they die? Or that the script stops using them? I'm guessing I have to use peds.erase() on the valid rioters with every iteration because otherwise "peds" will grow infinitely? How can I have my mod just completely ignore one class of ped? By that I mean cops, in particular (I already excluded them in the canPedRiot method).

 

If I do not use the "else" statement at the bottom of the above code, cops who stand around idle after I lose them will drop my frame rate by about 2-3 FPS per cop, even after I leave them behind and they disappear (then I have to load a new game to fix it). If I give them some kind of task inside the else statement, they will follow it and the lag will go away (if the task can hold their attention.) But this screws up their normal behavior, and lag still happens sometimes.

In Provocateur, the else statement had the same targeting and combat instructions as the main "active" part of the mod, and yet no peds ever followed the combat commands when the mod was disabled (even though the "purge" section of the mod doesn't check to see if the mod is enabled or not). The PED::IS_PED_HUMAN native was used instead of my canPedRiot() method, though, so maybe it's just nothing valid was getting there. How can I just have the cops return to their default behavior while preventing any FPS lag? Is there any way to just have the mod completely not touch them at all? I notice that, if the mod is on, cops will frequently flee like scared civilians no matter what I try, even though they are excluded. Is it because of the way "peds" is handled?

 

Lastly, what exactly does the command below do? Without it being at the end of all the fight commands, none of the peds will do anything. Here:

peds.push_back();
Edited by whorse
Link to comment
Share on other sites

neeevermind, I think I got it. I had to say

else{ENTITY::SET_PED_AS_NO_LONGER_NEEDED(&peds.at(i)); peds.erase(peds.begin() + i);}

with no "break;"

That seems to make it so they don't cause huge lag when player's wanted level is lost (why did it lag in the first place?). I've wrongly thought to have fixed this problem several times in the past, though, so don't be surprised if I come back.

 

I still would appreciate if anyone answered any of my questions about how <Ped> works, though

 

EDIT: Oops! I was tired when I made this thread, I meant to put it in the "coding" section. If a mod could move it there I would appreciate it

Edited by whorse
Link to comment
Share on other sites

ENTITY::SET_PED_AS_NO_LONGER_NEEDED does indeed remove an entity once it is out of range of the player.

 

peds.push_back(); "push_back()" is a vector command that adds an item to the vector (array).

in this case it seems tho you are adding an empty space to the array as it would usually look something like

Ped ped = (put code to create or detect a ped here)peds.push_back(ped);

This increases the size of the vector peds but since the loop removes any empty slots in the vector by detecting no entity it would correct its self but seems pointless.

 

The following function could help you filter out cops but you could do it other ways.

bool isPedACop(Ped ped){return (PED::GET_PED_RELATIONSHIP_GROUP_HASH(ped) == GAMEPLAY::GET_HASH_KEY("cop"));}
Edited by C06alt
Link to comment
Share on other sites

 

ENTITY::SET_PED_AS_NO_LONGER_NEEDED does indeed remove an entity once it is out of range of the player.

 

peds.push_back(); "push_back()" is a vector command that adds an item to the vector (array).

in this case it seems tho you are adding an empty space to the array as it would usually look something like

Ped ped = (put code to create or detect a ped here)peds.push_back(ped);

This increases the size of the vector peds but since the loop removes any empty slots in the vector by detecting no entity it would correct its self but seems pointless.

 

The following function could help you filter out cops but you could do it other ways.

bool isPedACop(Ped ped){return (PED::GET_PED_RELATIONSHIP_GROUP_HASH(ped) == GAMEPLAY::GET_HASH_KEY("cop"));}

Thank you very much for the explanation! My mistake, in the OP it actually it says "peds.push_back(ped1);" and in the every-x-tics part of the mod, one of the conditions required to run the for-loop that issues all the fight-commands is that peds.size() must be greater than 1; I suppose that is maybe why I need to use peds.push_back(ped1)?

 

Right now I am still having a problem with the cops, though not nearly as bad. I already have excluded them using a similar method to what you posted (as part of the canPedRiot() method, where I check to see if their "ped type" is 6). But still, when I have a wanted level and the cops are searching for me very far away (but still blipped on the map), my frame rate will start chugging to like 1/4th normal speed until I lose my wanted level. Why is this? Every single entity except for dead people and non-existant ones be getting marked as no longer needed, and everything in peds.size() gets deleted from the "peds" vector between every triggering of the mod. Should I manually use the "delete entity" native on distant cops, maybe using an if-statement nested inside the "distanceBetween() > 600" part of the purge?

 

Also, I have another question (for anyone who can answer). Below is a simplified version of how the mod works:

 

 

while (true){	//(*purge peds section here*)	//(*check hotkey here, assign "active"*)	tic++;	if ((tic >= 75) && (active == true))	{		tic = 0;                const int numElements = 100;		const int arrSize = (numElements+1) * sizeof(Any);		Object nearbyPeds[arrSize];		nearbyPeds[0] = numElements;											int numRioters = PED::GET_PED_NEARBY_PEDS(player, nearbyPeds, -1);		if (nearbyPeds != NULL)		{			for (int i = 0; i < numRioters; i++)			{				int offsettedID = i * sizeof(Any) + 2;				ped1 = nearbyPeds[offsettedID];				if (peds.size() > 1)					{					//(*mod body here, instructions for ped1*)					}                        peds.push_back(ped1);			}		}	}WAIT(0);}

 

What is the reason for the arrSize and numElements values? Like, what happens when I change them? Could somebody break down to me how the whole for-statement cycling once for each of nearby ped works, and how the script knows to command only these nearby peds, even though GET_NEARBY_PEDS just returns an int? And finally, why do I need an "offsettedID" and why does it need to be "i" (the nearby ped's order in the list) times sizeof(Any) + 2 ?

 

All this stuff I'm asking about was written by Kokolaty, and I've basically left it all alone since I started the mod

Edited by whorse
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.