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

Interrupts

Started by Fullmetal5, 04 October 2014 - 02:03 AM
Fullmetal5 #1
Posted 04 October 2014 - 04:03 AM
I'm not sure this is even possible with the way Lua is implemented in ComputerCraft but it would be nice to see interrupts implemented in the game.

For example there could be an array that programs could add functions to, when an event is triggered in the system the current program is paused and the functions are called one after the other and passed the event as an argument.

The main reason for implementing this would be programs that need to execute when certain events happen (like a server) while other programs are currently being executed.

Because this would involve pausing lua programs mid execution I'm not 100% sure this can even be done.
Lyqyd #2
Posted 04 October 2014 - 04:21 AM
This isn't really necessary, I don't think. At least, not for the stated reasons. You'd just need to set up your server programs to run parallel to each other (for instance, in multiple tabs in multishell, or using one of the multitude of multitasking OSs available on the forums), as events will be distributed to all of them.
Fullmetal5 #3
Posted 04 October 2014 - 04:29 AM
I can across the idea when trying to code a telnet server in lua. The main problem was trying to update the screen of the client (irl computer) when the lua computer can't do anything when another program was running on it unless they called PullEvent, and even then when the programs would do long operations or get stuck in loops then I couldn't remotely do CTRL-C.

My thought was to use the interrupts and have a function in the interrupts update the client and set a new timed event. So I could both update the client and make sure that I could stop programs running using CTRL-C from the client.

EDIT: from looking at the OS's on the forum it looks like all they do is start them in new threads, this still leaves the issue of a program hogging execution and delaying events until they either get killed by CC or finish.
Edited on 04 October 2014 - 02:41 AM
Bomb Bloke #4
Posted 04 October 2014 - 04:55 AM
There isn't an interrupt system available such as the one you want, and implementing one would be non-trivial.

Fortunately, scripts seldom execute code for even a second before yielding. And besides, given that ComputerCraft's Lua VM only executes one thread of code at a time (world-wide), if one system is stuck in a loop, that means certain events cannot occur anyway - for example, you can't send a message from a given machine unless that is the one whose code is currently being executed, therefore a modem_message/rednet_message won't appear in a receiving system's event queue so long as that receiver is executing code.
Fullmetal5 #5
Posted 04 October 2014 - 05:08 AM
I kinda guessed that it couldn't be implemented in lua. I don't know LuaJ enough to even see if it is possible at the java level. I guess if it can't then the client (irl computer) will have to receive updates only when the lua process executes PullEvent(), causing unstable connection. ex. You type in "Hello World" and it will wait until the current process yields then it will check for updates and find it needs to type this.

Since the other computer is IRL it won't be synced with the Lua VM, and therefore I was having to have my lua program check a php file on my server every tenth of a second to both update the display and to check if there was a key typed.

With the interrupts I was hoping to have a more stable update period rather than whenever the program decided to yeild.
Lyqyd #6
Posted 04 October 2014 - 06:43 AM
Wait, what? Are you talking about trying to set up a system whereby you could interact with a ComputerCraft computer's terminal from an application external to the game via a php script? If not, I'm not sure where a php script becomes involved in the process.

Perhaps it would be easier to help you resolve any issues and assuage your doubts if you were to post your code over in the Ask a Pro section. If you're not trying to bridge to something outside of the game, and are just trying to create a program for use entirely within ComputerCraft, your concerns are, so far, all easily addressable. Even if you are trying to bridge to something external, I'm not sure where interrupts would come in to play, since the time scales of the event-based model should make such things completely unnecessary.

Seeing the code involved would be helpful.
Fullmetal5 #7
Posted 04 October 2014 - 05:12 PM
I only came up with the idea while trying to find a solution my own problem (doing php to CC telnet). I just thought it was a good idea so I posted it here, my usage was just an example.

I wanna do some more work on my code before posting in Ask A Pro.