Loading data from JSON / When server starts up spawning ents.
5 replies, posted
Hello FP, I'm trying to figure out what the issue is, what I'm trying to do is when the server restarts the positions angles of the npc(s) should spawn but they don't. Although, it does print in the console what is inside the file but ittdoesn't spawn them.
[LUA]
function loadNPCs()
local loadedNPCs = {}
if file.IsDir("ee_npcs", "DATA") and file.Exists( "ee_npcs/ee_npc_" .. game.GetMap() .. ".txt", "DATA" ) then
local content = file.Read("ee_npcs/ee_npc_" .. game.GetMap() .. ".txt", "DATA")
loadedNPCs = util.JSONToTable(content)
for i,v in ipairs(loadedNPCs) do
local i = ents.Create( "ee_npc" )
if ( !IsValid( ee_npc ) ) then return end
i:SetModel( "models/odessa.mdl" )
i:SetPos( v.pos )
i:SetAngles( v.ang )
i:Spawn()
end
PrintTable( loadedNPCs)
end
end
hook.Add("InitPostEntity", "bell_loadNPCs_easyemploy", loadNPCs)
[/LUA]
-- Below is what's prinited in the console on start-up.
[LUA]
ee_npc_test:
ang = 0.000 -134.311 0.000
pos = 1775.968750 831.968811 -143.968796
[LUA]
Can anyone see anything what I'm doing wrong? The entities should be spawning in the positions read from the file.
Help will be greatly appreciated, thanks!
[code]local function spawnSavedNpcs(npcs)
timer.Simple(10, function()
for _, data in pairs(npcs) do
local npc = ents.Create('ee_npc')
npc:SetPos(data.pos)
npc:SetAngles(data.ang)
npc:Spawn()
npc:SetModel("models/odessa.mdl" )
end
end)
end
hook.Add('Initialize', 'loadNpcs', function()
if file.Exists('npcpos.txt', "DATA") then
spawnSavedNpcs(util.JSONToTable(file.Read('npcpos.txt', "DATA")))
else
file.Write('npcpos.txt', util.TableToJSON({}))
end
end)
concommand.Add('savenpc', function()
local npcs = {}
for _, npc in ipairs(ents.FindByClass('ee_npc')) do
npcs[#npcs + 1] = { pos = npc:GetPos(), ang = npc:GetAngles() }
end
file.Write('npcpos.txt', util.TableToJSON(npcs))
end)[/code]
Try pairs instead of ipairs
You're setting the entity to `i` but checking if `ee_npc` is valid.
I thought to my knowledge ipairs runs in numeric order?
[editline]30th June 2017[/editline]
[QUOTE=bigdogmat;52418756]You're setting the entity to `i` but checking if `ee_npc` is valid.[/QUOTE]
Ah I see! I'll fix that and see if it makes a difference. I'll let you know.
[editline]30th June 2017[/editline]
[QUOTE=Pypsikan;52418637][code]local function spawnSavedNpcs(npcs)
timer.Simple(10, function()
for _, data in pairs(npcs) do
local npc = ents.Create('ee_npc')
npc:SetPos(data.pos)
npc:SetAngles(data.ang)
npc:Spawn()
npc:SetModel("models/odessa.mdl" )
end
end)
end
hook.Add('Initialize', 'loadNpcs', function()
if file.Exists('npcpos.txt', "DATA") then
spawnSavedNpcs(util.JSONToTable(file.Read('npcpos.txt', "DATA")))
else
file.Write('npcpos.txt', util.TableToJSON({}))
end
end)
concommand.Add('savenpc', function()
local npcs = {}
for _, npc in ipairs(ents.FindByClass('ee_npc')) do
npcs[#npcs + 1] = { pos = npc:GetPos(), ang = npc:GetAngles() }
end
file.Write('npcpos.txt', util.TableToJSON(npcs))
end)[/code][/QUOTE]
What even.
[B]EDIT:[/B] Nvm
Sorry, you need to Log In to post a reply to this thread.