# How can I implement a command system for a point-and-click style adventure game?

I'm making a point and click adventure game, and I'm currently trying to figure out the best way to implement a command system.

Assume a Monkey Island or Maniac Mansion style interface, with a palette of verbs, and objects in the scene and in the inventory. Clicking on these, you build up a sentence to execute.

for example, you might click look at and then a tree to get "look at tree" or click on an apple, and then give, and then a girl, to get "give apple to girl".

There are three possible forms of a sentence:

• verb, for example "save"
• verb noun, for example "pick up apple", "look at octopus"
• verb noun noun, for example "give pickle to rabid dog", "use crowbar with pickle jar"

In different situations, I'll want the action to be carried out to be defined by different objects.

For example, for the give verb, there might be a default action defined that makes the character say something like "I'd prefer to hang on to that", or the rabid dog might define an action where it eats anything you attempt to give it.

If the interface is to work like Maniac Mansion, then you build the sentence, and then tell it to execute (in Maniac Mansion, by either clicking the sentence, or clicking again on the last thing you clicked on).

If it's to work like Monkey Island, the sentence executes as soon as it's complete, which poses a problem for verbs like use which might sometimes take one noun, and other times two.

So how would you implement a system that handles this?

• There seems to be three types of verb, one type for zero nouns, another for two, etc. They're not the same class of object. You can't "look at octopus with pickle jar" so look is a single-noun verb, etc. Jul 16 '10 at 13:17

Your may find it easier to use if you reverse the selection order. So the user clicks a noun and then the game displays the set of verbs that can be performed on that noun. If the verb requires an object (i.e. "give to ___", then the game waits for the user to select the object before performing the action.

Something like:

1. User clicks a cheeseburger.
2. Game shows a menu: "pick up, eat, look at, give to ___".
3. User selects "give to ___".
4. Game says "give to whom?" and waits for the user to click another noun (or a cancel button).
5. User clicks a monkey.
6. Game gives the cheeseburger to the monkey.

Implementation-wise, each object in the game needs to have data for:

1. Which set of verbs you can apply to it.
2. For verbs it allows that take an object, which set of objects can be applied.
3. For each verb or verb-object pair, the behavior to perform when the user does that.

Since the grammar doesn't really nest or anything, you shouldn't need something as complicated as an interpreter pattern.

• +1, both on modeling side of things and on the interface side of things. Yes, mostly I'm agreeing because this is how I'd do it, but I like this answer. Jul 17 '10 at 0:38
• If we agree, then clearly we must both be right. ;) Jul 17 '10 at 0:46
• So, effectively, you think the behaviour to perform should always be defined by the first noun? Jul 20 '10 at 7:00
• Yes, I think multiple nouns will share behavior (so that "pick up" would use the same code for picking up different objects), but it makes sense for it to be up to the noun to determine the set of verbs that can be applied to it. Given that the set of nouns is very large (every thing in the game) and the set of verbs small (probably a handful of operations), I think it makes sense for the user to choose a noun first since that winnows the combinations down more quickly. And, pragmatically, it lets the user click a thing to initiate an interaction, and not some word. Jul 20 '10 at 18:19
• This is kind of how full throttle did it. Jan 20 '11 at 11:15

I present simplistic solution. It can be extended of course.

I think simple list of (verb, object1, object2) would solve it:

• if player clicked object (verb) "use" and clicked object "balloon" and player clicked object "pump" and there exists triplet ("use", "ballon", "pump") then You "used ballon with pump"
• Sometimes object2 would be NULL like in "use helium" (use, helium, NULL)
• Require player to click on object verb first
• if player clicks on something which does not match anything say "I can't do this, this is nonsense"
• Of course You should check after every click if the sequence is correct.

How to handle defaults:

• If first click is not on verb object, search for possible default action.
• One way to store defaults would be to make quadruplet (verb, object1, object2, is-default)
• Other way to store them would be to have list of default triplets
• ...

Some examples:

• (use, ballon, pump)
• (give, John, potato)
• (walk, piranhas, NULL)
• (use, ballon-in-inventory, pump)
• (open, door-to-roof, NULL) , default .... example of default action

It can be extended:

• add some events to be triggered (give player something, player will say "I can't do that because I am a mighty pirate", start cut-scene, change something in world ...)
• add some preconditions. Sometimes the ballon might be in cage, so You would need to express "if ballon is not in cage". I think I might approach this with event calculus or prolog or do it with function pointer ...
• sometimes the sentence in commandline would not be "look at hole" but would be rewritten to "look into hole", this requires just a variable :)

There's two problems here: Interpretting the player's input into a syntax tree, then exectuing that tree.

For the first stage, I'd have each verb button create a concrete verb instance derived from some verb interface. That instance would be passed further nouns or verbs for validation. If valid, it would attach the noun to its internal syntax tree, else it would reject with an appropriate error.

After each button press, you could ask the verb tree if it was in a valid state to proceed (for the Monkey Island-style input).

Once on to the second stage, the concrete verb would be responsible for parsing its own tree and updating the game state.

A more complex solution would involve parsing the tree externally to the verb. Each tree element would be responsible for small actions that together produced the desired result. This approach would allow you to make more emergent sentences from small building blocks.

Check out the Interpreter pattern for more info on the parsing stage.

• I don't think interpreting the players input is a problem. Certainly, there's nothing as sophisticated as parsing input to a text adventure needed here. Really, the question boils down to how would you design your object hierarchy and interactions to allow maximum flexibility over what object defines the actual action to be carried out. Jul 18 '10 at 18:49