Spawning items from a text file

I have basic spawn points set up so that I can spawn weapons around the map at predetermined locations. The problem, I want every map to have different spawn points so that each can be unique and I won’t be limited to just one map. I know of file/Read. But i’m not sure how to use it so that the coordinates saved in the text file will create new spawn locations for my weapons. Thank you in advance for taking time to check this out, and possibly help me achieve this.

You could go about parsing the text file yourself or using the function util.JSONToTable

Here’s a JSON example



[
    {
        "map": "whatever_name_1",
        "points": [
            {
                "x": "0",
                "y": "0",
                "z": "0"
            },
            {
                "x": "0",
                "y": "0",
                "z": "0"
            },
            {
                "x": "0",
                "y": "0",
                "z": "0"
            }
        ]
    },
    {
        "map": "whatever_name_2",
        "points": [
            {
                "x": "0",
                "y": "0",
                "z": "0"
            },
            {
                "x": "0",
                "y": "0",
                "z": "0"
            },
            {
                "x": "0",
                "y": "0",
                "z": "0"
            }
        ]
    }
]


So basically your text file would just contain json data ( more convenient to just make it a .json file )

I must be a special kind of stupid lol. I’m actually new to lua. I’m not sure how to setup what you have given me. I believe I understand the concept, just unsure of how to execute it :confused:

The function he gave you showed you an direction, read through the stickies to learn GLua.

I managed to get this bit of code working


local folders = {"weapons", "ammo"}

for k, v in pairs(folders) do

	file.CreateDir("br/spawns/"..v)

	if !file.Exists("br/spawns/"..v.."/"..string.lower(game.GetMap())..".txt", "DATA") then
	
		file.Write( "br/spawns/"..v.."/"..string.lower(game.GetMap())..".txt", util.TableToJSON({}, true) )
		
	end
	
end

It creates the files using util.TableToJSON, i’m still uncertain on how to get my code to read the x,y,z positions from the .txt it creates.

theres a revert function…

util.JSONToTable
use the wiki.

Okay so I get no errors or anything but when I join the game nothing is spawned here is what I have.

wepspawning.lua



local items = {"m9k_deagle", "m9k_ak47", "m9k_m416", "m9k_fal", "m9k_scar", "m9k_winchester73", "m9k_hk45", "m9k_thompson", "m9k_m92beretta", "m9k_colt1911", "m9k_colt1911", "m9k_vector", "m9k_m24", "m9k_dragunov", "m9k_g36", "m9k_famas", "m9k_acr", "m9k_ak74", "m9k_l85"}

local folders = {"spawns"}

LootVectors = {}
LootVectors[string.lower(game.GetMap())] = {}

for k, v in pairs(folders) do

	file.CreateDir("br/spawns/"..v)

	if !file.Exists("br/spawns/"..v.."/"..string.lower(game.GetMap())..".txt", "DATA") then
	
		file.Write( "br/spawns/"..v.."/"..string.lower(game.GetMap())..".txt", util.TableToJSON({}, true) )
		
	end
	
	if file.Size("br/spawns/"..v.."/"..string.lower(game.GetMap())..".txt", "DATA") == 3 then
		
	else
		local config = util.JSONToTable( file.Read("br/spawns/"..v.."/"..string.lower(game.GetMap())..".txt", "DATA") )
		
		if not istable(config) then
			return
		end

		LootVectors[string.lower(game.GetMap())][k] = config
	end
	
end

function GM:InitPostEntity( )
	if SERVER then
	
		local pos = table.Random( LootVectors[ string.lower( game.GetMap() ) ] )
		if !pos then return end
		
			local ent1 = ents.Create(table.Random( items ))
				ent1:SetPos( pos ) ;
				ent1:Spawn()
				
	end
end



This is my current .txt file to hold the vectors


366.92 -90.96 -84.02

Can anyone see what is going wrong?

You coded it very complicated. There are simpler ways to just store EVERYTHING in a text file.

[editline]22nd August 2015[/editline]
Alright heres a working code that sets the pos, which are configureable from the map, note its only tested on flatgrass, but should work.



local items = {
	{
		item 	= "weapon_crossbow",
		pos  	= {
			gm_flatgrass = Vector(64, -576, -12223),
			gm_construct = Vector(0, 0, 0)
		},
	},

	{
		item 	= "weapon_357",
		pos 	= {
			gm_flatgrass = Vector(169, -501, -12223),
			gm_construct = Vector(0, 0, 0)
		}
	}
}

if !file.Exists("items.txt", "DATA") then
	file.Write("items.txt", util.TableToJSON(items, true))
end

local data = util.JSONToTable(file.Read("items.txt", "DATA"))

hook.Add("InitPostEntity", "spawnitems", function()
	for _, v in pairs(data) do
		local ent = ents.Create(v.item)
		ent:SetPos(v.pos[game.GetMap()])  -- Sets the pos according to the mapname
		ent:Spawn()
	end
end)


[editline]22nd August 2015[/editline]

if you want to make multiple pos’ per map which are randomly chosen, just change the mapname = vector to mapname = table, and put vectors in there, then change setpos to an table.Random check.

[editline]22nd August 2015[/editline]

and heres the items.txt




{
	"1": 
	{
		"pos": 
		{
			"gm_construct": "[0 0 0]",
			"gm_flatgrass": "[64 -576 -12223]"
		},
		"item": "weapon_crossbow"
	},
	"2": 
	{
		"pos": 
		{
			"gm_construct": "[0 0 0]",
			"gm_flatgrass": "[169 -501 -12223]"
		},
		"item": "weapon_357"
	}
}


[editline]22nd August 2015[/editline]

Yes I also noticed somehow that the vectors are fucked up, but what ever.

Strange error here.


local items = {
	{
		item 	= "m9k_ak47",
		pos  	= {
			gm_flatgrass = table{366.92 -90.96 -84.02},
			gm_construct = table{366.92 -90.96 -84.02}
		},
	},

	{
		item 	= "m9k_deagle",
		pos 	= {
			gm_flatgrass = Vector(169, -501, -12223),
			gm_construct = table{400.92 -90.96 -84.02}
		}
	}
}

if !file.Exists("items.txt", "DATA") then
	file.Write("items.txt", util.TableToJSON(items, true))
end

local data = util.JSONToTable(file.Read("items.txt", "DATA"))

hook.Add("InitPostEntity", "spawnitems", function())
	for _, v in pairs(data) do
		local ent = ents.Create(v.item)
		ent:SetPos(table.Random(items))
		ent:Spawn()
	end
end


[ERROR] gamemodes/br/gamemode/sv_weaponspawning.lua:25: unexpected symbol near ')'

learn to copy and paste, you closed the hook on line 25.

Thank you for the assistance.