FR0314 Posted November 20, 2021 Share Posted November 20, 2021 I would like to make the pervious car disappear, but only if the player that spawned it has spawned more than 2 cars. This is my current code: function makeveh( player, command, model ) local x, y, z = getElementPosition(player) local rotX, rotY, rotZ = getElementRotation(player) rotZ = rotZ + 90 y = y + 5 local vehicle = createVehicle(model, x, y, z, rotX, rotY, rotZ) warpPedIntoVehicle(player, vehicle) end addCommandHandler('makeVeh', makeveh, false, false) This would be a command that everyone could use, thanks for the help! Link to comment
DiSaMe Posted November 20, 2021 Share Posted November 20, 2021 You can make a table that contains the list of cars and store it as element data. function makeveh( player, command, model ) local x, y, z = getElementPosition(player) local rotX, rotY, rotZ = getElementRotation(player) rotZ = rotZ + 90 y = y + 5 local vehicle = createVehicle(model, x, y, z, rotX, rotY, rotZ) warpPedIntoVehicle(player, vehicle) local vehicles = getElementData(player, "vehicles") or {} -- retrieve the existing data, or an empty table if no data has been set table.insert(vehicles, 1, vehicle) -- insert the vehicle as first item, shifting other vehicles higher up in the list if vehicles[3] then -- if the third item in the list exists destroyElement(vehicles[3]) -- destroy the vehicle vehicles[3] = nil -- remove the vehicle from the list end setElementData(player, "vehicles", vehicles, "local") -- store the new vehicle list as element data end addCommandHandler('makeVeh', makeveh, false, false) Because inserting into the list shifts the previously created vehicles to higher indices, the first vehicle will be shifted to third index when you use the command the third time. Link to comment
The_GTA Posted November 20, 2021 Share Posted November 20, 2021 52 minutes ago, Reyomin said: You can make a table that contains the list of cars and store it as element data. If you are using a table that is stored inside element data that contains (vehicle) elements then this table is not removed on resource stop BUT the vehicle elements do get destroyed. This will result in a table with invalid element IDs which then get recycled by the runtime to point to different elements. Thus I do not recommend to store such data inside element data but inside a Lua table with player as key instead. Link to comment
DiSaMe Posted November 20, 2021 Share Posted November 20, 2021 10 minutes ago, The_GTA said: If you are using a table that is stored inside element data that contains (vehicle) elements then this table is not removed on resource stop BUT the vehicle elements do get destroyed. This will result in a table with invalid element IDs which then get recycled by the runtime to point to different elements. Thus I do not recommend to store such data inside element data but inside a Lua table with player as key instead. That is a possibility if resource gets restarted, yes. But it can be avoided by looping through all players and removing the element data when resource gets stopped. Conversely, using a Lua table with player as key requires removing the player data from that table when the player quits if you want to avoid similar problems. But now that you mentioned pointing to different elements, doesn't that look like a problem in design of MTA? I don't want to start a long discussion in this topic because it's not the right place, but generally I would expect that a variable will never start pointing to a different element than the one it was assigned. Link to comment
The_GTA Posted November 20, 2021 Share Posted November 20, 2021 (edited) 34 minutes ago, Reyomin said: That is a possibility if resource gets restarted, yes. But it can be avoided by looping through all players and removing the element data when resource gets stopped. Conversely, using a Lua table with player as key requires removing the player data from that table when the player quits if you want to avoid similar problems. Haha, good find with that case player quit case. Else you would definitely get the same invalid element ID problem as for the vehicles in the element data table. I could note that the element data is a heavier system than a simple Lua table. But you have pointed at the right solution to the invalid element ID issue and please mention it the next time or add the code to it right away. 34 minutes ago, Reyomin said: But now that you mentioned pointing to different elements, doesn't that look like a problem in design of MTA? I don't want to start a long discussion in this topic because it's not the right place, but generally I would expect that a variable will never start pointing to a different element than the one it was assigned. I was wondering about this too for a long time. This wouldn't be the first time I criticised MTA for runtime environment design misdecisions. But don't get me started. It kinda just works how it is currently implemented, ignoring the quality of implementation. Edited November 20, 2021 by The_GTA Link to comment
FR0314 Posted November 20, 2021 Author Share Posted November 20, 2021 3 hours ago, Reyomin said: You can make a table that contains the list of cars and store it as element data. function makeveh( player, command, model ) local x, y, z = getElementPosition(player) local rotX, rotY, rotZ = getElementRotation(player) rotZ = rotZ + 90 y = y + 5 local vehicle = createVehicle(model, x, y, z, rotX, rotY, rotZ) warpPedIntoVehicle(player, vehicle) local vehicles = getElementData(player, "vehicles") or {} -- retrieve the existing data, or an empty table if no data has been set table.insert(vehicles, 1, vehicle) -- insert the vehicle as first item, shifting other vehicles higher up in the list if vehicles[3] then -- if the third item in the list exists destroyElement(vehicles[3]) -- destroy the vehicle vehicles[3] = nil -- remove the vehicle from the list end setElementData(player, "vehicles", vehicles, "local") -- store the new vehicle list as element data end addCommandHandler('makeVeh', makeveh, false, false) Because inserting into the list shifts the previously created vehicles to higher indices, the first vehicle will be shifted to third index when you use the command the third time. I will test out this code, thanks! Link to comment
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