jkub Posted December 8, 2009 Share Posted December 8, 2009 I am working on a script for adding cars moving them around and saving them to a seperate lua file. My problem here is that every time I save the map it only saves one of the vehicles. I may have placed several vehicles on the map to save but it will only save the last one I edited and only write that single car to the file please help 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 file = fileCreate ( "output/" ..name.. ".lua" ) fileWrite ( file, "--File Generated with Dr.PhillyBlunt's Experimental Vehicle Mapper and Mapped by " ..getPlayerName(player).. "\r\n" ) for k,v in pairs ( car ) do if car[k].car then local vx, vy, vz = getElementPosition ( car[k].car ) local vehName = getVehicleName ( car[k].car ) local realID = getVehicleModelFromName ( vehName ) fileWrite ( file, "car[" ..#car.. "] = createVehicle ( " ..realID.. ", " ..vx.. ", " ..vy.. ", " ..vz.. " ) -- " ..vehName.. " \r\n" ) outputChatBox ( "File processed", player, 0, 255, 0 ) end end fileClose ( file ) end end end ) Link to comment
Remp Posted December 8, 2009 Share Posted December 8, 2009 are you sure you are properly indexing your car table? how many times do you see "File Processed"? i tried it out with my own test code and it works ok (assuming you are also using sequential numerical indexing): local car = { [1] = {car = createVehicle(432,10,10,10)}, [2] = {car = createVehicle(433,10,10,20)} } though i did notice the index in the output code will always be the total number of vehicles, so you might want to use your own variable to track the index instead. you also dont need to use car[k] in the loop, car[k] is the same as v, so you can just do v.car: ... local count = 0 for _,v in pairs ( car ) do if v.car then count = count + 1 local vx, vy, vz = getElementPosition ( v.car ) local vehName = getVehicleName ( v.car ) local realID = getVehicleModelFromName ( vehName ) fileWrite ( file, "car[" ..count.. "] = createVehicle ( " ..realID.. ", " ..vx.. ", " ..vy.. ", " ..vz.. " ) -- " ..vehName.. " \r\n" ) outputChatBox ( "File processed", player, 0, 255, 0 ) end end ... Link to comment
jkub Posted December 8, 2009 Author Share Posted December 8, 2009 I tried that it didnt really work though... It was doing the same thing as before. Also the processed file message only appears one time does that mean its not looping through all the vehicles correctly? May there be a fault in my other part of the code? 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 local pX, pY, pZ = getElementPosition ( player ) local pRZ = getPedRotation ( player ) if carCount <= maxCarCount then car[carCount] = {} car[carCount].car = createVehicle ( getVehicleModelFromName(vehicleName), pX, pY + 5, pZ + 1, 0, 0, pRZ ) 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, player, 0, 255, 0 ) end end end end end addCommandHandler ( "add", cmdAddVehicle ) addCommandHandler ( "setstepping", function ( player, cmd, newStepping ) stepping = tonumber(newStepping) outputChatBox ( "Stepping set to " ..newStepping, player, 0, 255, 0 ) end ) function saveTheCar ( player, cmd ) destroyElement ( selMarker ) selMarker = nil outputChatBox ( "Vehicle Placed!", player, 0, 255, 0 ) end addCommandHandler ( "savecar", saveTheCar ) 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 file = fileCreate ( "output/" ..name.. ".lua" ) fileWrite ( file, "--File Generated with Dr.PhillyBlunt's Experimental Vehicle Mapper and Mapped by " ..getPlayerName(player).. "\r\n" ) fileWrite ( file, "local car = {} \r\n" ) local count = 0 for _,v in pairs ( car ) do if v.car then count = count + 1 local vx, vy, vz = getElementPosition ( v.car ) local vehName = getVehicleName ( v.car ) local realID = getVehicleModelFromName ( vehName ) fileWrite ( file, "car[" ..count.. "] = createVehicle ( " ..realID.. ", " ..vx.. ", " ..vy.. ", " ..vz.. " ) -- " ..vehName.. " \r\n" ) outputChatBox ( "File processed", player, 0, 255, 0 ) end end fileClose ( file ) end end end ) Link to comment
eAi Posted December 8, 2009 Share Posted December 8, 2009 You should save the location of things using map files - that's what they're designed for and will probably do what you want in a couple of lines. See saveMapData which will save anything you have loaded in-game into a map file (or just part of what you have loaded). Link to comment
jkub Posted December 8, 2009 Author Share Posted December 8, 2009 Thanks but what if I want to write it to an lua? Link to comment
Remp Posted December 8, 2009 Share Posted December 8, 2009 you arent incrementing carCount, it will always be 0 so every new car you make just overwrites the previous one ... local pX, pY, pZ = getElementPosition ( player ) local pRZ = getPedRotation ( player ) if carCount <= maxCarCount then carCount = carCount + 1 -- increment the car count car[carCount] = {} car[carCount].car = createVehicle ( getVehicleModelFromName(vehicleName), pX, pY + 5, pZ + 1, 0, 0, pRZ ) ... Link to comment
jkub Posted December 8, 2009 Author Share Posted December 8, 2009 Thank You so much that worked Wonderfuly! Ill get back and see what I can do Link to comment
eAi Posted December 8, 2009 Share Posted December 8, 2009 Thanks but what if I want to write it to an lua? You'll end up with slower code that isn't compatible with other resources or other editors. Your choice, of course. 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