How does the "maps" system work with spawn functions?

Hello. I’ve been working on a zombie survival gamemode, only problem is, I need to add supported maps instead of supporting one map, making them all spawn at 0, 0, 0.
I know the perfect system for this. However, I need help actually getting it to work with my zombie spawn function(READ: below is a combine spawn function used as an example)
If you know how I could get this to work but don’t know about the “maps” system then this is how it works:
You create a folder called “maps” in your gamemode files. Inside the maps folder, you create .lua files name like so “map_namehere.lua”. Inside the .lua files, you add functions to be run when the player is playing on that map. I need an expert on this system to help me out with getting my zombie(combine, for the example) spawn function to work. I know how to use vectors and add them, btw, but I’m really confused with getting the maps to work.
Example combine spawn code:


function SpawnCombineElite1( pos )
    NPC = ents.Create( "npc_combine_s" )
    NPC:SetKeyValue("NumGrenades", "0") 
    NPC:SetKeyValue( "model", "models/Combine_Super_Soldier.mdl" )
    NPC:SetPos( pos )
    NPC:SetKeyValue( "spawnflags", 768 )
    NPC:Spawn()
    NPC:Give( "ai_weapon_ar2" )
    NPC:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_GOOD )
    NPC:Fire("StartPatrolling","",0)
end

function CombineWave()
timer.Simple(2,CombineWave)
if CountEntity("npc_combine_s") < 30 then
 SpawnCombineElite1(Vector(0,0,0)) --I also am confused as to what to do with this, should I replace "Vector" and the numbers and parentheses around it with "pos"?
end
end

function CountEntity(ent)
local entities=0
for k, v in pairs(ents.GetAll()) do
if v:GetClass() == tostring(ent) then
entities=entities+1
end
end
return(entities)
end

First of all, hardcoding NPC spawn locations is not the best option.

To answer your “maps” question, game.GetMap() will return name of the current map.
Using this, you can do something like include(“maps/” … game.GetMap() … “.lua”) in your gamemode or something.

As for vectors, here’s an example:



local spawnpoints = {
	Vector( 0, 0, 0 ),
	Vector( 200, 0, 0 ),
	Vector( 0, 1000, 0 ),
	Vector( 0, 0, 100 ),
}

SpawnCombineElite1( spawnpoints[ math.random( 1, #spawnpoints ) ] )

This code will spawn a combine soldier at one of the 4 positions given in the table spawnpoints.

[lua]

local SuportedMaps = {
[“gm_mymap”] = {
Vector(0,0,0),
Vector(0,0,0)
},

["gm_mymap2"] = {
	Vector(0,0,0),
	Vector(0,0,0)
}

}

function GetSuportedSpawnPoints( )
local currentmap = game.GetMap()
if SuportedMaps[currentmap] then
return SuportedMaps[currentmap]
end
return false
end

function CombineWave()
timer.Simple(2,CombineWave)
if CountEntity(“npc_combine_s”) < 30 then
local Spawns = GetSuportedSpawnPoints()
if Spawns then
for _,pos in pairs( Spawns ) do
SpawnCombineElite1(pos) --I also am confused as to what to do with this, should I replace “Vector” and the numbers and parentheses around it with “pos”?
end
end
end
end
[/lua]

Something like this would be more suited I think.

[editline]17th April 2015[/editline]

Also, this is a better way of counting entities:

[lua]
function CountEntity(class)
return #ents.FindByClass( class )
end
[/lua]

This would work if I put it in init? Thanks!

Yup.

game.GetMap doesn’t return the file extension, so when adding maps to the table, don’t include the “.bsp”


function CountEntity(class)
	return #ents.FindByClass( class )
end

function SpawnCombineElite1( pos )
    NPC = ents.Create( "npc_combine_s" )
    NPC:SetKeyValue("NumGrenades", "0") 
    NPC:SetKeyValue( "model", "models/Combine_Super_Soldier.mdl" )
    NPC:SetPos( pos )
    NPC:SetKeyValue( "spawnflags", 768 )
    NPC:Spawn()
    NPC:Give( "ai_weapon_ar2" )
    NPC:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_GOOD )
    NPC:Fire("StartPatrolling","",0)
end

local SupportedMaps = {
	["gm_construct"] = {
		Vector(0,0,0),
		Vector(0,0,0)
	},

	["rnh_corridors"] = {
		Vector(0,0,0),
		Vector(0,0,0)
	}
}

function GetSupportedSpawnPoints( )
	local currentmap = game.GetMap()
	if SupportedMaps[currentmap] then
		return SupportedMaps[currentmap]
	end
	return false
end

function CombineWave()
	timer.Simple(2,CombineWave)
	if CountEntity("npc_combine_s") < 30 then
		local Spawns = GetSupportedSpawnPoints()
		if Spawns then
			for _,pos in pairs( Spawns ) do
				SpawnCombineElite1(pos) --I also am confused as to what to do with this, should I replace "Vector" and the numbers and parentheses around it with "pos"?
			end
		end
	end
end


Weird. it isn’t working for me…
Oh wait, is _,pos supposed to be something else?

Last two functions can be made into:


function GetSupportedSpawnPoints( )
	local currentmap = game.GetMap()
	if !SupportedMaps[currentmap] then return {} end
	return SupportedMaps[currentmap]
end

function CombineWave()
	timer.Simple(2,CombineWave)
	if CountEntity("npc_combine_s") < 30 then
		for _,pos in pairs( GetSupportedSpawnPoints() ) do
			SpawnCombineElite1(pos) --I also am confused as to what to do with this, should I replace "Vector" and the numbers and parentheses around it with "pos"?
		end
	end
end

And remember, “isn’t working for me” is NEVER helpful for anybody if you seek help.
Tell us exactly what is happening.

Works for me? Are you calling CombineWave() in InitPostEntity serverside?

Sorry, heh. Well, I got no errors. However, no combine spawned.

[editline]17th April 2015[/editline]

I’m not calling it in InitPostEntity, what I’m doing is just using it in plain init.lua

You have to actually call the CombineWave function in order for the “waves” to start.


function CountEntity(class)
	return #ents.FindByClass( class )
end

function SpawnCombineElite1( pos )
    NPC = ents.Create( "npc_combine_s" )
    NPC:SetKeyValue("NumGrenades", "0") 
    NPC:SetKeyValue( "model", "models/Combine_Super_Soldier.mdl" )
    NPC:SetPos( pos )
    NPC:SetKeyValue( "spawnflags", 768 )
    NPC:Spawn()
    NPC:Give( "ai_weapon_ar2" )
    NPC:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_GOOD )
    NPC:Fire("StartPatrolling","",0)
end

local SupportedMaps = {
	["gm_construct"] = {
		Vector(0,0,0),
		Vector(0,0,0)
	},

	["rnh_corridors"] = {
		Vector(0,0,0),
		Vector(0,0,0)
	}
}

function GetSupportedSpawnPoints( )
	local currentmap = game.GetMap()
	if !SupportedMaps[currentmap] then return {} end
	return SupportedMaps[currentmap]
end

function CombineWave()
	timer.Simple(2,CombineWave)
	if CountEntity("npc_combine_s") < 30 then
		for _,pos in pairs( GetSupportedSpawnPoints() ) do
			SpawnCombineElite1( pos ) --I also am confused as to what to do with this, should I replace "Vector" and the numbers and parentheses around it with "pos"?
		end
	end
end


Again, the whole spawn code.

[editline]17th April 2015[/editline]

Ohhhhhh, right. I wasn’t thinking clearly :P. Let me try calling it then

[editline]17th April 2015[/editline]

It works now! Thanks for the help, guys.