The COG Language from Jedi Knight: Dark Forces II

Yesterday, I briefly discussed programming languages for game engines and the difference between a compiled language and a scripting language. Today, I am going to briefly cover some of the features of the COG programming language from the article written in 1997 by senior programmer Robert Huebner. Again, everything I present today belongs to LucasArts and the developers of Jedi Knight: Dark Forces II.  

The developers of Dark Forces II wanted to create language that was easy to use for non-programmers (such as artists and game designers) and one that was fast enough for real-time action. This is what inspired the COG language! Huebner mentions that COG was a custom compiled language that was based on the C programming language. The developers used the C programming language as a starting point and modeled COG off of its syntax, while removing a bunch of C's "obscure" keywords and some major parts of the language. Huebner states that the library of COG functions for the author to use is the most important part of the language rather than the actual syntax. For those of you who don't know what a 'function' is, think of it as "a step-by-step instruction guide for doing a particular task in code." These functions supposedly were useful in the game engine for manipulating the environment and other tasks such as information querying. Essentially, Huebner boils it down to the idea that the COG language functions were used to "control the game environment", while the language syntax provided were used for "branching and looping control" (i.e. if-else statements, while loops, for loops, return keyword, etc.)

Some sample COG library functions include:

  • StartAnim - starts an animation for a sprite, surface or material

  • CreateThing - creates a new "thing" in the game world, which is essentially an object.

  • GetCurSector - obtains a reference to the current sector (i.e. area/region) that a "thing" is in

  • SectorThrust - sets a thrust force for a given sector to create an apparent feeling of physics.

  • AISetTarget - sets the target of the AI. In other words, set the target of the AI as the object the AI is attacking.

The game engine for Jedi Knight: Dark Forces II was event-driven, meaning that the engine frequently checks for certain types of events to occur, such as user input, collision detection, or other "messages" passed along by other programs. Some sample COG event messages that could be passed along are:

  • Touched - an event called when an object touches another surface or object. Both objects that collided can be referenced.

  • Entered - an event called when an object, such as the player, enters a different sector, which is basically another region or room that has been defined.

  • Damaged - an event called when player took damage from bullets or explosives, where the references to the source of the damage and the type of damage can be found.

  • Arrived - an event called when the player arrives at a certain destination

  • Killed - an event called when either the player or the enemy are about to be removed from the game due to his/her death

Through these event-driven COG scripts, the game can be manipulated in whatever manner necessary to the event OR the game can choose to ignore the event entirely! This is very powerful! According to Huebner, there are additional messages that are also sent to the COG scripts directly, rather than the objects to which that script is linked. Such messages include level startup messages and even player respawn messages!

What is even more interesting is that COG scripts execute as "separate virtual machines, each with its own variables, stack, and execution pointer", as Huebner states. This means that these COG scripts are protected from each other. So if one COG script is broken, then it only affects itself and the objects to which that script is linked and nothing else! 

Again, here was just a brief introduction to the COG language written for Jedi Knight: Dark Forces II. The rest of the article gets very technical on compilers and parsing, and describes some other 'stack' data structure operations. I don't want to explain too much to the point where it gets confusing, so I will leave that as an exercise for you!

Hope that all seemed interesting! All rights belong to LucasArts.

Here are the links to this two-page article by Robert Huebner, and I've also attached a video to Jedi Knight: Dark Forces II gameplay!

https://www.gamasutra.com/view/feature/131641/adding_languages_to_game_engines.php

https://www.gamasutra.com/view/feature/131641/adding_languages_to_game_engines.php?page=2