StateMachine

Started by KingofGamesYami, Aug 13, 2018, 02:10 am

Previous topic - Next topic

KingofGamesYami

Reposted from here

Many people have heard of multitasking, or running multiple things at once. Well, my API takes a different look at multitasking - that of the statemachine. With a state machine, you do not use any blocking commands (sleep, os.pullEvent, etc.). Instead, you use entirely Asynchronous commands, which allows you to (potentially) do many things at the same time.
The basic usage of my API is somewhat simple:
Code Select
os.loadAPI( "statemachine" )
statemachine.setLoop( function()
  print( "An event occurred!" )
  end )
statemachine.run()
...however, there is more to it than that. My API includes it's own pullEvent, which means you can do this:
Code Select
os.loadAPI( "statemachine" )
statemachine.setLoop( function()
  local event = {statemachine.pullEvent()}
  print( "An event of the type " .. event[ 1 ] .. " has occurred!" )
end )
statemachine.run()
Unfortunately, many functions commonly used are blocking (eg turtle.forward). You can use them with my API, but you must first convert them to a non-blcoking command via the unblock function.
Code Select
local async_forward = statemachine.unblock( turtle.forward )
It is important to note that a blocking command will return true when it is complete, with any values returned by the function afterward.
Code Select
-- using async_forward
os.loadAPI( "statemachine" )
statemachine.setLoop( function()
local complete, success = async_forward()
  if complete and success then
    print( "The turtle moved forward!" )
  end
end )
statemachine.run()
In this example, the turtle will (try to) move forward forever, and each time it completes a movement it will execute the print statement.
Another important bit of usage to note is the ability to end your program. To end your program simply add a return true.
This example will print all events that occur for 10 seconds, then end the program
Code Select
os.loadAPI( "statemachine" )
local usleep = statemachine.unblock( sleep )
statemachine.setLoop( function()
  local event = {statemachine.pullEvent()}
  print( event[ 1 ] )
  if usleep( 10 ) then --after 10 seconds
    return true --end the program
  end
end )
statemachine.run()
The API can be found here:
pastebin get 3qe1iUQc
Please comment with any bugs or features you want removed or added, respectively.
I'm a ComputerCraft veteran with over 3k posts on the old ComputerCraft Forum.  I'm mostly inactive in CC having moved on to bigger, more interesting projects but still contribute to the community.

demhydraz

I don't mean to be rude, but what does this have to do with state machines..?

KingofGamesYami

Basically, it removes an obstacle preventing one from using a state machine in a program.  Namely, that several functions (such as turtle.forward) like to eat events.
I'm a ComputerCraft veteran with over 3k posts on the old ComputerCraft Forum.  I'm mostly inactive in CC having moved on to bigger, more interesting projects but still contribute to the community.