This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LDDestroier's profile picture

Help with chatbox command system

Started by LDDestroier, 16 November 2017 - 03:25 PM
LDDestroier #1
Posted 16 November 2017 - 04:25 PM
I was trying to one-up nago with a chatbox-based command interpreter that would have modular commands that could run asynchronously upon activation, so that followup input could be read. However, I cannot get that to work. Right now it just hangs until you reboot with CTRL+R. How do I do this?

Link to pastebin: https://pastebin.com/ECeJ3CpL

Let me explain its intended action.

Instances of commands being ran are stored in the userActions table. When any external command is ran (found in "/chatcommand/cmd/"), it will create a coroutine of a function of that command being ran with the first argument being the user who ran the command, and the rest being an arbitrary amount of extra arguments. When a command is done executing, it's supposed to be removed automatically from the userActions table. A person would chat a command, then it would look for all external commands and check too see if the user command matched. Failing that, it would check the internal ones instead.
Of course, none of that happens, because it hangs immediately. It doesn't return a "too long without yield" though.

If I get this to work, this could be my big chance to learn the ins and outs of coroutines, and I could make new, wonderful things!
KingofGamesYami #2
Posted 16 November 2017 - 04:31 PM
You aren't passing any events, which your commands probably need. Also, this sounds like a great place to use [url="http://www.computercraft.info/forums2/index.php?/topic/25805-statemachine-api/']my statemachine API[/url].
LDDestroier #3
Posted 16 November 2017 - 05:10 PM
You aren't passing any events, which your commands probably need. Also, this sounds like a great place to use my statemachine API.

That looks really useful, but the functions that run in the chatbox command interpreter are in a table that needs to be indexed with strings. Can functions asynchronously ran by statemachine be named, individually terminated, and stored in a table? The instructions on the forum post didn't seem to demonstrate that exactly.
KingofGamesYami #4
Posted 16 November 2017 - 05:23 PM
Yes. The API is designed to give you as much control as possible, without having to deal with co-routines.
Something like this would work:

local tFuncs = {} --# put a bunch of functions here
local tFuncAsyncs = {}
for k, v in pairs( tFuncs ) do
  tFuncAsyncs[ k ] = statemachine.unblock( v )
end
statemachine.setLoop( function()
  for k, v in pairs( tFuncAsyncs ) do
    if v() then
      tFuncAsyncs[ k ] = nil
    end
  end
end )

Edit: I have a project demonstrated here which shows a bit more of what you can do. It's not 100% working and I may have slightly abandoned the project, but it's there.
Edited on 16 November 2017 - 04:35 PM
LDDestroier #5
Posted 16 November 2017 - 06:55 PM
-snip-

After a bit of modifying, I got it working. I think it will work for other people too, so I'll test further on a multiplayer server. Thanks for the help, your API really saved my ass.
KingofGamesYami #6
Posted 16 November 2017 - 07:02 PM
Thanks :)/> I think this is the first time someone has actually used it (besides me), so I'm glad you found it useful.