Learning UnrealScript Part 5 — Understanding the World

For a long time I thought of the Unreal Editor and UnrealScript as two separate things. On the one hand we have the editor, which is good for making amazing worlds, movies, lighting, level events…the list goes on and on. On the other hand we have code that can manage menus, HUDs, player actions, weapons, animations…another long list, but hardly the same (or so I thought).

As it turns out, the relationship between editor and code is much stronger than that. To understand this relationship we’ll have to remember a point I made way back in Part 2 of this series: UDK treats everything as objects.

Everything.

See that static mesh you just placed into your level? That mesh is being controlled by code — Engine.StaticMeshActor to be exact. What about terrain? Engine.Terrain. That Matinee movie you’re making in Kismet is nothing but an Engine.MatineeActor. Even the nodes in your AI’s path mesh (Engine.PathNode) or your pawn’s model (Engine.SkeletalMeshActor) are governed by underlying UnrealScript code.

So what does that mean for you, the UnrealScript coder? It mean that you have nearly godlike power over what goes on in the worlds you create. I’m not saying that you need to start making custom classes for everything in the world (in fact, amazing maps have been put together without touching a single bit of code). But this does open a lot of doors and takes creative map design to a whole new level.

As a simple example, consider adjusting a KActor to move in only two dimensions (something I’ve covered in my blog before, but will go into more detail here). If you’ve never heard of a KActor, you just have to know that it stands for “Karma Actor” as in the Karma Physics Engine. If you open up the UDK example map you’ll see a bunch of KActors all over the place: crates, barrels, a stack of glowing light cubes. Shoot one of them and it flies off just like a real crate or barrel would.

All the underlying code for a KActor is housed in Engine.KActor. Open up KActor.uc, and you’ll notice that most of the stuff in there is either cpptext or native (that is, you can’t edit or even touch it). This makes sense since the Karma engine is not written in UnrealScript and most physics calculations are handled by faster C++ or ASM code anyway.

Of course, we want the actor we create to be spawnable; that is, we want to be able to place it in our world. So we have to extend Engine.KActorSpawnable instead. Now it’s just a simple matter of adding this function:

simulated event PostBeginPlay()
{
 local RB_ConstraintActor TwoDConstraint;
 super.PostBeginPlay();
 TwoDConstraint = Spawn(class'RB_ConstraintActorSpawnable', self, '', Location, rot(0,0,0));
 TwoDConstraint.ConstraintSetup.LinearYSetup.bLimited = 0;
 TwoDConstraint.ConstraintSetup.LinearXSetup.bLimited = 0;
 TwoDConstraint.InitConstraint(self, None);
}

Save and compile, switch over to the editor, and look in the actor tree for your custom KActor class. With it selected in the actor tree, right click in any map and click “Add [your class name] here”. Test it out! You should notice that the actor is fixed firmly in two dimensions, no matter what you throw at it. I’ll leave it up to you to think of interesting applications for this.

I’ve only shared with you a brief example, but the principles remain the same:

  1. Find an actor in a map that doesn’t do what you want it to do
  2. Find its underlying UnrealScript class (usually with the same name as the actor)
  3. Extend this class and add custom code to implement the feature
  4. Place your custom actor class in the map instead

Remember, this works for anything in the level, whether it be a SoundCue, a StaticMesh, a Pawn, a PlayerStart, etc. Now that you know this you can take full command of your UDK worlds, so go out there and do it!

Previous: Building an AI Class
Next: Undetermined

Advertisements

4 thoughts on “Learning UnrealScript Part 5 — Understanding the World

  1. Pingback: Learning UnrealScript Part 4 — Building an AI Class | WillyG Productions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s