Skip to content

Instantly share code, notes, and snippets.

@ExtReMLapin
Last active October 10, 2018 19:59
Show Gist options
  • Save ExtReMLapin/4cb9b43ce7c7eb11bc0209c49107f1cb to your computer and use it in GitHub Desktop.
Save ExtReMLapin/4cb9b43ce7c7eb11bc0209c49107f1cb to your computer and use it in GitHub Desktop.
gmod auto prop save for multiplayer and singleplayer (no owned prop system like ffp)
local function saveentities()
local tbl = {}
local filepos = Format("_savefilemap_%s.txt", game.GetMap())
for k, v in pairs(ents.FindByClass("prop_physics")) do
if v:MapCreationID() != -1 then continue end
table.insert(tbl, {v:GetModel(), v:GetPos(), v:GetAngles(), v:GetMaterial()})
end
local tbltxt = util.TableToJSON(tbl)
file.Write(filepos,tbltxt)
MsgC(Color(50,255,50), "Wrote " ,Color(100,100,255), tostring(#tbl), Color(50,255,50), " props in file : ", filepos, "\n")
end
local function restoreentities()
local i = 0
local filepos = Format("_savefilemap_%s.txt", game.GetMap())
if not file.Exists(filepos,"DATA") then return false end
local txt = file.Read(filepos, "DATA")
local tbl = util.JSONToTable(txt)
for k, v in ipairs(tbl) do
local tmpent = ents.Create("prop_physics")
tmpent:SetModel(v[1])
tmpent:SetPos(v[2])
tmpent:SetAngles(v[3])
tmpent:SetSolid(SOLID_VPHYSICS)
tmpent:PhysicsInit(SOLID_VPHYSICS)
if IsValid(tmpent:GetPhysicsObject()) then
tmpent:GetPhysicsObject():EnableMotion(false)
else
i = i +1
print(Format("missing prop : %s with id %i", v[1], i))
end
tmpent:SetMaterial(v[4])
end
end
hook.Add("InitPostEntity","prop restore lapin",function()
restoreentities()
timer.Create("mapsaveprop",60,0,function()
saveentities()
end)
end)
concommand.Add("forcesavemap",saveentities)
@ExtReMLapin
Copy link
Author

Drop the file in lua/autorun/server/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment