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

[C++] Add spawned vehicles to counter to limit amount spawned


MissingNoGuy55
 Share

Recommended Posts

MissingNoGuy55

I have a script here that spawns vehicles whenever a specific NPC from a gang is killed if an integer reaches an amount.

 

However, my problem is that even after the maximum amount spawn, it still spawns more. I have a feeling I have made an error somewhere, and need someone to point me in the right direction.

 

How can I get this to work? I don't want the function with "maxActiveVehicles < 3" to be called. It seems to be running anyway despite me incrementing it by 1.

 

I also want it to cycle through the vehicles from this method that have been destroyed, and delete them/mark them as no longer needed. It's at the bottom of the code below, called via "else if", and doesn't seem to work at all, and I need help with that too.

 

Any help would be appreciated

if (trafficUpdateTime + 3200 < GetTickCount())    {        trafficUpdateTime = GetTickCount();        if (KKrewAnger >= 800 && maxActiveVehicles < 3)        {            int randNumber = rand() % 3;            DWORD model = KKrewVehicles[randNumber];            Vector3 nodeCoords;            float nodeBearing;            STREAMING::REQUEST_MODEL(model);            while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);            Vector3 pCoords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 15.0, 0.0);            PATHFIND::GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(pCoords.x, pCoords.y, pCoords.z, &nodeCoords, &nodeBearing, 1, 3, 0);            Vehicle veh = VEHICLE::CREATE_VEHICLE(model, nodeCoords.x, nodeCoords.y, nodeCoords.z, nodeBearing, 1, 1);            Ped ped1 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 4, $("g_m_m_korboss_01"), -1, 1, 1);            Ped ped2 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korlieut_01"), 0, 1, 1);            Ped ped3 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_01"), 1, 1, 1);            Ped ped4 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_02"), 2, 1, 1);            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped1, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped2, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped3, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped4, $("AMBIENT_GANG_WEICHENG"));            VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);            VEHICLE::SET_VEHICLE_ENGINE_ON(veh, TRUE, TRUE, FALSE);            VEHICLE::SET_VEHICLE_FORWARD_SPEED(veh, 15.0f);            AI::TASK_VEHICLE_DRIVE_WANDER(ped1, veh, 75.0f, 6);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped1, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped2, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped3, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped4, playerPed);            PED::SET_PED_AS_ENEMY(ped1, TRUE);            PED::SET_PED_AS_ENEMY(ped2, TRUE);            PED::SET_PED_AS_ENEMY(ped3, TRUE);            PED::SET_PED_AS_ENEMY(ped4, TRUE);            //        ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));            //        PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);            WAIT(0);            STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(model);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped1);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped2);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped3);            maxActiveVehicles =+ 1;        }        else if (maxActiveVehicles >= 3)        {            Vehicle vehs[ARR_SIZE];            int vehicleCount = worldGetAllVehicles(vehs, ARR_SIZE);            //                maxActiveVehicles = maxActiveVehicles;            for (int j = 0; j < vehicleCount; j++)            {                Vehicle currentVehicle = vehs[j];                if (VEHICLE::IS_VEHICLE_MODEL(currentVehicle, KKrewVehicles[j]))                {                    if (!VEHICLE::IS_VEHICLE_DRIVEABLE(currentVehicle, FALSE))                    {                        if (!VEHICLE::_IS_VEHICLE_ENGINE_ON(currentVehicle))                        {                            ENTITY::SET_ENTITY_AS_MISSION_ENTITY(currentVehicle, TRUE, TRUE);                            VEHICLE::EXPLODE_VEHICLE(currentVehicle, TRUE, TRUE);                            VEHICLE::DELETE_VEHICLE(&currentVehicle);                            maxActiveVehicles =- 1;                        }                    }                }            }        }    }
Edited by MissingNoGuy55
Link to comment
Share on other sites

MissingNoGuy55

 

 

I'll try that and get back to you. Thanks, by the way.

 

Okay, that worked. Thanks!

 

However, I'm still having problems with cycling through the spawned vehicles to tell which ones are destroyed, so they can be removed to make way for more waves.

 

Here's the snippet. It doesn't work at the moment. "vehicleCount" is also defined but isn't listed here. Help me out with this, please:

if (maxActiveVehicles == 3)            {                for (int j = 0; j < vehicleCount; j++)                {                    Vehicle currentVehicle = vehs[j];                    if (VEHICLE::IS_VEHICLE_MODEL(currentVehicle, KKrewVehicles[j]))                    {                        float vehicleEngineHealth = VEHICLE::GET_VEHICLE_ENGINE_HEALTH(currentVehicle);                        float vehiclePetrolHealth = VEHICLE::GET_VEHICLE_PETROL_TANK_HEALTH(currentVehicle);                        if (vehicleEngineHealth == 0.0f || vehiclePetrolHealth == 0.0f)                        {                            ENTITY::SET_ENTITY_AS_MISSION_ENTITY(currentVehicle, TRUE, TRUE);                            WAIT(0);                            VEHICLE::EXPLODE_VEHICLE(currentVehicle, TRUE, TRUE);                            VEHICLE::DELETE_VEHICLE(&currentVehicle);                            maxActiveVehicles = maxActiveVehicles -= 1;                            char statusText[32];                            sprintf_s(statusText, "%s deleted.", currentVehicle);                            set_status_text(statusText);                        }                    }                }            }
Edited by MissingNoGuy55
Link to comment
Share on other sites

With floats it's usually better to check bounds. <= 0.0f should be safer. I have no idea what KKrewVehicles is.

What did you try yourself?

Link to comment
Share on other sites

MissingNoGuy55

With floats it's usually better to check bounds. <= 0.0f should be safer. I have no idea what KKrewVehicles is.

What did you try yourself?

 

KKrewVehicles is an array containing three vehicle hashes:

const Hash KKrewVehicles[3] ={    0x1F52A43F,        // Moonbeam    0x8CB29A14,        // Rapid GT    0x8E9254FB        // Asterope};

As far as your suggestion, the function stopped working when I used <=, so I went back to ==.

 

The last issue however is that even with the vehicles removed, incrementing it doesn't make the above function call more when it should clearly be below 3. Here's the whole function to reference. Help me out with this too, I may have made a mistake somewhere. It's the last thing I need to get the script fully functional.

if (trafficUpdateTime + 3200 < GetTickCount()) // "trafficUpdateTime" is a DWORD at top of script.    {        trafficUpdateTime = GetTickCount();        maxActiveVehicles = maxActiveVehicles; // Defined at the very top as 0, outside the update() function.        if (maxActiveVehicles < 3)        {            int randNumber = rand() % 3;            DWORD model = KKrewVehicles[randNumber];            Vector3 nodeCoords;            float nodeBearing;            STREAMING::REQUEST_MODEL(model);            while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);            Vector3 pCoords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 15.0, 0.0);            PATHFIND::GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(pCoords.x, pCoords.y, pCoords.z, &nodeCoords, &nodeBearing, 1, 3, 0);            Vehicle veh = VEHICLE::CREATE_VEHICLE(model, nodeCoords.x, nodeCoords.y, nodeCoords.z, nodeBearing, 1, 1);            Ped ped1 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 4, $("g_m_m_korboss_01"), -1, 1, 1);            Ped ped2 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korlieut_01"), 0, 1, 1);            Ped ped3 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_01"), 1, 1, 1);            Ped ped4 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_02"), 2, 1, 1);            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped1, $("AMBIENT_GANG_WEICHENG")); // Using Whorse's custom hash string function.            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped2, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped3, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped4, $("AMBIENT_GANG_WEICHENG"));            VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);            VEHICLE::SET_VEHICLE_ENGINE_ON(veh, TRUE, TRUE, FALSE);            VEHICLE::SET_VEHICLE_FORWARD_SPEED(veh, 15.0f);            AI::TASK_FOLLOW_TO_OFFSET_OF_ENTITY(ped1, playerPed, pCoords.x, pCoords.y, pCoords.z, 35.0f, -1, 10.0f, 1);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped1, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped2, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped3, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped4, playerPed);            PED::SET_PED_AS_ENEMY(ped1, TRUE);            PED::SET_PED_AS_ENEMY(ped2, TRUE);            PED::SET_PED_AS_ENEMY(ped3, TRUE);            PED::SET_PED_AS_ENEMY(ped4, TRUE);            //        ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));            //        PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);            WAIT(0);            STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(model);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped1);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped2);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped3);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped4);            maxActiveVehicles = maxActiveVehicles += 1;        }        if (maxActiveVehicles == 3)        {            for (int j = 0; j < vehicleCount; j++)            {                Vehicle currentVehicle = vehs[j];                if (VEHICLE::IS_VEHICLE_MODEL(currentVehicle, KKrewVehicles[j]))                {                    float vehicleEngineHealth = VEHICLE::GET_VEHICLE_ENGINE_HEALTH(currentVehicle);                    float vehiclePetrolHealth = VEHICLE::GET_VEHICLE_PETROL_TANK_HEALTH(currentVehicle);                    if (vehicleEngineHealth == 0.0f || vehiclePetrolHealth == 0.0f)                    {                        ENTITY::SET_ENTITY_AS_MISSION_ENTITY(currentVehicle, TRUE, TRUE);                        WAIT(0);                        VEHICLE::EXPLODE_VEHICLE(currentVehicle, TRUE, TRUE);                        VEHICLE::DELETE_VEHICLE(&currentVehicle);                        maxActiveVehicles = maxActiveVehicles - 1;                    }                }            }        }    }
Edited by MissingNoGuy55
Link to comment
Share on other sites

1. int vehicleCount = worldGetAllVehicles(vehs, ARR_SIZE);

2. for (int j = 0; j < vehicleCount; j++)

3. if (VEHICLE::IS_VEHICLE_MODEL(currentVehicle, KKrewVehicles[j]))

 

Should this not crash?

 

Less-or-equal should give more "matches" than equal.

Link to comment
Share on other sites

MissingNoGuy55

1. int vehicleCount = worldGetAllVehicles(vehs, ARR_SIZE);

2. for (int j = 0; j < vehicleCount; j++)

3. if (VEHICLE::IS_VEHICLE_MODEL(currentVehicle, KKrewVehicles[j]))

 

Should this not crash?

 

Less-or-equal should give more "matches" than equal.

So what you're asking me to do is to change "j < vehicleCount" to "j <= vehicleCount"?

 

Or add "int vehicleCount = worldGetAllVehicles(vehs, ARR_SIZE);" above "for (int j = 0; j < vehicleCount; j++)"?

Link to comment
Share on other sites

No, I'm just seeing you're trying to access KKrewVehicles with an index that's bigger than 2 here, assuming you've called worldGetAllVehicles somewhere else.

 

What are you even trying to do? Seems like you want to loop through all vehicles, check if the model is in your list of models and then delete it when it's broken. Why not just keep the vehicle handle and remove it with that later? No need to check all vehicles.

 

Also I referred to <= 0.0f instead of == 0.0f. FYI, the health values can be negative.

Link to comment
Share on other sites

MissingNoGuy55

No, I'm just seeing you're trying to access KKrewVehicles with an index that's bigger than 2 here, assuming you've called worldGetAllVehicles somewhere else.

 

What are you even trying to do? Seems like you want to loop through all vehicles, check if the model is in your list of models and then delete it when it's broken. Why not just keep the vehicle handle and remove it with that later? No need to check all vehicles.

 

Also I referred to <= 0.0f instead of == 0.0f. FYI, the health values can be negative.

 

Yeah, I called it near the top of the script. You're also right in that I was trying to loop through all the vehicles and check the models for broken ones. But your idea of just keeping the handle and using that instead sounds like a better idea. I'll get back to you once I try it out, which shouldn't be too long.

Link to comment
Share on other sites

MissingNoGuy55

Okay, so it still doesn't seem to work. Maybe I applied it wrong. The video should speak for itself why it isn't working.

 

 

 

When I destroy the vehicles, it's supposed to spawn more as I subtract maxActiveVehicles, but it doesn't work. Here's the updated code. "KKrewAnger" is also just an int applied at the top that works as intended, so don't worry about that:

if (trafficUpdateTime + 3200 < GetTickCount())    {        trafficUpdateTime = GetTickCount();        maxActiveVehicles = maxActiveVehicles; // Defined at the very top, outside the update() function.        if (maxActiveVehicles < 3 && KKrewAnger >= 800)        {            int randNumber = rand() % 3;            DWORD model = KKrewVehicles[randNumber];            Vector3 nodeCoords;            float nodeBearing;            STREAMING::REQUEST_MODEL(model);            while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);            Vector3 pCoords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 15.0, 0.0);            PATHFIND::GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(pCoords.x, pCoords.y, pCoords.z, &nodeCoords, &nodeBearing, 1, 3, 0);            Vehicle veh = VEHICLE::CREATE_VEHICLE(model, nodeCoords.x, nodeCoords.y, nodeCoords.z, nodeBearing, 1, 1);            Ped ped1 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 4, $("g_m_m_korboss_01"), -1, 1, 1);            Ped ped2 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korlieut_01"), 0, 1, 1);            Ped ped3 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_01"), 1, 1, 1);            Ped ped4 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_02"), 2, 1, 1);            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped1, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped2, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped3, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped4, $("AMBIENT_GANG_WEICHENG"));            VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);            VEHICLE::SET_VEHICLE_ENGINE_ON(veh, TRUE, TRUE, FALSE);            VEHICLE::SET_VEHICLE_FORWARD_SPEED(veh, 15.0f);            AI::TASK_FOLLOW_TO_OFFSET_OF_ENTITY(ped1, playerPed, pCoords.x, pCoords.y, pCoords.z, 35.0f, -1, 10.0f, 1);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped1, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped2, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped3, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped4, playerPed);            PED::SET_PED_AS_ENEMY(ped1, TRUE);            PED::SET_PED_AS_ENEMY(ped2, TRUE);            PED::SET_PED_AS_ENEMY(ped3, TRUE);            PED::SET_PED_AS_ENEMY(ped4, TRUE);            //        ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));            //        PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);            WAIT(0);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped1);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped2);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped3);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped4);            maxActiveVehicles = maxActiveVehicles += 1;            if (maxActiveVehicles == 3)            {                float vehicleEngineHealth = VEHICLE::GET_VEHICLE_ENGINE_HEALTH(veh);                float vehiclePetrolHealth = VEHICLE::GET_VEHICLE_PETROL_TANK_HEALTH(veh);                if (vehicleEngineHealth <= 0.0f || vehiclePetrolHealth <= 0.0f)                {                    STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(model);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh);                    ENTITY::SET_ENTITY_AS_MISSION_ENTITY(veh, TRUE, TRUE);                    WAIT(0);                    VEHICLE::EXPLODE_VEHICLE(veh, TRUE, TRUE);                    VEHICLE::DELETE_VEHICLE(&veh);                    maxActiveVehicles = maxActiveVehicles -= 1;                }            }        }    }
Edited by MissingNoGuy55
Link to comment
Share on other sites

 if (vehicleEngineHealth <= 0.0f || vehiclePetrolHealth <= 0.0f)

 

Why don't you use ENTITY::IS_ENTITY_DEAD(yourVehicle)?

Using classes would have also made your code more readable and understandable even for yourself.

Link to comment
Share on other sites

MissingNoGuy55

 

 if (vehicleEngineHealth <= 0.0f || vehiclePetrolHealth <= 0.0f)

 

Why don't you use ENTITY::IS_ENTITY_DEAD(yourVehicle)?

Using classes would have also made your code more readable and understandable even for yourself.

 

 

Tried that, and it still doesn't work

Edited by MissingNoGuy55
Link to comment
Share on other sites

MissingNoGuy55

I'm not giving up though.

 

When it did work, it would only delete one vehicle. Either way, it just doesn't work as intended. No matter how many times I try to finesse it to work, it just refuses to increment downwards on destroying, the vehicle models won't disappear, and it just won't spawn more in its place because the increment doesn't work for whatever reason.

 

Would ENTITY::SET_VEHICLE_AS_NO_LONGER_NEEDED have something to do with it? Tempted to just rewrite the whole thing.

Edited by MissingNoGuy55
Link to comment
Share on other sites

Must be something with your vehicle array. Try using std::vector<Vehicle> to store your own created vehicles and keep an eye on the vehicle count with yourVector.size();

Edited by ins1de
Link to comment
Share on other sites

 

Okay, so it still doesn't seem to work. Maybe I applied it wrong. The video should speak for itself why it isn't working.

 

 

 

When I destroy the vehicles, it's supposed to spawn more as I subtract maxActiveVehicles, but it doesn't work. Here's the updated code. "KKrewAnger" is also just an int applied at the top that works as intended, so don't worry about that:

if (trafficUpdateTime + 3200 < GetTickCount())    {        trafficUpdateTime = GetTickCount();        maxActiveVehicles = maxActiveVehicles; // Defined at the very top, outside the update() function.        if (maxActiveVehicles < 3 && KKrewAnger >= 800)        {            int randNumber = rand() % 3;            DWORD model = KKrewVehicles[randNumber];            Vector3 nodeCoords;            float nodeBearing;            STREAMING::REQUEST_MODEL(model);            while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);            Vector3 pCoords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 15.0, 0.0);            PATHFIND::GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(pCoords.x, pCoords.y, pCoords.z, &nodeCoords, &nodeBearing, 1, 3, 0);            Vehicle veh = VEHICLE::CREATE_VEHICLE(model, nodeCoords.x, nodeCoords.y, nodeCoords.z, nodeBearing, 1, 1);            Ped ped1 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 4, $("g_m_m_korboss_01"), -1, 1, 1);            Ped ped2 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korlieut_01"), 0, 1, 1);            Ped ped3 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_01"), 1, 1, 1);            Ped ped4 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_02"), 2, 1, 1);            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped1, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped2, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped3, $("AMBIENT_GANG_WEICHENG"));            PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped4, $("AMBIENT_GANG_WEICHENG"));            VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);            VEHICLE::SET_VEHICLE_ENGINE_ON(veh, TRUE, TRUE, FALSE);            VEHICLE::SET_VEHICLE_FORWARD_SPEED(veh, 15.0f);            AI::TASK_FOLLOW_TO_OFFSET_OF_ENTITY(ped1, playerPed, pCoords.x, pCoords.y, pCoords.z, 35.0f, -1, 10.0f, 1);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped1, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped2, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped3, playerPed);            AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped4, playerPed);            PED::SET_PED_AS_ENEMY(ped1, TRUE);            PED::SET_PED_AS_ENEMY(ped2, TRUE);            PED::SET_PED_AS_ENEMY(ped3, TRUE);            PED::SET_PED_AS_ENEMY(ped4, TRUE);            //        ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));            //        PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);            WAIT(0);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped1);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped2);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped3);            ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped4);            maxActiveVehicles = maxActiveVehicles += 1;            if (maxActiveVehicles == 3)            {                float vehicleEngineHealth = VEHICLE::GET_VEHICLE_ENGINE_HEALTH(veh);                float vehiclePetrolHealth = VEHICLE::GET_VEHICLE_PETROL_TANK_HEALTH(veh);                if (vehicleEngineHealth <= 0.0f || vehiclePetrolHealth <= 0.0f)                {                    STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(model);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh);                    ENTITY::SET_ENTITY_AS_MISSION_ENTITY(veh, TRUE, TRUE);                    WAIT(0);                    VEHICLE::EXPLODE_VEHICLE(veh, TRUE, TRUE);                    VEHICLE::DELETE_VEHICLE(&veh);                    maxActiveVehicles = maxActiveVehicles -= 1;                }            }        }    }

 

You are not checking if the vehicle is destroyed so maxActiveVehicles will always stay at 3.

 

Your if statement:

if (maxActiveVehicles == 3)

is inside:

if (maxActiveVehicles < 3)

so once maxActiveVehicles becomes 3 it will never reach that last if statement. You should place the if under the other if statement.

 

 

Also:

ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh);ENTITY::SET_ENTITY_AS_MISSION_ENTITY(veh, TRUE, TRUE);

I don't know why you are doing this but you are saying you don't need the entity anymore (so you mark it as disposable for the engine) and after that you mark it as persistent again with SET_ENTITY_AS_MISSION_ENTITY.

Link to comment
Share on other sites

MissingNoGuy55

You are not checking if the vehicle is destroyed so maxActiveVehicles will always stay at 3.

 

Your if statement:

if (maxActiveVehicles == 3)

is inside:

if (maxActiveVehicles < 3)

so once maxActiveVehicles becomes 3 it will never reach that last if statement. You should place the if under the other if statement.

 

 

Also:

ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh);ENTITY::SET_ENTITY_AS_MISSION_ENTITY(veh, TRUE, TRUE);

I don't know why you are doing this but you are saying you don't need the entity anymore (so you mark it as disposable for the engine) and after that you mark it as persistent again with SET_ENTITY_AS_MISSION_ENTITY.

 

 

I will try that and get back to you. I'll also not mark the vehicle as no longer needed, as that was probably an oversight on my part. I assumed the handle wasn't removed if I marked it as no longer needed.

 

Thanks!

Edited by MissingNoGuy55
Link to comment
Share on other sites

MissingNoGuy55

So I tried your suggestion and while it probably works, there's no way I can actually check the vehicle outside of the if statement without cycling through all the vehicles in the world, as "veh" is stated to be undefined.

 

This is what I got so far. Remember that "KKrewVehicles" is just an array consisting of three model hashes.

if (maxActiveVehicles < 3)            {                int randNumber = rand() % 3;                DWORD model = KKrewVehicles[randNumber];                Vector3 nodeCoords;                float nodeBearing;                STREAMING::REQUEST_MODEL(model);                while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);                Vector3 pCoords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 5.0, 5.0, 0.0);                PATHFIND::GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(pCoords.x, pCoords.y, pCoords.z, &nodeCoords, &nodeBearing, 1, 3, 0);                Vehicle veh = VEHICLE::CREATE_VEHICLE(model, nodeCoords.x, nodeCoords.y, nodeCoords.z, nodeBearing, 1, 1);                if (maxActiveHelis == 0)                {                    DWORD model = 0x2C634FBD;                    STREAMING::REQUEST_MODEL(model);                    while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);                    Vector3 heliCoords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 25.0, 5.0, 45.0);                    Vehicle veh2 = VEHICLE::CREATE_VEHICLE(model, heliCoords.x, heliCoords.y, heliCoords.z, ENTITY::GET_ENTITY_HEADING(playerPed), 1, 1);                    Ped heliped1 = PED::CREATE_PED_INSIDE_VEHICLE(veh2, 4, $("g_m_m_korboss_01"), -1, 1, 1);                    Ped heliped2 = PED::CREATE_PED_INSIDE_VEHICLE(veh2, 5, $("g_m_y_korlieut_01"), 0, 1, 1);                    Ped heliped3 = PED::CREATE_PED_INSIDE_VEHICLE(veh2, 5, $("g_m_y_korean_01"), 1, 1, 1);                    Ped heliped4 = PED::CREATE_PED_INSIDE_VEHICLE(veh2, 5, $("g_m_y_korean_02"), 2, 1, 1);                    AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(heliped1, playerPed);                    //        AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(heliped2, playerPed);                    //        AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(heliped3, playerPed);                    //        AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(heliped4, playerPed);                    VEHICLE::SET_HELI_BLADES_FULL_SPEED(veh2);                    PED::SET_PED_RELATIONSHIP_GROUP_HASH(heliped1, $("AMBIENT_GANG_WEICHENG"));                    PED::SET_PED_RELATIONSHIP_GROUP_HASH(heliped2, $("AMBIENT_GANG_WEICHENG"));                    PED::SET_PED_RELATIONSHIP_GROUP_HASH(heliped3, $("AMBIENT_GANG_WEICHENG"));                    PED::SET_PED_RELATIONSHIP_GROUP_HASH(heliped4, $("AMBIENT_GANG_WEICHENG"));                    AI::TASK_HELI_CHASE(heliped1, playerPed, 0.0, 15.0, 15.0);                    //                AI::TASK_FOLLOW_TO_OFFSET_OF_ENTITY(heliped1, playerPed, pCoords.x, pCoords.y, pCoords.z, 50.0f, -1, 10.0f, 1);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&heliped1);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&heliped2);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&heliped3);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&heliped4);                    ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&veh2);                    maxActiveHelis = maxActiveHelis += 1;                }                Ped ped1 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 4, $("g_m_m_korboss_01"), -1, 1, 1);                Ped ped2 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korlieut_01"), 0, 1, 1);                Ped ped3 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_01"), 1, 1, 1);                Ped ped4 = PED::CREATE_PED_INSIDE_VEHICLE(veh, 5, $("g_m_y_korean_02"), 2, 1, 1);                PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped1, $("AMBIENT_GANG_WEICHENG"));                PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped2, $("AMBIENT_GANG_WEICHENG"));                PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped3, $("AMBIENT_GANG_WEICHENG"));                PED::SET_PED_RELATIONSHIP_GROUP_HASH(ped4, $("AMBIENT_GANG_WEICHENG"));                VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);                VEHICLE::SET_VEHICLE_ENGINE_ON(veh, TRUE, TRUE, FALSE);                VEHICLE::SET_VEHICLE_FORWARD_SPEED(veh, 15.0f);                AI::TASK_FOLLOW_TO_OFFSET_OF_ENTITY(ped1, playerPed, pCoords.x, pCoords.y, pCoords.z, 35.0f, -1, 10.0f, 1);                AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped1, playerPed);                AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped2, playerPed);                AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped3, playerPed);                AI::ADD_VEHICLE_SUBTASK_ATTACK_PED(ped4, playerPed);                PED::SET_PED_AS_ENEMY(ped1, TRUE);                PED::SET_PED_AS_ENEMY(ped2, TRUE);                PED::SET_PED_AS_ENEMY(ped3, TRUE);                PED::SET_PED_AS_ENEMY(ped4, TRUE);                WAIT(0);                ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped1);                ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped2);                ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped3);                ENTITY::SET_ENTITY_AS_NO_LONGER_NEEDED(&ped4);                STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(model);                maxActiveVehicles = maxActiveVehicles += 1;            }            if (maxActiveVehicles == 3)            {                Vehicle destroyed[ARR_SIZE];                int destroyedCount = worldGetAllVehicles(destroyed, ARR_SIZE);                for (int j = 0; j < destroyedCount; j++)                {                    if (VEHICLE::IS_VEHICLE_MODEL(destroyed[j], KKrewVehicles[j]) && ENTITY::IS_ENTITY_DEAD(destroyed[j]))                    {                        Hash dModels = ENTITY::GET_ENTITY_MODEL(destroyed[j]);                        ENTITY::SET_VEHICLE_AS_NO_LONGER_NEEDED(destroyed);                        maxActiveVehicles = maxActiveVehicles -= 1;                    }                }            }
Edited by MissingNoGuy55
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.