Survival NPC gamemode: how do I start

So, I’ve been playing awesome gamemodes and addons too much. I want to create my own.

**The cons: **

  • I don’t know shit about coding. Javascript, MS-DOS and HTML basics, not more.

The pros:

  • I know exactly what I want to do.
  • I’m not stupid. I know this will be hard and I will have patience. I plan on divide this in small objetives that, in the end, should cooperate and work together to have this gamemode functioning.
  • I have time to work in this.

As you see, the cons overweight the pros. My solution to that unbalance between them is this thread. I ask you for help. I don’t ask for written code (I’m not that stupid), I only need you people to tell me where to look, tutorials, similar ideas that other people did, wikias, etc. I need you to guide me.

This thread would be about the development of this gamemode (until it’s playable, then I would make a thread in ‘Gamemode & Addon Releases’) , plus me asking you about the problems I face. With time, I would have experience with this and would be more and more independent.

If it was a mistake making this thread, sorry. I’m only asking for all forms of help coding a gamemode for Garry’s Mod 13. If this is not the correct way to do this, tell me how I should do it.

About the gamemode

In short terms, my gamemode would be a NPC survival. Like Reasons To Kill, or Vein, or nZombies, or these ‘Combine-coming-to-kill-you’ specialized maps. The final features would be:

-Survive the maximum time possible against the Squad of Combines that is trying to find and kill you.

Gameplay features:
-Able to play in every map (previously configured a la nzombies)
-Custom NPC weapons.
-Enemies will operate in Squads, this should encourage stealth.
-Weapons, Ammo & health dispersed on maps. You start with nothing and will have to find them.
-Combines will search for ammo, medkits and other goods like you.
-Over time, certain events will happen. Zombie hordes or enemy backups, for example.
-The number of enemies will not be fixed. At start, for example, could be you vs 20 combines, but until you kill at least 18 of them, it won’t spawn more squads.
-If the Combine find X weapon, they will use it. (If a combine finds an RPG, it’s current weapon will change and be an RPG)

As you see the idea is very ambitious. I know it will be hard, but I think it’s posible, too. I’ve seen a lot of gamemodes and addons that have this features (one or two per addon/gamemode) or similar features.

I’m basing all of this on the actual Combine AI (and their ‘squads’ and ‘search mode’), the default GMod items (ammo, medkits, weapons) and a not-too-complex spawning system. I only need to control the Combine right with proper coding. If I can have them searching for a item, this project should be possible.

Actual objetives

  • My first goal is setting the gamemode. This seems “easy”, it seems that I only have to follow this guide.
  • Then I want to spawn a combine on the map and make him enter in ‘search’ mode. I know Combines have that, I’ve seen them searching the area looking for me in other ocassions.
  • Then I want to spawn an entire squad and make them do the same.
  • Then I want to expand the area they use to search, so they can explore the entire map looking for the player.

Other objetives:

  • Strip the player weapons.
  • Spawn weapons in a map.

Until I encounter a problem I can’t solve myself, your thoughts and advice about this are gold to me right now.


local ent = ents.Create( weapon_classname )
ent:SetPos( pos )
the wiki is your friend, also for AI you may want to look into the nextbot library

This post has some helpful questions and answers.

Look for some of my posts; I post a lot of tutorials I wrote to help answer questions users have.

Oh, I also made a simple base-gamemode; contact me if interested.

I think it’d be a lot better for him to just use normal NPCs. Nextbot isn’t exactly for beginners, and normal NPCs can be controlled pretty easily.

Thanks, people. I appreciate this.

I prefer using the combine as the enemies, they can do any of the tasks I need for the enemies of this gamemode.

Like this?

local ent = ents.Create( weapon_ar2 )
ent:SetPos( 882 740 -16)

for now, it doesn’t spawn anything.
I get an error,

[ERROR] gamemodes/combinesurvival/gamemode/init.lua:8: ')' expected near '740'
  1. unknown - gamemodes/combinesurvival/gamemode/init.lua:0

Couldn't Load Init Script: 'combinesurvival/gamemode/init.lua'

I’ve set up the gamemode properly, now I can select and play it. I stripped the player weapons, gave him a weapon and removed all of his ammo when he dies.
I’ve set the player model, too.

But it stands like this all the time,[/T]

and doesn’t have any hands.

I guess it’s something I haven’t configured. I’m looking the .lua of the other gamemodes, but I don’t find anything that could help me.

No, like this:

local ent = ents.Create( "weapon_ar2" )
ent:SetPos( Vector( 882, 740, -16 ) )

The animations should be fixed as soon as you fix your Lua errors.
About hands, here’s an article:

As mentioned above, wiki is your best friend. Search it before posting.

I searched first, and found that article, but I didn’t even know where to put these codes. Later on it worked, by trial and error.

Thanks for the example, that’s what I need. I didn’t knew positions were vectors nor they were separated by commas.

Uh, on that page it says where to put all the stuff exactly.

For you, experienced coders, maybe. But for me it only reads ‘Serverside’ or ‘Clientside’, that doesn’t mean much for someone who doesn’t know what files are ‘serverside’ or ‘clientside’. After some research, now I know that the ‘Serverside’ it’s the init.lua and Clientside it’s the cl_init.lua. The player has his hands now.

I repeat, I don’t know anything about coding than some javascript. I know even less for gmod coding.

Well, now I have the player, the enemies and the items. All I have to do now it’s figuring out how to give weapons to the enemies and have them going to ‘search’ mode.

(And setting some triggers to have them spawning at the proper time and places)

You can use ply:Give(“weapon”) with a PlayerSpawn hook to give them the weapon when they spawn.

So, NPCs count as players?

I don’t know what’s a hook and the wiki seems to assume that I know what it is. This will take some time.

Anyway, I was thinking on something along the lines of

this player is npc_combine_s
then give 'self' weapon_ar2
this player it's a real player
then give 'self' weapon_pistol

is it possible? It would be even better if I could name the NPCs that I spawn so I can separate them and give x NPC a certain weapon.

Anyway I guess that’s impossible until I learn some more.

Actual goals:
-Arm the NPCs that spawn with the codes Code_gs mentions.
-Make these NPCs wander around

No, NPCs are not players:
They just have some common methods. To avoid confusion, etc.

When you create your NPCs, just do myNpcVariable:Give(“weapon_ar2”) on them.

code_gs gave you examples for arming the player.

Hooks ( aka Callbacks ) are what engine calls in Lua to do specific actions. If you need more info, I am sure googling ‘callbacks’ will give you some.

It also might be useful for you to search for GMod development tutorials on youtube, I am sure I have seen some for developing gamemodes too, but be careful, there are some for older version of GMod.

I am not sure what about ‘wandering about’, HL2 npcs aren’t very dynamic in that regard, they will be able to know where you are exactly no matter where you go once they see you in most cases.

I don’t think you understand what it means, yes init.lua is the script that gets called by default when making a gamemode, but besides init.lua, it’s not a client side script or server side script unless you make it one, you could make a .lua called sv_player but make it client if you wanted to.

They seem to know your exact position once they see you, then chase you until you are like 10 seconds hid. Then they seem to ‘forget’ your position, THEN they start to patrol a tiny area looking in all directions. You can see this behavior spawning a combine and using noclip to hide for like 10 seconds, then enabling ai_disable and looking.

I want that behavior, but in a expanded range. If it’s not possible, then I need to find a way to order them going through a random waypoint (within a range) every x seconds.

So I can make more .lua and ‘call them’ (or their functions) from cl_init.lua and init.lua?
This way everything could be more organized

Again, thanks for your feedback, people, when I can have combines patrolling on the map, it could be at least playable.
The first version will be simple, respawning weapons in random places, respawning combines in prefixed “entrances” to the map…
Yet it will be functional!

One of the main things I need to do it’s change the accuracy and damage dealt by the Combine, they’re apes at aiming. I know it’s possible, the ‘NPC Weapons Pack 2’ does this. It’s matter of looking in that code.

(That or the easy way: reducing the max health of the player to 10


Alright, I don’t know how to continue.

This piece of code spawns a npc, gives him a weapon, makes him wander and sets a timer that calls a new function that SHOULD make THAT npc wander around every x seconds.

function GM:PlayerInitialSpawn( ply )
local ent = ents.Create( "npc_combine_s" )
ent:SetPos( Vector( 2139.246094, -791.463989, -79.968750) )
ent:SetNPCState ( 2 )
ent:SetLastPosition( ent:GetPos() )
ent:SetSchedule( SCHED_IDLE_WANDER )
timer.Create( "makenpcwander", 5, 0, morewander ) 

function morewander()
ent:SetSchedule( SCHED_IDLE_WANDER )

How do I ‘give a class’ (html style class, a “group” if you prefer) to the npc I just spawned?
I need it so morewander knows what NPC (or group of NPCs) it will make wander around.

If I remove ‘local’ from the second line, it works, but I know it’s not the best idea to have a variable for every NPC. I need to know how to give that NPC a class so it is part of a group I can command with a single line.

And a way to give them a name would be useful, too. I guess that my current way it gives that NPC the name ‘ent’ it’s not the best way to do it.

For example, let’s say I spawn 3 npc_combine_s with an smg, and 4 npc_combine_s with an ar2 rifle. I want the SMG ones to go to x point, and the AR2 ones to attack the player.
I need a way to define these two groups, so I can control them separately.

I’m happy to say that I finally achieved what I wanted. At least, the base for it.

  • NPCs spawning and wandering around
  • NPCs sharing their info about you, AKA if an NPC sees you, other NPCs will lock you as target and will come for you. I’m looking for a way to have the ‘backup’ sometimes going to their allies, not always directly to you.

The rollermines are so funny when they come for you from far away. They don’t run, they simply roll heavily coming slowly to you. And when you have killed their allies, they simply switch back to ‘wander’ mode without even noticing you.