GAME JOBS
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
June 6, 2013
 
LeapFrog
Associate Producer
 
Off Base Productions
Senior Front End Software Engineer
 
EA - Austin
Producer
 
Zindagi Games
Senior/Lead Online Multiplayer
 
Off Base Productions
Web Application Developer
 
Gameloft
Java Developers
spacer
Blogs

  Towards a Rule-Based Game Engine
by Shay Pierce on 06/21/11 02:52:00 pm   Expert Blogs   Featured Blogs
16 comments Share on Twitter Share on Facebook RSS
 
 
The following blog was, unless otherwise noted, independently written by a member of Gamasutra's game development community. The thoughts and opinions expressed here are not necessarily those of Gamasutra or its parent company.

Want to write your own blog post on Gamasutra? It's easy! Click here to get started. Your post could be featured on Gamasutra's home page, right alongside our award-winning articles and news stories.
 

I'll be doing a series of blogs posts shortly about the iOS puzzle game I just designed ("Connectrode") and submitted to the App Store this morning. For now I thought I would "detox" from that project, and ease myself back into blogging, by writing about something else that interests me.

Warning, this is a fairly off-the-cuff attempt to express some very abstract ideas about both game design and programming - I haven't necessarily polished these ideas into the most comprehensible form... I'm mostly just writing this to get the ideas out of my head and onto "paper."

I've been a "Gameplay Programmer" in one form or another for most of my career. A lot of this has taken the form of my designing games myself, and then proceeding to implement them myself - a process which I love both sides of. Not very many game designers actually do the coding themselves; and not many gameplay programmers do a great deal of game design. I feel like it's given me a unique perspective, and in particular it's led me to conceive of a new type of game engine - one that would minimize the need for even having a gameplay programmer, and put the implementation directly into the hands of the designer.

The idea started to come to me as I worked on card game and board game design prototypes - this, I realized, was a very pure form of game design. Though "table game" design gives you a limited set of tools compared to those you have on electronic games, it can really open your eyes as to what the core of game design really is.

I've believed for some time that, structurally, a game is comprised of only 3 things:

  1. Nouns - Elements of your game system, and variables related to them. Example: Players, avatars, NPCs, items, a map, a world, locations, tiles, cards, XP, HP.
  2. Verbs - The actions that agents (players or player stand-ins) in your game can enact; the "things you can do." Example: Attack, jump, move, bribe, play card, roll dice
  3. Rules - Rules, which limit the nature of the existence of the nouns and create relationships and interactions between them; and which limit which verbs can be enacted when and in what context. Example: "draw a card at the start of every turn"; "when a unit attacks another unit, it does damage equal to the attacker's base damage value"; "when Mario is not on the ground, gravity pulls him down at a rate of acceleration of 1 pixel per millisecond".

Every game has these three elements, and I think that games (in terms of a formal structure) are solely comprised of these three elements. (In fact I theorize that all systems can be defined by these three elements, and that the "engine" I'm about to propose might be a new approach to developing not just any type of game, but any type of software. But for now I'm sticking with the domain I understand best: games).

Given that this is the structure of any game, shouldn't a proper game engine be focused on, first and foremost, modeling these three things, and making the creation and modification of them as straightforward as possible? A few game engines seem to do this, but in very specific ways which don't seem to get to the heart of the matter.

What this comes down to is that I feel that current programming paradigms do not provide an appropriate language for expressing these structures. This may not be true for the "nouns" element - Object-Oriented programming, as its name implies, is very good at representing different types of objects ("nouns") and the relationships they may have between each other, with minimal duplicated code or wasted work.

But both verbs and rules are hard to express clearly in any programming language I know. In particular I think that rules (which I see as the more important and often-changed element of game design) can often be difficult to represent in existing programming/engine paradigms.

Let's say I'm developing a platformer and I want to add rules that say "when you jump on any enemy's head, it dies", but then add an exception that says "when you jump on a porcupine's head, it survives and YOU die". In current Object-Oriented programming paradigms, I'm probably going to express this by 1) adding a "jumpedOnBy()" function to the Enemy class with a body of "die();" ... and 2) overriding that function on the Porcupine enemy class with a body that says to kill the player instead. (This is not exactly how I would actually implement this but it's close enough for the point I'm making.)

The point is that the habits of Object-Oriented programming lead me to instinctively express this rule as properties or behaviors of the nouns. I think this habit is inappropriate: each rule should be represented as an object in its own right; and conceptually the rule should in fact be an element that "controls" the nouns and can modify them. But our current typical representation of rules doesn't do this at all.

Instead they find themselves poked and prodded into various places in code. It's rare, in gameplay code, to find a rule that is even concisely expressed in exactly one place in code - in other words, it's rare to find a 1-to-1 relationship between "a game rule" and "a chunk of code."

As I think this through now, I think that perhaps the biggest, paradigm-shifting implication of coding this way would be thatnouns do not have behavior of their own. In a game of Monopoly, the Thimble on the board has no behavior of its own; it just has the capacity to rest on different locations on the board. It is verbs that put the thimble into motion (a player initiates a "move" verb), and it is rules that restrict and define that motion (a rule states that the player performing a move verb must roll the dice and move the thimble that many game spaces in the forward direction).

If we believe that our code will be cleaner and more maintainable if it structure matches the structure of the conceptual objects that we're actually representing, then we should move gameplay code to a paradigm in which Nouns have no behavior of their own; and push to make Rules and Verbs first-order objects.

Furthermore, this should be extended to the point that Rules are entirely data-driven, and can be defined entirely by designers using engine tools. My ideal game engine looks like this:

  • The Gameplay Designer can add Rules into the game engine using a straightforward but robust engine for expressing those rules. (They also define Nouns, and have some capacity to define Verbs.)
  • They can then immediately play the game with these rules being applied by the game exactly as defined. They see where the rules fail to create the behavior that they want, and can easily modify those rules and run the game again.
  • That's it. There is no Gameplay Programmer in this workflow. It's kind of like the Game Designer is sitting down and defining a board game design, playing it and iterating on the rules... except that this game happens to automate all the enforcement of the rules, allowing them to make much more complex games. Because that's all that video games are.

All right, this is a grand high-level vision of intentions, but how exactly do we go about it? I gave a counter-example with the "jumping on the porcupine head" rule and how we shouldn't be representing that; but where's my example of exactly how weshould?

I'm afraid I haven't gotten that far; I only have the glimmer of an idea at this point, and I feel like throwing out my half-baked answers wouldn't help anyone. But, it's something I've thought a lot about and would like to spend more time researching. Googling for "Rules-Based Programming" has been encouraging, as it turns out that there's a substantial amount of existing knowledge for programming frameworks that work along these lines; but I haven't yet explored them thoroughly enough to determine whether they truly apply what I'm trying to get at here.

The "hard problem" here is representing rules as abstract entities. The fact is that a "rule" is something that we as humans have a very loosely-defined idea of. I don't know if there's a single language that can be used to express any and all possible "rules"; perhaps the only language that can actually do that is human language itself.

But perhaps it's not as hard as all that, and we need to simply sit down and take this abstract ideal and determine what concrete form it could take. Or perhaps someone out there has already made significant strides in this direction, or an entire engine based around this, of which I'm unaware.

My main hope is that this blog will spark more thought and discussion on the topic; lay out a (very vague and high-level) ideal we can work towards; and hopefully that someone will even point out to me a paradigm that already represents gameplay code in this way, or at least is a step in this direction!


[Shay Pierce is a guy who started making games at a young age and has never been able to stop, in spite of his many years in the professional games industry. He is currently the Lead Game Developer at OMGPOP Austin, a newly-formed social game development studio. This post was cross-posted from his game design blog at http://www.DeepPlaid.com]

 
 
Comments

Luis Guimaraes
profile image
This is an interesting topic. I can't see as far ahead in the matter as you, but can have many smaller insights of different ways to tackle gameplay algorithm based on what you propose. But even the farther I try to imagine that, can't see the gameplay programmer being of no need since they have to at least construct the base for these rules.



My best guess is some mixed architecture of tables rulesets made of events and static functions tied together. But the processing of any simple gameplay action would be very expensive.

Shay Pierce
profile image
@Luis, thanks for the comment. I should clarify that in my ideal world, EVERY rule that is part of gameplay would be part of this rules system, including, say, physics... probably even AI (many AI applications are already very rules-based). And I forgot to include the logical extension of this idea: engine users could exchange "rule clusters" from different games and modify them; this would be a way to essentially pass around, say, a platformer physics engine. Actual rendering to the screen and the visuals of animation wouldn't be controlled by this engine either, of course - though if the animation impacted gameplay, it should be taken into account somehow. This is one of the areas where the division would get odd...



I should add that Andrew Traviss replied to me on Twitter referring me to his blog posts about some very interesting work he's doing on a (Flash?) game engine in this exact direction, it looks like he's already gone further in the same direction. Very interested to see how it works for him:

http://blog.andrewtraviss.com/tag/engine-design/

Luis Guimaraes
profile image
Quite interesting exemple, thank you.

It's definitely going to be of help in future projects.

Kevin Swiecicki
profile image
This sounds very interesting. I'd imagine that scribblenauts would have a lot in common with this type of engine. Not to be a negative nancy, my thoughts or concerns lie in how one would go about debugging these rules if they are brought under the same "cluster." Also, say you have a series of rules that depend on another rule. I guess what I'm trying to say, is how would you maintain a healthy separation of concerns so that adding or changing a rule doesn't unintentionally conflict other existing rules. If that makes any sense.

Joe McGinn
profile image
That makes sense, thanks for such an interesting article.



Presumably you would still need a gameplay programmer to implement new verbs, like if I want to tell a car to drive and that's never been done in our game before? Still I can almost imagine the rules part of what you are saying and how beneficial that would be to be able to implement it in a more organized and natural way.

Brett Witty
profile image
Have you seen Inform 7? You specify the game world precisely as verbs, nouns and rules. It uses natural(ish) English and deduces a lot of the appropriate markup just by grammatical analysis. It's geared towards Interactive Fiction, so there are certain limitations. You can give a generic jump command, and then have special exceptions for certain objects.



I don't know how useful it'd be to, say, a Mario-like game. The basic gameplay elements could be abstracted into Inform 7, but a lot of the gameplay and game-feel couldn't. How high should he jump? What angle? What speed should a goomba move? How tight is the bounding box? As Chris Hecker has mentioned on a few occasions, this is where game design sits. At some point, putting in elements and rules into a game *is* programming. Whether you use a scripting language or C++, you'll still have to tell the computer dumb things to make it do fun things.

Shay Pierce
profile image
I've seen some incredible things regarding Inform 7 and have seen a couple of demos; I didn't know that it modeled things in this way! I may have to explore it more deeply, it sounds like a brilliant "text game engine."



It's possible that none of this type of architecture would be PRACTICALLY feasible for a game running 60fps; but the idea is that a system such as this could be applied to any type of gameplay, whether a text adventure, a platformer, an FPS, a Magic-style trading card game, or an MMORPG. Whether or not it's practical has yet to be proven, but it seems conceptually sound as a way of properly representing the elements of gameplay in code.



I'm currently thinking a lot about an example Rule object for a Mario-like platformer...an implementation of the "2D collision detection" rule. This Rule object would:

1. Be triggered when an Event of "changing a character sprite's X position" was ABOUT to occur.

2. Would need to check whether this "position change" Event would cause that character sprite to overlap another "collision sprite".

3. If it saw that this "position change" Event WOULD cause such an overlap, it would then PREVENT that Event from actually taking place - it would cancel the event that had triggered the Rule to be checked.

4. It would additionally spawn a NEW event of "reduce the character sprite's X velocity to 0".



With something like that you would have a Rule object that represented the game rule that causes Mario, upon running into a pipe, to halt and not move through the pipe.



Normally code like that would live in the realm of gameplay code and in the responsibility of the gameplay programmer. But these are still gameplay rules, and gameplay designers ultimately have the responsibility of making sure they work together correctly and make the game fun.

Paolo Taje'
profile image
That's EXACTLY how you do things in The Games Factory and Multimedia Fusion since 1996

steven tu
profile image
Deep stuff, but good to know.

Joseph Bleau
profile image
Perhaps it's because I lack visionary quality, but I can't even begin to imagine how you can abstractly define a rule with the relative ease you seem to want to be able to, without having to describe that rules implications and interactions with all other existing rules. Once you reach that level of granularity and specificity you might take a step back and think, "Oh. Well, I suppose what I've created is just like any other programming language."



That said, I personally hope I'm wrong. I would love if individuals could express themselves in any art form (music, painting, game design) more easily. The less barriers there are between imagination and creation the better. I wish you the best of luck!

Jonathan Lawn
profile image
Intransitive verbs (associated only with a subject noun only) are easy in OO code. Transitive (subject and object) are harder but aren't usually commutative I don't think (unless you want to base your verbs around having an "equal and opposite reaction" on each noun), so this is perhaps more of a problem for the rule code.



I think the key is to break down each rule into an event, conditions and resultant effects. Both events and effects are a verb with associated nouns. A condition is a test on a property of a noun (adjective), and could perhaps be rolled into the effect. I suspect the fun comes in how to map all the nouns between these.



e.g. On event "A and B contact" do "damage A from B" and "damage B from A".



Then you have scale to worry about, which probably depends on the objects, e.g. a bullet contacting a gas cylinder may be two distinct objects, but a bullet hitting a body may need to be treated as a bullet hitting a leg (a sub-object) too, so that the leg can take damage (and knock you over, and affect movement in the future) but the body can take shock, or lose points or blood or something. I think this is why windows code tends to happily generate "clicked" events for the window and each child object, but the effect of the event has to depend on each object (OO style) not be based around a general rule.



Are you thinking that you could just operate all rules at the lowest level of object (which can then influence their parents)? Or that the rule should be able to define what sort of object it refers to? If the latter, can the labelling really be simple (e.g. with each object in a single class)? I'd have thought you'd end up with the labelling being a set of tags (probably effectively a list of implemented verbs) in which case you're almost back to OO again. If you can implement most rules in base objects though, maybe you can avoid much coding.

Max Antinone
profile image
Reading through this, the first thing that I thought of was FF12's Gambit system, made to allow players to describe a certain pattern of behavior. Of course, now that I think of it, that's basically just a cascading if-else flowchart...



Thinking about the gambit system, I just remembered something I saw on Kotaku a year or two ago. There's this simplistic game engine called Kodu that was released on XBLA a while back. It says that it works on a rules-based paradigm. I don't know that it's anything particularly revolutionary, but it's probably worth looking into.

Here's the wikipedia page.

http://en.wikipedia.org/wiki/Kodu_Game_Lab

Harry Fields
profile image
But for the rules to have any meaning in such an engine, they must be cable of interacting with myriad properties of said nouns and verbs. Sure you could create a simple DIY framework with basic rules easily handled with the most basic of logic, but say my widget, which has many complex properties, flummitzes; an action which has many complex properties in and of itself... at a gizmo, which also has many complex properties. The rules to handle such occurences would be quite expansive. Now what if my widget is flummitzing while blorking at armies of gizmos, some of which are occluded by wazzologs and minajabos. Granted, this exactly the situation which makes such an engine incredibly drool-inducing, but the complexity of it all.... /shudder.

Shay Pierce
profile image
Yes, the rules to handle complex verbs between complex objects ARE quite expansive and complex. They already are! It's just that the way we enforce these rules in code is obtuse to the structure of what a rule actually is. In theory, this system would let you express anything you already could using gameplay code, just in a very different way... but a way that is based on representing rules as what they are.



It's possible that game designers creating games aren't actually aware of just how complex, rules-wise, their games ARE, because often gameplay programmers end up working out exactly how different rules conflict and interact, and resolving certain edge cases. Any game designer worth their salt should be dealing with these problems themselves however; and it's generally better for one person to be designing a system, rather than the responsibilities falling to both a designer and a programmer, who might even have different visions of the game design.

Alex P
profile image
Couple of ideas to help you develop yours....



1) Consider how web servers use server side scripting to dynamically render HTML... you basically have a set of XML tags that the server interprets and renders a result.



2) Consider the concept of a Mediator pattern (http://www.oodesign.com/mediator-pattern.html). One interpretation of this OO pattern is that it is the manifestation of the 'Verb' and acts on 'Nouns'.



3) Check out the concepts of Aspect Oriented Programming (http://www.javaworld.com/javaworld/jw-01-2002/jw-0118-aspect.html). This provides a way to add capability to a class without re-coding.



I believe with the combination of these three concepts you could realize the the platform you're trying to build. I've built a number of platforms in my day and the trick is visuallizing a clear path through the major technical hurdles and then refining over time. Lots of hard work, but really rewarding.

Nick Harris
profile image
This reminds me of a paper that I stumbled upon recently:



"Recombinable game mechanics for automated design support" by Mark J. Nelson and Michael Mateas:



http://scholar.google.com/scholar?cluster=7604083257316498260&hl=en&as_sdt=0,5


none
 
Comment:
 




 
UBM Tech