Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. Red Dead Redemption 2

      1. PC
      2. Gameplay
      3. Missions
      4. Help & Support
    2. Red Dead Online

      1. Gameplay
      2. Find Lobbies & Outlaws
      3. Help & Support
      4. Frontier Pursuits
    1. Crews & Posses

      1. Recruitment
    2. Events

    1. GTA Online

      1. Diamond Casino & Resort
      2. DLC
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Grand Theft Auto Series

    3. GTA 6

    4. GTA V

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

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

    7. GTA Vice City Stories

    8. GTA Liberty City Stories

    9. GTA San Andreas

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

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

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

      1. GTA Advance
      2. GTA 2
      3. GTA
    13. 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. 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. News

    2. Forum Support

    3. Site Suggestions

Sign in to follow this  
whorse

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

Recommended Posts

whorse

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

Share this post


Link to post
Share on other sites
whorse

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

Share this post


Link to post
Share on other sites
C06alt

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

Share this post


Link to post
Share on other sites
whorse

 

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

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