Spawning NPC in my gamemode

Hi, I’m having a bit of trouble having npcs spawn at the start of the gamemode. I know i need them in the “info_player_counterterrorist” spawnpoints. I tried some other codes and they all failed can anyone help?

the code

DeriveGamemode( “fretta13” )

IncludePlayerClasses()

GM.Name = “Deathmatch Base”
GM.Author = “”
GM.Email = “”
GM.Website = “”
GM.Help = “Simple Deathmatch”

GM.GameLength = 30
GM.MaximumDeathLength = 1
GM.MinimumDeathLength = 1
GM.RealisticFallDamage = true
GM.EnableFreezeCam = true
GM.SelectColor = true
GM.ShowTeamName = false
GM.NoPlayerTeamDamage = false
GM.FragLimit = 20

function GM:CreateTeams()

team.SetUp( 1, "Deathmatchers", Color( 70, 230, 70 ), true )
team.SetSpawnPoint( 1, "info_player_terrorist" )
team.SetClass( 1, { "Deathmatcher" } )

end

local spawn = table.Random( ents.FindByClass( “info_player_counterterrorist” ) )

local zombie = ents.Create( “npc_zombie” )
zombie:SetPos( spawn:GetPos() )
zombie:Spawn()

What errors are you having?

You aren’t “required” to spawn them at those points. Those are called “point” entities, they’re literally a text file with ENT.Type = “point”; or whatever…

You can define your own vector locations to spawn entities.

First, I’d recommend spawning your npcs in game. Take a look at my dev_spawnent command: https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_npc_jointeam_example.rar

Then, run this code:
-snip- CHECK LINK AT BOTTOM FOR COMPLETE CODE!

Untested, should work… Basically spawn your npc_whatever around the map in sandbox. Position them and angle them. Then type dev_buildspawns npc_kleiner or whatever the name is. It will create a txt file in your data folder that will work on the current map and spawn the npcs. IT ONLY sets the CLASS, the POSITION and the ANGLE, nothing else. Make sure the entity handles that part. It’s simple…

EDIT: Tested and working. Example output:

EDIT2: ADDED MODEL, just in case…


if ( game.GetMap( ) != string.lower( "rp_evocity_v33x" ) ) then return; end

local EntitySpawnPoints = {
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 6118, -3841, 47 ), ang = Angle( 0, 0, 0 ) };
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 6118, -3741, 47 ), ang = Angle( 0, 0, 0 ) };
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 6118, -3641, 47 ), ang = Angle( 0, 0, 0 ) };
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 6118, -3541, 47 ), ang = Angle( 0, 0, 0 ) };
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 6118, -3478, 47 ), ang = Angle( 0, 0, 0 ) };
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 6058, -3478, 47 ), ang = Angle( 0, 0, 0 ) };
	{ ent = "prop_physics_multiplayer", model = "models/props/cs_assault/pylon.mdl", pos = Vector( 5999, -3478, 47 ), ang = Angle( 0, 0, 0 ) };
};

hook.Add( "InitPostEntity", "SpawnEntitiesOnMapLoad:prop_physics_multiplayer", function( )
	for k, v in pairs( EntitySpawnPoints ) do
		local _ent = ents.Create( v.ent );
		_ent:SetPos( v.pos || vector_origin );
		_ent:SetAngles( v.ang || angle_zero );
		_ent:SetModel( v.model );
		_ent:Spawn( );
		_ent:Activate( );
	end
end );


Save as file: https://dl.dropboxusercontent.com/u/26074909/tutoring/entities/spawn_entities_on_map_start.lua.html Remove .html to view .lua

Simply type “dev_buildspawns npc_kleiner” as an example, no quotes needed.

Hey thanks so far with all your help. I forgot to mention however when i now launch the gamemode to test it gmod instantly closes at “Loading resources” I found out that if I remove these line of code

local spawn = table.Random( ents.FindByClass( “info_player_counterterrorist” ) )

local zombie = ents.Create( “npc_zombie” )
zombie:SetPos( spawn:GetPos() )
zombie:Spawn()

It dosen’t crash. I put that line of code at the end of my shared.lua I need help to see why it is crashing. I need to have the zombies to spawn once on a map. however if anyone can help me make it not crash and use pos = vector then theat would be awsome. I’m a bit new to this so as much help will be awesome

Make sure you spawn them in InitPostEntity, also call Activate on the zombie; you may need to set the model too.

If you use my example, you can simply go into sandbox, disable ai and spawn them where ever you want, then use “dev_buildspawns npc_zombie” without the quotes and it’ll create the Lua for you to put into lua/autorun/server or addons/acecool/lua/autorun/server/ as a .lua file ( rename the EntitySpawnPoints.txt file it creates in the data/ folder to something like sv_spawn_npc_zombie.lua… ) and they’ll spawn once per map in your designated locations in the proper hook.

Hey Acecool is there a way we can instant chat or something. I tried what u said and it said “unknown command dev_buildspawn” my steam name is ncc360 if u wanna use steam chat

Sure, click the Steam icon under my name and add me.

The command is dev_buildspawns <ent_name>. not dev_buildspawn

Okay i sent the friend request

ok so far it’s almost working. I got the zombie to spawn but i’m trying to add an npc that’s from an addon and it’s not spawning.

this is the code

function GM:InitPostEntity( )
local ent = ents.Create(“toy_chica_run”)
ent:SetPos(Vector(-3363.335693, 103.444580, -239.968750))
ent:Spawn()
end

it works with the zombies but not with that npc. any help?