PDA

View Full Version : State Machines


WetWired
2002-04-16, 11:41 AM
Okay, how many of you guys have heard of state machines?

State machines are functions that perform a small amount of a task each time they are called and keep track of their position in the process using a state variable. State machines are used for multi-tasking; a main loop can call several state machines each time through so each function gets time to perform its duties.

Maybe I can even find time to come up with an example and an excercise if people are interested
--WetWired

GameCube
2002-04-17, 05:28 PM
what r u talking about?

WetWired
2002-04-18, 07:18 AM
State machines are functions that perform a small amount of a task each time they are called and keep track of their position in the process using a state variable. State machines are used for multi-tasking; a main loop can call several state machines each time through so each function gets time to perform its duties.That's what I'm talking about :rolleyes:

If I somehow find time between working on my phpBB style, setting up phpBB on my Linux box, and creating my new website design, I might come up with a simple example program
--WetWired

Badass Gandhi
2002-04-18, 04:29 PM
Never heard of 'em. :confused:

BlueCube
2002-06-13, 03:30 PM
Something like, there's a main function... and 3 sub functions...

Main
SmallFunc
MedFunc
LargeFunc


And then, Main calls each one, but each one only goes a certain ways through..
SmallFunc, a few commands
MedFunc, a few commands more than SmallFunc
LargeFunc, a few commands more than MedFunc

So that, all of the functions complete at about the same time? Is that what you mean?

WetWired
2002-06-14, 07:45 AM
Essentially, that's the way the program appears to work to the user, but how it works is each function has a state variable and a switch statement, so the first time it's called it might do the code for state 0, then set the variable to state 1, next time it's called it'll do the code for state 1, then it might set the state to 50 to do that code next time, or stay in state 1 while waiting for something. All the functions are called in a loop in the main function and each does a little part of what they do each time they are called so it appears as if they are running at the same time.

It's a simple concept, though hard to explain on a msg board...
--WetWired

BlueCube
2002-06-15, 09:34 AM
Okay, I know what you're talking about now...

switch (melmac)
{
case 0: {run code here; melmac = 1; break;}
case 1: {run code here; do calculations; if (bob == 5) melmac = 2; else melmac = 3; break;}
case 2: {cout << "Bob equaled five!"; break;}
case 3: {cout << "Stupid Bob, it didn't equal five!"; break;}
}

Will only run one case statement each time through (might be a bit off, haven't programmed in C++ in a few months...)

Sum Yung Guy
2002-06-16, 03:12 AM
nope never heard of it

Randuin
2002-06-16, 08:20 AM
but they seem pretty useful...

ZERONX
2002-07-02, 04:52 PM
never heard of :(