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

    2. GrandTheftAuto.net - Website Re-Launch

    1. GTA Online

      1. The Diamond Casino Heist
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Red Dead Online

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

      1. Events
    1. Red Dead Redemption 2

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

    1. Grand Theft Auto Series

    2. GTA 6

    3. GTA V

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

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

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    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. DYOM
      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. 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
    3. Gangs

    1. News

    2. Forum Support

    3. Site Suggestions

++tachyon

[C++] Preventing vehicles from disappearing

Recommended Posts

++tachyon

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

Share this post


Link to post
Share on other sites
outlier

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

Share this post


Link to post
Share on other sites
leftas

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

Share this post


Link to post
Share on other sites
++tachyon

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

Share this post


Link to post
Share on other sites
outlier

 

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 :)

Share this post


Link to post
Share on other sites
++tachyon

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);

Share this post


Link to post
Share on other sites
outlier

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 :)

Share this post


Link to post
Share on other sites
outlier

 

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 :)

Share this post


Link to post
Share on other sites
++tachyon

Thanks for your help!

 

I uploaded the script on gtainside.com

 

Enjoy!

Edited by ++tachyon

Share this post


Link to post
Share on other sites
leftas

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.

Share this post


Link to post
Share on other sites
0xsatoshi

 

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.

Share this post


Link to post
Share on other sites
++tachyon

 

No one uses C++14.

 

 

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

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.

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.


  • 2 Users Currently Viewing
    0 members, 0 Anonymous, 2 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.