Jump to content

Need Help Selecting from a table


jkub

Recommended Posts

Hello. I have released a command based vehicle mapper a while back on the resource page and yesterday made a major working update to it. Today I wanted to release another update. This one I am working on now gives you the ability to destroy vehicles from the map and select other vehicles you have previously saved into the map. For the most part this works BUT I have a big problem nonetheless.

Its kind of hard to explain so I will make an example

1.I add 5 banshees using the mapper and save them into the map.

2.Since I currently have no vehicles selected I will start selecting other vehicles. So I select vehicle 1 from the table, then 2, then 3, then 4, ... here is the problem. When I try to select my last vehicle ( Vehicle 5 ) it selects vehicle 4. Its like I never have created a 5th vehicle. And when I save the whole map to a map or lua file the last 2 vehicles are ALWAYS duplicate of the vehicle before the last. Same positions model color etc.

Here is some code

function cmdSelectVehicle ( player, cmd, selID )
num = tonumber(selid)
if selMarker then
outputChatBox ( "*You must either save or delete the current vehicle you are working on*", player, 255, 0, 0 )
else
if car[num] then
		car[carCount].car = car[tonumber(selID)].car
local x, y, z = getElementPosition ( car[carCount].car )
		selMarker = createMarker ( x, y, z, "arrow", 2, 255, 255, 0, 125, getRootElement() )
attachElements ( selMarker, car[carCount].car, 0, 0, 5 )
outputChatBox ( "*You have selected vehicle index: " ..tonumber(selID).. " *", player, 0, 255, 0 )
else
outputChatBox ( "*You have selected an invalid vehicle index*", player, 255, 0, 0 )
end
end
end
addCommandHandler ( "sel", cmdSelectVehicle )

Link to comment

I've since then fixed that from selid to selID and still does the same thing. Like you said I can't understand why most of it work but the last insertion in the table is missing. Could it be the "carCount" thing? I know that that value is manipulated several places in the script. Do I need to post any more code?

EDIT: Any time I select an index that isn't valid It will tell me via script and perhaps a line in the debug but always the last vehicle I select in the table it says nothing in debug almost as if it was perfect.

Link to comment

What's the carCount? If it's the number of vehicles you spawn, then: Why do you change car[carCount].car to the vehicle which you select (replaces the last vehicle in table to the one you select)?

Link to comment

Yes. I think I am trying to make carCount the number of cars in the table. Here is alot more code. I post functions for saving removing and selecting vehicles. I do not know how to properly "select" another vehicle from the table for "editing" without overwriting values as you said.

local carCount = 0
maxCarCount = 50
car = {}
 
function cmdAddVehicle ( player, cmd, vehicleName )
if cmd == "add" then
if selMarker then
outputChatBox ( "You are already editing a vehicle... save your current", player, 255, 0, 0 )
else
if vehicleName == nil then
outputChatBox ( "You need to specify a vehicle name in order to create a vehicle", player, 255, 0, 0 )
else
local pX, pY, pZ = getElementPosition ( player )
local pRZ = getPedRotation ( player )
if carCount <= maxCarCount then
			    carCount = carCount + 1
				car[carCount] = {}
				car[carCount].car = createVehicle ( getVehicleModelFromName(vehicleName), pX, pY + 5, pZ + 1, 0, 0, pRZ )
setElementData ( car[carCount].car, "owner", player )
local x, y, z = getElementPosition ( car[carCount].car )
				selMarker = createMarker ( x, y, z, "arrow", 2, 255, 255, 0, 125, getRootElement() )
attachElements ( selMarker, car[carCount].car, 0, 0, 5 )
if car[carCount].car then
outputChatBox ( "You have created a " ..vehicleName.. " (Vehicle Index: #FFFFFF" ..carCount, player, 0, 255, 0, true )
local model = getVehicleModelFromName (vehicleName)
if model == 577 or model == 447 or model == 460 then
warpPedIntoVehicle ( player, car[carCount].car, 0 )
end
end
else
outputChatBox ( "*You have reached the maximum limit of cars*", player, 255, 0, 0 )
end
end
end
end
end
addCommandHandler ( "add", cmdAddVehicle )
 
function cmdRemoveVehicle ( player, cmd )
if cmd == "del" then
if selMarker then
destroyElement ( car[carCount].car )
		car[carCount] = {carCount}
destroyElement ( selMarker )
		selMarker = nil
		carCount = carCount - 1
outputChatBox ( "*Vehicle deleted*", player, 0, 255, 0 )
else
outputChatBox ( "*You have no vehicle selected to delete*", player, 255, 0, 0 )
end
end
end
addCommandHandler ( "del", cmdRemoveVehicle )
 
function cmdSelectVehicle ( player, cmd, selID )
num = tonumber(selID)
if selMarker then
outputChatBox ( "*You must either save or delete the current vehicle you are working on*", player, 255, 0, 0 )
else
if car[num] then
        car[carCount].car = car[num].car
local x, y, z = getElementPosition ( car[carCount].car )
        selMarker = createMarker ( x, y, z, "arrow", 2, 255, 255, 0, 125, getRootElement() )
attachElements ( selMarker, car[carCount].car, 0, 0, 5 )
outputChatBox ( "*You have selected ( vehicle index: #FFFFFF" ..tonumber(selID).. "*", player, 0, 255, 0, true )
else
outputChatBox ( "*You have selected an invalid vehicle index*", player, 255, 0, 0 )
end
end
end
addCommandHandler ( "sel", cmdSelectVehicle )
 
function saveTheCar ( player, cmd )
if selMarker then
destroyElement ( selMarker )
	selMarker = nil
outputChatBox ( "*Vehicle Saved*", player, 0, 255, 0 )
end 
end
addCommandHandler ( "savecar", saveTheCar )

Link to comment

If I were you, I would create a global variable which would hold vehicle element (whichever you select) and then use this variable anywhere where you edit the vehicle (position, colour, etc.)

Something like this:

local selectedVeh;
 
-- select command:
selectedVeh = car[ tonumber( selID ) ].car;

Link to comment

Thanks:)

But now If I delete any vehicle before saving the map I get flooded with errors and it doesent save right. If I save the map without ever removing any vehicles it works fine. So I assume the problem lies in my remove function somewhere. I have a bogus way of getting the correct vehicle in the remove function so I wonder may that have something to do with it?

function cmdSelectVehicle ( player, cmd, selID )
num = tonumber(selID)
if selMarker then
outputChatBox ( "*You must either save or delete the current vehicle you are working on*", player, 255, 0, 0 )
else
if car[num] then
local selectedVeh
        selectedVeh = car [tonumber(selID)].car
local x, y, z = getElementPosition ( selectedVeh )
        selMarker = createMarker ( x, y, z, "arrow", 2, 255, 255, 0, 125, getRootElement() )
attachElements ( selMarker, selectedVeh, 0, 0, 5 )
outputChatBox ( "*You have selected ( vehicle index: #FFFFFF" ..tonumber(selID).. "*", player, 0, 255, 0, true )
else
outputChatBox ( "*You have selected an invalid vehicle index*", player, 255, 0, 0 )
end
end
end
addCommandHandler ( "sel", cmdSelectVehicle )
 
function cmdRemoveVehicle ( player, cmd )
if cmd == "del" then
if selMarker then
local selectedVeh
     	 selectedVeh = getElementAttachedTo ( selMarker )
destroyElement ( selectedVeh )
destroyElement ( selMarker )
        selMarker = nil
        carCount = carCount - 1
outputChatBox ( "*Vehicle deleted*", player, 0, 255, 0 )
else
outputChatBox ( "*You have no vehicle selected to delete*", player, 255, 0, 0 )
end
end
end
addCommandHandler ( "del", cmdRemoveVehicle )
 
addCommandHandler ( "save",
function ( player, cmd, name )
if cmd == "save" then
if selMarker then
outputChatBox ( "You are currently editing a vehicle you must save the vehicle before you can save the map", player, 255, 0, 0 )
else
if name == nil then
outputChatBox ( "You need to specify a name for your vehicle map before you can save it", player, 255, 0, 0 )
else
				file = fileCreate ( "output/" ..name.. "."..mode.."" )
if mode == "map" then
fileWrite ( file, " \r\n" )
elseif mode == "lua" then
fileWrite ( file, "--File Generated with Dr.PhillyBlunt's Experimental Vehicle Mapper and Mapped by " ..getPlayerName(player).. "\r\n" )
end
local count = 0
if mode == "lua" then
fileWrite ( file, "local car = {} \r\n" )
else
fileWrite ( file, '\r\n' )
end
for _,v in pairs ( car ) do
if v.car then
outputChatBox ( "V.car has been found", player, 0, 255, 0 )
							count = count + 1
local vx, vy, vz = getElementPosition ( v.car )
local rx, ry, rz = getVehicleRotation ( v.car )
local color1, color2, color3, color4 = getVehicleColor ( v.car )
local dimension = getElementDimension ( v.car )
local interior = getElementInterior ( v.car )
local vehName = getVehicleName ( v.car )
local realID = getVehicleModelFromName ( vehName )
if mode == "map" then
fileWrite ( file, '') ('..count..')" color="'..color1..','..color2..','..color3..','..color4..'" dimension="'..dimension..'" interior="'..interior..'" model="'..realID..'" plate="GTASA" posX="'..vx..'" posY="'..vy..'" posZ="'..vz..'" rotX="'..rx..'" rotY="'..ry..'" rotZ="'..rz..'" upgrades=""/> \r\n' )
elseif mode == "lua" then
fileWrite ( file, "car[" ..count.. "] = createVehicle ( " ..realID.. ", " ..vx.. ", " ..vy.. ", " ..vz.. ", " ..rx.. ", " ..ry.. ", " ..rz.. " ) -- " ..vehName.. " ) \r\n" )
fileWrite ( file, "setVehicleColor ( car[" ..count.. "]," ..color1.. ", " ..color2.. ", " ..color3.. ", " ..color4.. " ) \r\n" )
end
end
end
if mode == "map" then
fileWrite ( file, "" )
end
fileClose ( file )
outputChatBox ( "File processed and saved as #00FF00" ..name.. "#0000FF."..mode.."", player, 255, 255, 255, true )
end
end
end
end
)

Link to comment

Well, when you delete vehicle, you remove the vehicle from game and memory but the pointer to the vehicle (which doesn't exist any more) is still in your car table. So, you have to remove the vehicle from the table as well (nil it) or when you save your map and you loop through the table, just add an if statement with isElement, which will check if the "vehicle variable" is vehicle. Something like:

...
if isElement( v.car ) then
-- if vehicle exists, isElement will return true and code will follow here, where you save your vehicle
end
...

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...