I was actually working on a similar thing earlier, here’s the code:
permaprops.lua (in your lua/autorun directory)
[lua]require(“glon”)
PERMA = {}
PERMA.Pos = {}
PERMA.File = “permaprops.txt”
if SERVER then
function PERMA:Initialize()
self:LoadFromFile()
for _,v in pairs( ents.GetAll() ) do
if v:GetNWBool("isperma") then v:Remove() end
end
for _,v in pairs( self.Pos ) do
if game.GetMap() == v.map then
local e = ents.Create( v.class )
e:SetNWBool( "isperma", true )
e:SetPos( v.pos )
e:SetAngles( v.ang )
e:SetColor( v.color.r, v.color.g, v.color.b, v.color.a )
e:SetModel( v.model )
e:SetMaterial( v.material )
e:SetSkin( v.skin )
e:SetSolid( v.solid )
e:SetName( v.name )
e:Spawn()
e:Activate()
local phys = e:GetPhysicsObject()
if IsValid( phys ) then phys:Sleep() end
end
end
concommand.Add("perma_save",function(pl)
if not pl:IsAdmin() then return end
local e = pl:GetEyeTrace().Entity
if not ValidEntity( e ) then pl:ChatPrint("That is not a valid entity!") return end
if e:GetNWBool("isperma") then pl:ChatPrint("That entity is already permanent!") return end
self:SaveEnt( e )
pl:ChatPrint("You saved " .. e:GetClass() .. " with model ".. e:GetModel() .. " to the database.")
end)
concommand.Add("perma_printfile",function()
PrintTable( glon.decode(file.Read(self.File)))
end)
concommand.Add("perma_printcurrent",function()
PrintTable( self.Pos )
end)
concommand.Add("perma_erase",function( pl )
if not pl:IsSuperAdmin() then return end
for _,v in pairs( ents.GetAll() ) do
if v:GetNWBool("isperma") then v:SetName("") end
end
file.Write(self.File,glon.encode({}))
self.Pos = {}
pl:ChatPrint("Database successfully erased!")
end)
concommand.Add("perma_remove",function( pl )
if not pl:IsAdmin() then return end
local e = pl:GetEyeTrace().Entity
if not ValidEntity( e ) then pl:ChatPrint("That is not a valid entity!") return end
if not e:GetNWBool("isperma") then pl:ChatPrint("That is not a PermaProp!") return end
self:RemoveEnt( e )
e:SetNWBool("isperma",false)
pl:ChatPrint("You erased " .. e:GetClass() .. " with a model of " .. e:GetModel() .. " from the database.")
end)
concommand.Add("perma_isperma",function(pl)
local e = pl:GetEyeTrace().Entity
if not ValidEntity( e ) then pl:ChatPrint("That is not a valid entity!") return end
pl:ChatPrint( "That entity is" .. ( not e:GetNWBool("isperma") && " not " || " " ) .. "a PermaProp.")
end)
end
function PERMA:SaveEnt( ent )
local col = { ent:GetColor() }
local info = {
name = "perma"..table.Count(self.Pos),
class = ent:GetClass(),
pos = ent:GetPos(),
ang = ent:GetAngles(),
color = Color(col[1],col[2],col[3],col[4]),
model = ent:GetModel(),
material = ent:GetMaterial(),
skin = ent:GetSkin(),
solid = ent:GetSolid(),
map = game.GetMap(),
}
ent:SetName( info.name )
ent:SetNWBool("isperma", true )
self.Pos[info.name] = info
self:SaveToFile()
end
function PERMA:RemoveEnt( ent )
local name = ent:GetName()
if not self.Pos[name] then return end
self.Pos[name] = nil
self:SaveToFile()
end
function PERMA:LoadFromFile()
if not file.Exists(self.File) then file.Write(self.File,glon.encode({}))
else self.Pos = glon.decode(file.Read(self.File)) end
end
function PERMA:SaveToFile()
file.Write(self.File,glon.encode(self.Pos))
end
hook.Add("InitPostEntity","InitializePermaProps",function() PERMA:Initialize() end)
end[/lua]
And there’s also a tool you can use if you want:
[lua]if SERVER then
AddCSLuaFile(“weapons/gmod_stool/stools/permaprops.lua”)
end
TOOL.Name = “#PermaProps”
TOOL.Category = “Construction”
TOOL.Command = nil
TOOL.ConfigName = “”
if CLIENT then
language.Add( “Tool_permaprops_name”, “PermaProps” )
language.Add( “Tool_permaprops_desc”, “Save a prop for server restarts” )
language.Add( “Tool_permaprops_0”, “Left click to make a a prop permanent. Right click to make a prop temporary.” )
//language.Add( “Tool_unbreakable_etendre”, “Extend to constrained objects” )
end
function TOOL:LeftClick( tr )
RunConsoleCommand(“perma_save”)
return true
end
function TOOL:RightClick( tr )
RunConsoleCommand(“perma_remove”)
return true
end
function TOOL:Reload( tr )
RunConsoleCommand(“perma_isperma”)
return true
end
function TOOL.BuildCPanel( panel)
panel:AddControl( "Header" , { Text = "#Tool_permaprops_name", Description = "#Tool_permaprops_desc" } )
end[/lua]
This is different in that you can spawn the prop, and then put it some where and then save its position. Also, it saves more information about it since it uses glon and not SQL.