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

Happy Holidays from the GTANet team!

[C++] Preventing vehicles from disappearing


++tachyon
 Share

Recommended Posts

hello,

I am trying to make a script for the game that will store the current vehicle you are in and prevent it from disappearing with numpad +.

So far I have successfully stored spawned peds/bodyguards and vehicles with a std::vector of type Ped and Vehicle respectively.

When I try to save the current vehicle the player is in with the key numpad + it wont stop the vehicle from disappearing.

Its all using natives.h and the native trainer from Alexander Blade.

//global variablesstd::vector<Vehicle> Veh_Vector;char veh_title[10]{};Ped ped_value = PLAYER::PLAYER_PED_ID();if (IsKeyJustUp(0x6B))			//numpad +{    if (ENTITY::DOES_ENTITY_EXIST(ped_value) && PED::IS_PED_IN_ANY_VEHICLE(ped_value, 0))    //check if player exists and if he/she is in a vehicle    {        size_t count{};	Vehicle v_vehicle = PED::GET_VEHICLE_PED_IS_IN(ped_value, false);      //get the vehicle the player is currently in	if (Veh_Vector.size() >= 1)                            // if false the script doesn't need to check if this vehicle has already been saved        {        	for (size_t i{}; i < Veh_Vector.size(); i++)    //check to see if the vehicle has already been saved		{			if (v_vehicle != Veh_Vector.at(i))      //... if it hasn't been saved increase count			{				++count;			}		}		if (count == Veh_Vector.size())               //... if count is the same size as the vector add the new vehicle to the vector		{	        	Veh_Vector.push_back(v_vehicle);       //push_back and add it to the vector			sprintf_s(veh_title, "%i saved", Veh_Vector.size());			set_status_text(veh_title);		}		else		{			set_status_text("Already saved");		}	}	else	{		Veh_Vector.push_back(v_vehicle);		sprintf_s(veh_title, "%i saved", Veh_Vector.size());		set_status_text(veh_title);	}    }    else    {	set_status_text("please enter a vehicle");    }}

For some reason when spawning a new vehicle/ped it works just fine but when adding vehicles that already exist they keep disappearing as usual.

Help would be very appreciated!

Edited by ++tachyon
Link to comment
Share on other sites

Not sure I get what you're doing there mate. I don't see anything to tell the game to prevent the vehicle from disappearing (or if it's even possible). You might need to store the vehicle details and the coordinates then respawn it when the player is nearby. The game most likely would destroy the vehicle object once you leave the area/get in another vehicle rendering the Vehicle handle(s) you've saved invalid. Simply storing it in the vector doesn't tell the game to leave it there. Maybe I'm not quite understanding your aim/method... :)

 

Also, you should (as good programming practice) specifically initialise your size_t's to 0 (i.e. size_t count = 0; / size_t i = 0;). The way you have it, they could be initialised to any random value, depending on what was in that memory address beforehand.

Edited by outlier
Link to comment
Share on other sites

At first I didn't understand what is wrong (that your code deleting in vector existing vehicles or that game deletes your vehicles)

You just can set entity as mission entity, and it won't be deleted because of obvious reasons.

 

All the best,

Paul.

Edited by leftas
Link to comment
Share on other sites

Not sure I get what you're doing there mate. I don't see anything to tell the game to prevent the vehicle from disappearing (or if it's even possible). You might need to store the vehicle details and the coordinates then respawn it when the player is nearby. The game most likely would destroy the vehicle object once you leave the area/get in another vehicle rendering the Vehicle handle(s) you've saved invalid. Simply storing it in the vector doesn't tell the game to leave it there. Maybe I'm not quite understanding your aim/method... :)

 

Also, you should (as good programming practice) specifically initialise your size_t's to 0 (i.e. size_t count = 0; / size_t i = 0;). The way you have it, they could be initialised to any random value, depending on what was in that memory address beforehand.

 

All variables are initialized to 0 with the empty initializer list {} (C++14), but thanks for your comment!

 

The thing is when I spawn a vehicle (with the native trainer) and add it to the vector then customize it in LS, go to mt chilliad and back, it's still there with all the modifications from LS customs.

Here's the code that works:

		if (bSelect)		{			menu_beep();			LPCSTR modelName = vehicleModels[carspawnActiveLineIndex][carspawnActiveItemIndex];			DWORD model = GAMEPLAY::GET_HASH_KEY((char *)modelName);			if (STREAMING::IS_MODEL_IN_CDIMAGE(model) && STREAMING::IS_MODEL_A_VEHICLE(model))			{				STREAMING::REQUEST_MODEL(model);								while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);				Vector3 coords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 5.0, 0.0);				Vehicle veh = VEHICLE::CREATE_VEHICLE(model, coords.x, coords.y, coords.z, 0.0, 1, 1);				VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);				if (featureVehWrapInSpawned)				{					ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));					PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);				}				Veh_Vector.push_back(veh);                     //saving it to a std::vector				WAIT(0);				char statusText[32];				sprintf_s(statusText, "%s spawned", modelName);				set_status_text(statusText);				return true;			}
Edited by ++tachyon
Link to comment
Share on other sites

 

Not sure I get what you're doing there mate. I don't see anything to tell the game to prevent the vehicle from disappearing (or if it's even possible). You might need to store the vehicle details and the coordinates then respawn it when the player is nearby. The game most likely would destroy the vehicle object once you leave the area/get in another vehicle rendering the Vehicle handle(s) you've saved invalid. Simply storing it in the vector doesn't tell the game to leave it there. Maybe I'm not quite understanding your aim/method... :)

 

Also, you should (as good programming practice) specifically initialise your size_t's to 0 (i.e. size_t count = 0; / size_t i = 0;). The way you have it, they could be initialised to any random value, depending on what was in that memory address beforehand.

 

All variables are initialized to 0 with the empty initializer list {} (C++14), but thanks for your comment!

 

The thing is when I spawn a vehicle (with the native trainer) and add it to the vector then customize it in LS, go to mt chilliad and back, it's still there with all the modifications from LS customs.

Here's the code that works:

		if (bSelect)		{			menu_beep();			LPCSTR modelName = vehicleModels[carspawnActiveLineIndex][carspawnActiveItemIndex];			DWORD model = GAMEPLAY::GET_HASH_KEY((char *)modelName);			if (STREAMING::IS_MODEL_IN_CDIMAGE(model) && STREAMING::IS_MODEL_A_VEHICLE(model))			{				STREAMING::REQUEST_MODEL(model);								while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);				Vector3 coords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 5.0, 0.0);				Vehicle veh = VEHICLE::CREATE_VEHICLE(model, coords.x, coords.y, coords.z, 0.0, 1, 1);				VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);				if (featureVehWrapInSpawned)				{					ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));					PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);				}				Veh_Vector.push_back(veh);                     //saving it to a std::vector				WAIT(0);				char statusText[32];				sprintf_s(statusText, "%s spawned", modelName);				set_status_text(statusText);				return true;			}

What happens if you do that with 2 vehicles? Do both stay? It may be the game itself saving your last used vehicle like it has done since GTA III (as long as you don't enter another vehicle). Try lefta's suggestion of setting the vehicles as mission entities. How you do that I have no idea, sorry :)

Link to comment
Share on other sites

What happens if you do that with 2 vehicles? Do both stay? It may be the game itself saving your last used vehicle like it has done since GTA III (as long as you don't enter another vehicle). Try lefta's suggestion of setting the vehicles as mission entities. How you do that I have no idea, sorry :)

 

 

 

 

I actually tested it with 20+ vehicles and i teleported to the other side of the map, then I drove back and they were as I left them, just with the occasional miss alignment that an AI crashed into the vehicle :lol:

 

 

 

 

At first I didn't understand what is wrong (that your code deleting in vector existing vehicles or that game deletes your vehicles)

You just can set entity as mission entity, and it won't be deleted because of obvious reasons.

 

All the best,

Paul.

 

 

This works perfect! Thank you for your help!

ENTITY::SET_ENTITY_AS_MISSION_ENTITY(v_vehicle, true, true);
Link to comment
Share on other sites

All variables are initialized to 0 with the empty initializer list {} (C++14), but thanks for your comment!

Like I said, it's good programming practice to initialise to 0. If you compile with -pedantic, the compiler will tell you that the standard forbids empty initializer braces :)
Link to comment
Share on other sites

 

What happens if you do that with 2 vehicles? Do both stay? It may be the game itself saving your last used vehicle like it has done since GTA III (as long as you don't enter another vehicle). Try lefta's suggestion of setting the vehicles as mission entities. How you do that I have no idea, sorry :)

 

 

I actually tested it with 20+ vehicles and i teleported to the other side of the map, then I drove back and they were as I left them, just with the occasional miss alignment that an AI crashed into the vehicle :lol:

 

 

 

 

At first I didn't understand what is wrong (that your code deleting in vector existing vehicles or that game deletes your vehicles)

You just can set entity as mission entity, and it won't be deleted because of obvious reasons.

 

All the best,

Paul.

 

This works perfect! Thank you for your help!

 

ENTITY::SET_ENTITY_AS_MISSION_ENTITY(v_vehicle, true, true);

 

Glad you got it working! Whatever was causing them to stick, it was not storing them in a vector. Possibly the method of spawn, not sure. Interesting nonetheless :)

Link to comment
Share on other sites

Thanks for your help!

 

I uploaded the script on gtainside.com

 

Enjoy!

Oh wow, thanks! I guess.

by the way, nice script.

 

All the best,

Paul.

Link to comment
Share on other sites

 

Not sure I get what you're doing there mate. I don't see anything to tell the game to prevent the vehicle from disappearing (or if it's even possible). You might need to store the vehicle details and the coordinates then respawn it when the player is nearby. The game most likely would destroy the vehicle object once you leave the area/get in another vehicle rendering the Vehicle handle(s) you've saved invalid. Simply storing it in the vector doesn't tell the game to leave it there. Maybe I'm not quite understanding your aim/method... :)

 

Also, you should (as good programming practice) specifically initialise your size_t's to 0 (i.e. size_t count = 0; / size_t i = 0;). The way you have it, they could be initialised to any random value, depending on what was in that memory address beforehand.

All variables are initialized to 0 with the empty initializer list {} (C++14), but thanks for your comment!

 

The thing is when I spawn a vehicle (with the native trainer) and add it to the vector then customize it in LS, go to mt chilliad and back, it's still there with all the modifications from LS customs.

Here's the code that works:

		if (bSelect)		{			menu_beep();			LPCSTR modelName = vehicleModels[carspawnActiveLineIndex][carspawnActiveItemIndex];			DWORD model = GAMEPLAY::GET_HASH_KEY((char *)modelName);			if (STREAMING::IS_MODEL_IN_CDIMAGE(model) && STREAMING::IS_MODEL_A_VEHICLE(model))			{				STREAMING::REQUEST_MODEL(model);								while (!STREAMING::HAS_MODEL_LOADED(model)) WAIT(0);				Vector3 coords = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(PLAYER::PLAYER_PED_ID(), 0.0, 5.0, 0.0);				Vehicle veh = VEHICLE::CREATE_VEHICLE(model, coords.x, coords.y, coords.z, 0.0, 1, 1);				VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(veh);				if (featureVehWrapInSpawned)				{					ENTITY::SET_ENTITY_HEADING(veh, ENTITY::GET_ENTITY_HEADING(PLAYER::PLAYER_PED_ID()));					PED::SET_PED_INTO_VEHICLE(PLAYER::PLAYER_PED_ID(), veh, -1);				}				Veh_Vector.push_back(veh);                     //saving it to a std::vector				WAIT(0);				char statusText[32];				sprintf_s(statusText, "%s spawned", modelName);				set_status_text(statusText);				return true;			}

No one uses C++14.

Link to comment
Share on other sites

 

No one uses C++14.

 

 

I actually meant C++11 not 14. Not in that code fragment either.

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.