++tachyon Posted June 13, 2015 Share Posted June 13, 2015 (edited) 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 June 13, 2015 by ++tachyon Link to comment Share on other sites More sharing options...
outlier Posted June 13, 2015 Share Posted June 13, 2015 (edited) 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 June 13, 2015 by outlier Link to comment Share on other sites More sharing options...
leftas Posted June 13, 2015 Share Posted June 13, 2015 (edited) 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 June 13, 2015 by leftas Link to comment Share on other sites More sharing options...
++tachyon Posted June 13, 2015 Author Share Posted June 13, 2015 (edited) 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 June 13, 2015 by ++tachyon Link to comment Share on other sites More sharing options...
outlier Posted June 13, 2015 Share Posted June 13, 2015 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 More sharing options...
++tachyon Posted June 13, 2015 Author Share Posted June 13, 2015 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 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 More sharing options...
outlier Posted June 13, 2015 Share Posted June 13, 2015 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 More sharing options...
outlier Posted June 13, 2015 Share Posted June 13, 2015 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 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 More sharing options...
++tachyon Posted June 15, 2015 Author Share Posted June 15, 2015 (edited) Thanks for your help! I uploaded the script on gtainside.com Enjoy! Edited June 18, 2015 by ++tachyon Link to comment Share on other sites More sharing options...
leftas Posted June 16, 2015 Share Posted June 16, 2015 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 More sharing options...
0xsatoshi Posted June 18, 2015 Share Posted June 18, 2015 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 More sharing options...
++tachyon Posted June 18, 2015 Author Share Posted June 18, 2015 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now