NPC Logic - Use a "state machine"?

Hi All,

I’m developing the logic to control the activities of an NPC in my current project. This “bad guy” will be doing things like moving, attacking, waiting, etc.

What’s the best way to control these behaviors?

Use an onEnterFrame event listener to check a master “behavior function” on each frame? And then would I use a series of timerPerformWithDelay functions to set “flags” to perform certain activities (for instance I wouldn’t want the NPC to attack on every frame if the attack criteria were met)?

Or is there some preferred technique for controlling an NPC that I’m completely missing?

Thanks in advance for any insights anyone can share :slight_smile: [import]uid: 105707 topic_id: 28676 reply_id: 328676[/import]

LUA coroutines. [import]uid: 12704 topic_id: 28676 reply_id: 115618[/import]

Wow… I’d never even heard of a “coroutine” but this looks like a powerful concept. I am sort of generally aware of multithreading and this appears to be lua’s solution for that.

Has anyone seen any bits of sample code or tutorials related to this? A practical example would help me move from the “theory” of the lua manual toward the reality of controlling my NPC behaviors.

Thanks :slight_smile: [import]uid: 105707 topic_id: 28676 reply_id: 115620[/import]

http://www.rocheon.com/?p=179 quick example [import]uid: 12704 topic_id: 28676 reply_id: 115644[/import]

@gtatarkin - great sample, thanks!

I’m going to summarize what I’m getting from all of this (my research and this latest tutorial)… please let me know if I’m on track.

coroutines in lua are a way of emulating collaborative multithreading (only one extra “thread” running at a time)

You “break out of” a coroutine with the yield function. You must use a yield function to avoid a dead loop. (my initial impression was that a coroutine is a loop but am I right in thinking that it actually only “loops” if you’re driving it in an iterative manner from wherever you step into and out of it?)

It strikes me that it’s imperative that a person uses mutually exclusive logic (if statements) to ensure that only one of multiple coroutines will want to run at any given moment.

How am I doing?

[import]uid: 105707 topic_id: 28676 reply_id: 115676[/import]

Coroutines are just normal functions, but they can pause at yield points and then resume. If you want them to loop, then they can do that, but really this is what they are and the “memory” location is what serves them well. [import]uid: 150267 topic_id: 28676 reply_id: 144949[/import]

Coroutines are just normal functions, but they can pause at yield points and then resume. If you want them to loop, then they can do that, but really this is what they are and the “memory” location is what serves them well. [import]uid: 150267 topic_id: 28676 reply_id: 144949[/import]

Coroutines are just normal functions, but they can pause at yield points and then resume. If you want them to loop, then they can do that, but really this is what they are and the “memory” location is what serves them well. [import]uid: 150267 topic_id: 28676 reply_id: 144949[/import]

Coroutines are just normal functions, but they can pause at yield points and then resume. If you want them to loop, then they can do that, but really this is what they are and the “memory” location is what serves them well. [import]uid: 150267 topic_id: 28676 reply_id: 144949[/import]