shaio Posted August 20, 2016 Posted August 20, 2016 How do I get this to work with offedit? I've tried and tried and tried. I really wanna save to json files. :L addCommandHandler('savemap', function (player, command, mapName) local account = getPlayerAccount(player) if (isGuestAccount(account) == true) then outputChatBox("You need to be logged in to use this!", player, 0, 255, 255) return end if mapName then if not string.find(mapName, '%W') then if (getElementData(player,"tempbuilder") == true) then if playerobj[player] then if #playerobj[player] >= 1 then local tempdata = {} tempdata['info'] = {} tempdata['info']['creator'] = getPlayerName(player) tempdata['info']['serial'] = getPlayerSerial(player) tempdata['info']['ip'] = getPlayerIP(player) tempdata['info']['time'] = getTimeStamp() tempdata['info']['dimension'] = getElementDimension(playerobj[player][i]) for _,i in ipairs(playerobj[player]) do if playerobj[player][i] then tempdata[i] = {} tempdata[i]['model'] = getElementModel(playerobj[player][i]) tempdata[i]['col'] = getElementCollisionsEnabled(playerobj[player][i]) local x, y, z, rx, ry, rz = getElementAttachedOffsets(playerobj[player][i]) tempdata[i]['pos'] = {x, y, z, rx, ry, rz} if getElementType(playerobj[player][i]) == 'vehicle' then tempdata[i]['type'] = 'vehicle' local colours = {getVehicleColor(playerobj[player][i], true)} for k=1,12 do if not colours[k] then colours[k] = 255 end end tempdata[i]['colour'] = colours else local scalex, scaley, scalez = getObjectScale(playerobj[player][i]) tempdata[i]['scale'] = {scalex, scaley, scalez} tempdata[i]['type'] = 'object' end end end local file = fileCreate('maps/'..mapName.. '.json') fileWrite(file, toJSON(tempdata)) fileClose(file) tempdata = {} outputChatBox('Saved map as ' .. mapName, player, 0, 255, 255) end end end else outputChatBox('Invalid map name.', player, 0, 255, 255) end else outputChatBox('SYNTAX: /savemap name', player, 0, 255, 255) end end ) function loadMap(player,cmd,mapName) local account = getPlayerAccount(player) if (isGuestAccount(account) == true) then outputChatBox("You need to be logged in to use this!", player, 0, 255, 255) return end local vehicle = vehicle if not vehicle then vehicle = getPedOccupiedVehicle(player) end if vehicle then if fileExists('maps/'..mapName..'.json') then local name if isElement(player) then clearVehicleObjects(player) outputChatBox('Map loaded: ' ..mapName, player, 0, 255, 255) name = getPlayerName(player) else name = 'the server' end local file = fileOpen('maps/'..mapName..'.json') local size = fileGetSize(file) local buffer = fileRead(file, size) local tempdata = fromJSON(buffer) fileClose(file) playerobj[player] = {} local vx, vy, vz = getElementPosition(vehicle) for k,v in pairs (tempdata) do if k ~= 'info' then local id = tonumber(k) local ids = tostring(k) if tempdata[ids]['type'] == 'vehicle' then objectid[playerobj][id] = createVehicle(tempdata[ids]['model'], vx, vy, vz) setVehicleColor(playerobj[player][id], unpack(tempdata[ids]['colour'])) else playerobj[player][id] = createObject(tempdata[ids]['model'], vx, vy, vz) scalex, scaley, scalez = unpack(tempdata[ids]['scale']) setObjectScale(playerobj[player][id], scalex, scaley, scalez) end setElementCollisionsEnabled(attachment[vehicle][id], tempdata[ids]['col']) end end tempdata = {} else outputChatBox("Map " ..mapName.. " failed to load.", player, 0, 255, 255) end end end
shaio Posted August 20, 2016 Author Posted August 20, 2016 i want to save offedit maps inside of json files, with all the properties of the objects, meaning scale, position, rotation, collidable(true/false) etc..
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