Jump to content

Save objects to .map file


Zorgman

Recommended Posts

  • Moderators
Posted (edited)

Loop trough all objects, get details and write it to a file, like any .map editor file.
You can check the syntax in an existing .map file.

 

Something like that

-- CLIENT SIDE

local objects = getElementsByType("object")
local file    = fileCreate("clonedmap.map")

fileWrite(file, "<map>\n")
for i = 1, #objects do
    local obj        = objects[i]
    local x, y, z    = getElementPosition(obj)
    local rx, ry, rz = getElementRotation(obj)

    row = ('\t<object id="something (%d)" breakable="%s" interior="%d" collisions="%s" alpha="%d" model="%d" doublesided="%s" scale="%d" posX="%f" posY="%f" posZ="%f" rotX="%f" rotY="%f" rotZ="%f"></object>\n'):format(
        i,
        tostring(isObjectBreakable(obj)),
        getElementInterior(obj),
        tostring(getElementCollisionsEnabled(obj)),
        getElementAlpha(obj),
        getElementModel(obj),
        tostring(isElementDoubleSided(obj)),
        getObjectScale(obj),
        x, y, z,
        rx, ry, rz
    )
    fileWrite(file, row)
end
fileWrite(file, "</map>")

fileClose(file)

 

Save file to mta_folder/mods/deathmatch/resources/YOUR_RESOURCE_NAME/clonedmap.map

Edited by Patrick
  • Moderators
Posted (edited)

An alternative is to save an specific part of the element tree. Pick the method that works best for you.

 

local file = xmlCreateFile ("map.xml", "map")
local baseElement = createElement("temporary")

--[[
	
]]

local element = -- createObject, createVehicle, etc. etc,

setElementParent(element, baseElement)

--[[
	
]]

saveMapData ( file, baseElement )
xmlSaveFile ( file )
xmlUnloadFile ( file )

destroyElement(baseElement) -- destroy every element, parent as well as the children thanks to propagation

Pro's

  • Flexibility, you do not have to write specific code for other elements.

 

Cons:

  • Serverside only
  • No custom properties with special characters, for example : notations and code comments on the fly.
  • All properties are copied, you do not have fully control which ones are saved.
Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   ?

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

I didn't knew about saveMapData either, this was actually what I was looking for so I tried it first, but it produced an xml file with empty object elements, not sure what i messed up ? Patrick's solution worked for me, I got my map. Thank you both!

BHgi29I.png
  Dystopia alpha 0.75
DOWNLOAD

  • Moderators
Posted (edited)
20 hours ago, Zorgman said:

not sure what i messed up ? Patrick's solution worked for me

You didn't mess anything up, but it seems the function has been broken somewhere on the versioooon-way.

The wiki example didn't work either.

local baseElement = createElement("map-dinner")

--[[
	
]]

local element = createObject ( 1337, 5540.6654, 1020.55122, 1240.545, 90, 0, 0 )
setElementParent(element, baseElement)

--[[
	
]]


local file = xmlCreateFile ("map.xml", "map")
saveMapData ( file, baseElement, true )
xmlSaveFile ( file )
xmlUnloadFile ( file )

--[[
<map>
    <object></object>
</map>
]]

local file = xmlCreateFile("saved.map", "map")
if file then
   saveMapData ( file, getResourceRootElement(getThisResource()), true )
   xmlSaveFile ( file )
   xmlUnloadFile ( file )
end
--[[
<map>
    <map>
        <map-dinner>
            <object></object>
        </map-dinner>
    </map>
</map>
]]

 

I am going to add a bug report.

https://github.com/multitheftauto/mtasa-blue/issues/1473

 

Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   ?

 

  Useful functions  3x 

  Tutorials  4x 

 

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...