Hello, I have a use case for some external tooling interacting with a running game. While the game is running I'd like it to process messages sent to it by the tooling. Currently it's not possible to do non-blocking reads from stdin. Calls to
serial(0x804, ...) will block until the pico 8 process receives something. What I'd like to do is something like this:
function _update() -- Hypothetically stat returns true if there's data available on stdin, false otherwise, if stat(123) then serial(0x804, 0x4300, 1) local message_type = peek(0x4300, 1) process_message(message_type) end end
It would be useful to have some way to tell if there's data available on stdin or to perform non-blocking reads from stdin.
Does it not work to use a coroutine?
For example, something like this seems like it'd work:
function _init() msgstate = -1 end function _update() if msgstate == -1 then msglen = 1 msgc = cocreate(_stdin) msgstate = 0 elseif msgstate == 0 then if costatus(msgc) == "dead" then msglen = @0x4300 msgc = cocreate(_stdin) msgstate = 1 end else if costatus(msgc) == "dead" then msgstate = -1 print(chr(peek(0x4300,msglen))) end end end function _stdin() serial(0x804,0x4300,msglen) end
+1; here's a full example of how this is a problem (built together with citizenerased)
x=64 y=64 s="" function _update() local b=btn() x+=b\2%2-b%2 y+=b\8%2-b\4%2 local len=serial(0x804,0x4300,1) if len>0 then s..=chr(@0x4300) end end function _draw() cls() spr(0,x,y) print(s,0,0) end
invoke this as
cat | pico8 -run /path/to/cart.p8 and then type lines of text in your terminal for it to appear in-game. (ctrl-c to exit)
we couldn't figure out a way to move the player while no text was being inputted (because p8 gets stuck waiting for serial to return when there's no input on stdin)
note that this runs fine if you run it normally or with
echo hello instead of
cat (i.e. when stdin is closed)
[Please log in to post a comment]