I am trying to get entities to spawn from a text file but I can't seem to get it working. To top it off its not throwing any errors and I am out of ideas now =X
[code]
concommand.Add("test_write", function()
if ( !file.IsDir( "testing", "DATA" ) ) then file.CreateDir( "testing" ) end
local Map = game.GetMap()
local filename = "testing/"..Map..".txt"
local BlockPositions = {}
for k, v in pairs( ents.FindByModel( "models/hunter/blocks/cube1x1x025.mdl") ) do
template = { class=v:GetClass(), pos=v:GetPos() }
table.insert( BlockPositions, template )
end
local towrite = util.TableToJSON( BlockPositions )
file.Write( filename, ("\n"..towrite) )
if file.Exists( filename, "DATA" ) then
local str = file.Read( filename, "DATA" )
local thetable = util.JSONToTable( str ) -- Convert JSON formatted string back to a table
PrintTable( thetable )
end
end )
concommand.Add ("test_spawn", function()
local Map = game.GetMap()
local filename = "testing/"..Map..".txt"
local str = file.Read( filename, "DATA" )
local thetable = util.JSONToTable( str )
for k,v in ipairs(thetable) do
local pos = v.pos
local entType = v.class
print(pos)
newEnt = ents.Create( entType )
newEnt:SetPos( Vector( pos ) )
newEnt:Spawn()
newEnt:Activate()
end
end)
[/code]
We can't help you with out the code you are using to save the file.
Updated original post. Sorry I was trying to keep it as simple as possible.
Okay, If you had looked into the console, you'd have known the problem:
[img]http://i.imgur.com/inkDjdL.jpg[/img]
Then, you are doing some incorrect things in your code:
[code]file.Write( filename, ("\n"..towrite) )[/code]
You absolutely don't need to add "\n" there, just do
[code]file.Write( filename, towrite )[/code]
And lastly [code]newEnt:SetPos( Vector( pos ) )[/code]
TableToJSON and JSONToTable save the variable type, you don't need to do anything like that, just do
[code]newEnt:SetPos( pos )[/code]
And by the way, you don't need to define variables for everything. Here's fixed code
[code]
concommand.Add("test_write", function()
if ( !file.IsDir( "testing", "DATA" ) ) then file.CreateDir( "testing" ) end
local BlockPositions = {}
for k, v in pairs( ents.FindByModel( "models/hunter/blocks/cube1x1x025.mdl") ) do
table.insert( BlockPositions, { class = v:GetClass(), pos = v:GetPos() } )
end
file.Write( "testing/" .. game.GetMap() .. ".txt", util.TableToJSON( BlockPositions ) )
end )
concommand.Add ("test_spawn", function()
local str = file.Read( "testing/" .. game.GetMap() .. ".txt", "DATA" )
local thetable = util.JSONToTable( str )
for k, v in ipairs( thetable ) do
newEnt = ents.Create( v.class )
newEnt:SetPos( v.pos )
newEnt:SetModel( "models/hunter/blocks/cube1x1x025.mdl" )
newEnt:Spawn()
newEnt:Activate()
end
end)
[/code]
Sorry I didn't see that error I was running a listen server, were you running a dedicated? Ya the variables were shit it was in a hectic testing phase :p Thanks for breaking down I really appreciate it!
Thanks again!
Sorry, you need to Log In to post a reply to this thread.