Table-driven NPC actions

In a current WIP (18 Rooms to Home) I’ve got multiple NPCs taking actions in the same room. And the actions each NPC wants to take are affected by the PC’s actions and other NPC actions. Usually, I manage NPC actions with stacked if statements, but it got really unwieldy this time (and by “unwieldy”, I mean "such a mess I had to tear it down and start over).

So I tried a data-driven approach. It’s working well so far, and I thought other people might find the technique useful, so I wrote up a post.

Table-Driven NPC Actions in Inform 7

If you want a bit more flexibility, so that you don’t need to change the table structure whenever you want to add a condition, you could follow ATTACK’s approach. It has a rulebook which adds stored actions to a table. The rules can decide based on whatever criteria they like whether to add an action or not, and what action to add. There is also a ranking system, both for choosing which action a npc will take, and for choosing the order in which the npcs act.

One thing I see in your sample code is that you have an “activity” column and a bunch of one-off activities. It will be simpler and take less overhead if you have a “rule” column and a bunch of one-off rules:

Table of Caleb Actions
completed	has-cards	Teo-playing	high-stakes	rule	message
true	true	true	true	foo rule	"Nah."

This is the foo rule:
	say "Foo."

And use “follow the rule entry” instead of “carry out the activity entry activity.”

Activities have before/carry-out/after rulebooks, but it looks like you’re not making use of that, so rules are just as good. Saves you typing all the “XXX is an activity” lines too.

(Also, having just glanced at Emily’s review of inurashii’s Bloom – I see it’s Max Gladstone Fan Week. :slight_smile:

(I just finished Last First Snow. Excellent, although it doesn’t quite escape the burden of being a prequel.)

Good to know, thank you! I’ll modify my code and do a follow-up post.

(Split my Gladstone response to here.)