Tutorial: NPC Scripting Basics Last edited 10 years ago2008-02-25 10:43:05 UTC by doodle doodle

This article was converted from a previous version of TWHL and may need to be reviewed

  1. The formatting may be incorrect due to differences in the WikiCode processing engine, it needs to be revised and reformatted
  2. Some information may be out of date
  3. After the article is re-formatted and updated, remove this notice and the Review Required category.
  4. Some older tutorials are no longer useful, or they duplicate information from other tutorials and entity guides. In this case, delete the page after merging any relevant information into other pages. Contact an admin to delete a page.

Download attachment

Getting Started

The use of scripted_sequences in maps is one of the things that make single player mapping that much more versatile. Scripted_sequences are used to control npc characters and give them animations; it can used to breathe life into an otherwise still scene.

For this example, we'll be making two npc_citizens carry out their everyday existence. The first one will walk to the couch and sit on it while the other one will try to get a TV working.

Firstly, create a room. Make it a reasonable size; mine was 256x448 (on top view).
Place an info_player_start and a few light entities in the room. Now (for the benefit of my example), place a couch in the room, and a TV on a table opposite it.
A nice little sceneA nice little scene

Choosing the Animations

Now we're going to set up our scripted sequences. Firstly, let's see what animations we want the NPC's to have. Place an npc_citizen somewhere in the room, where is not important at the moment. Go into the properties and click the Model tab. Now go through the sequences, click a few to see what they look like. This is the animations library, we need the names of the animations we like to put in the scripted_sequence.

Okay, so for this example I want to use the Idle_to_Sit_Chair, you can go and find this to see a demonstration.
Look, he's alive!Look, he's alive!
Now name the npc. For my example set the name to npc_bob, and cut and paste him into a corner (moving them normally after setting up an animation in the model tab can crash hammer, setting it back to Sequence: ragdoll can prevent this).

Now we want to place the scripted_sequence entity. Select it from the entity list and place it on the floor right in front of the couch (it's important not to place it in the couch. Scripts don't work inside props or other entities). The small yellow line in the top view shows which way the scripted_sequence is facing: you want it facing toward the TV. To change this line, go to the scripted_sequence properties and change the angle.
Yellow line is in top left viewYellow line is in top left view

Setting up the scripted_sequence

The fun part: go to the scripted_sequence properties and make the Target Npc npc_bob. We'll set the entry animation to Idle_to_Sit_Chair. Set the action animation to Sit_Chair and set the post action animation to Sit_Chair. Move to Position should be set to Walk. You can also make him run or go to the sequence instantly. Go to the flags tab and tick Override AI, Loop in Post idle and Priority Script. This basically means the NPC will do this despite distractions and will continue sitting, even after the script is finished.

Testing the sequence

Name the scripted_sequence script_bobsit. Now, create a func_button on the wall somewhere and set the Output to: Onpressed, Target entity: script_bobsit, Via this output: Begin Sequence. Now would be a good time to test, so compile and load it up. Press the button and see if the npc does the sequence.
Woo, buttonWoo, button

Part 2: Fixing the TV

Now we're going to set up the other npc, the one that'll try to get the TV working.

Place another npc_citizen in another corner of the room. Name this one npc_paul. Now place a scripted_sequence in front of the TV you placed earlier, and set the angle to face the TV. Set the Target NPC to npc_paul. Set the Entry Animation to standtocrouchhide, set the action animation to crouchidlehide and the Post action idle Animation to crouchhidetostand. This time however, the npc will not be looping the animation, in which case the only flags you need to tick are override AI and Priority Script. Tick the Repeatable flag as well.

Name the scripted_sequence script_paulfix and place another button. This button will be: OnPressed, Target Entity: script_paulfix, Via input: BeginSequence.

Conclusion

If you've followed this correctly, you should be able to get both npc's going by pressing their individual buttons. If you ticked the Repeatable flag, you can press the TV guy's button more than once. Compile and test.

Of course, this is just a starting point for what you can do with scripted_sequences. I hope this gives you many ideas and has taught you how to use them effectively.
Full room layoutFull room layout
In actionIn action

7 Comments

Commented 10 years ago2008-02-27 14:37:00 UTC Comment #100838
very nice indeed.

One thing though, should it not be "breathe" rather than "breath" at the beginning?
Commented 10 years ago2008-03-07 21:39:16 UTC Comment #100839
You wrote "bo" instead of "bob" at the start. ("For my example set the name to npc_bo")
Commented 10 years ago2008-03-22 16:25:42 UTC Comment #100840
both of those fixed, thanks guys
Commented 10 years ago2008-09-03 19:57:18 UTC Comment #100841
thanks!!!
Commented 10 years ago2008-11-24 23:44:09 UTC Comment #100842
a problem... I haven't got those animations. Iv only got idle_smg1 and other guns like 400times and that's it.. ^^
Commented 9 years ago2009-01-01 15:30:54 UTC Comment #100843
Do its same on GoldSource?
Commented 9 years ago2009-03-04 03:31:22 UTC Comment #100844
When i tried this for the map im making it crashes HL2, and I found ou its the sequence cuasing it....how do I fix it?

You must log in to post a comment. You can login or register a new account.