Last updated: 06/10/2024, 02:17

Current Project: Rejuvenation of Hellkeeper.net

Unreal 2 AI Scripts

This tutorial will get you started with AI scripting by creating a simple .u2s script and explaining what a few of the .u2s commands do.

Basic scene

Preparation

Create the directory for your .u2s scripts (see the related tutorial if you have problems), we're calling this one "u2stutorial".
You can download the level used in this tutorial. It's a simple room with 4 PathNodes, a PlayerStart, a Light and a light marine. I've set the level's MapName to "u2stutorial" to correspond to the directory you've created.

Play the level. The marine is walking around randomly (he's autonomous), because he doesn't have an AI script yet. In this condition the underlying AI code takes control of the pawn and he reacts to all surrounding stimuli (seeing or hearing an enemy, etc.)

Create an empty text file and save it as "marine01.u2s" in your "/Scripts/u2stutorial" directory. This is the AI script that the marine will use. Make sure your text editor doesn't try to append a .txt extension when you save the file.

Select the marine and open his properties. Open the AI tab and look for the "CommandFileName" slot. Enter "marine01" (without any extension) and save the map. You have now set the marine's AI script.

A Simple Patrol

Add the following lines to the "marine01.u2s" file and save it:

sleep 2
gotoactor PathNode0
gotoactor PathNode1
sleep 2
gotoactor PathNode2
agentcall Event_U_Wave 1
gotoactor PathNode3
sleep

Play the level and see what happens. When you start the level, the marine stands still for 2 seconds, then runs to PathNode0, then PathNode1 (where he waits for another 2 seconds), then to PathNode2 (where he plays the animation "Event_U_Wave"). Then he runs to PathNode3, where he waits indefinitely.

Let's take a look at the commands used in our example (parameters in () brackets are required, parameters in [] brackets are optional):

Labels

You can create labels and jump to them, for example to create a looping sequence of actions.

Add two more lines to the beginning and end of your script and delete the final sleep statement, so that it looks like this:

:MarinePatrol
sleep 2
gotoactor PathNode0
gotoactor PathNode1
sleep 2
gotoactor PathNode2
agentcall Event_U_Wave 1
gotoactor PathNode3
gotolabel MarinePatrol

Play the level and see what happens. You define labels with the : character and jump to them with the "gotolabel" command.

You can also call labels. The script will jump to the specified label and then jump back to the line after the call statement when it encounters a return:

:WaitABit
sleep 5
call PatrolThisArea
sleep 3
gotolabel WaitABit

:PatrolThisArea
gotoactor PathNode0
gotoactor PathNode1
return

This makes the NPC wait 5 seconds, go to PathNode0 and PathNode1, wait 3 seconds and start all over.

Messages And Debugging

To better see what's going on, you can print out messages. They will only appear on screen if you've enabled the debug message area, though.

Modify your script again so that it looks like this:

:MarinePatrol
message "sleeping 2 seconds"
sleep 2
message "going to pathnode 0, then PathNode1"
gotoactor PathNode0
gotoactor PathNode1
message "sleeping 2 seconds"
sleep 2
message "going to pathnode 2"
gotoactor PathNode2
message "waving!"
agentcall Event_U_Wave 1
gotoactor PathNode3
message "starting over"
gotolabel MarinePatrol

When you play the level, the script will print a note announcing what the marine is going to do. You have to print the message of what the NPC is about to do before you tell him to do it.

An easier way to follow a script is using the game's debug mode. You can read more about it in the debugging tutorial. Delete all text messages from the script and add the line "debugmode 11" at the very beginning, before the :MarinePatrol label. Play the game and see what happens.

Some Additional Commands

Here's some additional handy (or rather, random) script commands (indicated by "->", do not add those to your actual script):

Modify your script again so that it looks like this:

:MarinePatrol
sleep 2
gotoactor PathNode0
-> turntoactor Light0
-> fire 2
-> firealt 2
gotoactor PathNode1
sleep 2
-> setmovespeed 0.5
gotoactor PathNode2
-> setmovespeed 1
agentcall Event_U_Wave 1
gotoactor PathNode3
gotolabel MarinePatrol

Events

Events are an important tool for scripting AI behavior, here's a quick inroduction:

There's already a Trigger in the level that sends the event "ChangeMarinePatrol". To be able to trigger an NPC, the bTriggerNPCs flag (under "Trigger") has to be set to true. The marine won't react to the Trigger yet because he hasn't been told to. We'll change that now.

I've simplified the existing script a bit for. Delete all lines and enter the following commands:

:MarinePatrol
ontrigger ChangeMarinePatrol gotolabel MarinePatrol2
gotoactor PathNode0
gotoactor PathNode1
sleep 2
gotolabel MarinePatrol

:MarinePatrol2
ontrigger ChangeMarinePatrol gotolabel MarinePatrol
gotoactor PathNode2
gotoactor PathNode3
sleep 2
gotolabel MarinePatrol2

Play the game and see what happens when you touch the Trigger. The marine loops through the MarinePatrol label until he receives the ChangeMarinePatrol event from the level, at which point he loops the MarinePatrol2 part of the script.

Tutorial by Matthias Worch

© 2005-2026, by Hellkeeper.

Valid XHTML 1.1 & CSS 3