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

Monitor GUI

Started by areuz, 04 December 2015 - 12:28 AM
areuz #1
Posted 04 December 2015 - 01:28 AM
It's been a really long time since I last worked with CC, actually, LUA in general, so please don't judge me :)/>

This is just a snippet of my code (I don't think the rest is needed), but it's the thing that's causing the problems:

mon = peripheral.wrap("right")

function awaitTouch()
  while true do
	local event, side, var_x, var_y = os.pullEvent("monitor_touch")
	print("TOUCH")
  end
end
parallel.waitForAll(awaitTouch()) --There are more threads running, but even when I disabled them, the problem stayed.

It literally doesn't work, the touches aren't recognised and nothing is happening. Before, the computer was shutting down by itself after about 5 seconds, but somehow that stopped now…
Lyqyd #2
Posted 04 December 2015 - 02:10 AM
Moved to Ask a Pro.
Bomb Bloke #3
Posted 04 December 2015 - 02:27 AM
parallel.waitForAny() wants you to pass it function pointers (eg "awaitTouch"), not the results of executing functions (eg "awaitTouch()").

If you're still stuck, please post the whole of the script - use Pastebin if need be.
areuz #4
Posted 04 December 2015 - 08:33 AM
parallel.waitForAny() wants you to pass it function pointers (eg "awaitTouch"), not the results of executing functions (eg "awaitTouch()").

If you're still stuck, please post the whole of the script - use Pastebin if need be.

It doesn't work even when I do that. It was just a rewrite mistake of mine.
Here is the full code, I started yesterday evening, and this is a problem that stopped all of my progress.
All you need, is to connect a advanced monitor to the right side of a computer and it should work.

http://pastebin.com/ZysDSzdb
Bomb Bloke #5
Posted 04 December 2015 - 01:09 PM
If I understand you correctly, you're saying that on executing this script, "using" (right-clicking) the attached monitor does not result in the computer printing "TOUCH" (or anything else, for that matter)? Just browsing the code, I confess I can't see how that would be the case…

The only obvious "error" that I'm seeing is line 60, which should be using "greater/lessor or equal to" for its checks, like so:

if var_x >= var_buttonList[var_loop1][0] and var_y == var_buttonList[var_loop1][1] and var_x <= var_buttonList[var_loop1][2] then

But I'd still expect you to get some output regardless.

Some notes on yielding, which I feel may be relevant here:

When a computer/turtle starts running code, ComputerCraft starts a ~ten second timer. If that code doesn't yield before that timer ends then ComputerCraft will either crash the script or the whole computer (depending on the nature of the functions your script is calling). After each yield, any other systems waiting to run code may do so, then after they've all yielded, processing continues with a new time limit.

The reason why is that running your code chews up valuable server processing power, and so it shouldn't be able to monopolise it. In fact, only ONE CC device can run code at a time: While one is doing something, none of the others can do anything until it yields.

Whether or not it takes more than ten seconds for your code to execute has a little to do with the power of the Minecraft server it's running on, and a lot to do with how often you allow your code to yield. Pulling events (eg getting typed characters or checking timers) triggers a yield, and many commands (eg turtle movements, sleeping, or getting text input from the user) have to pull events to work anyway. Basically, anything that triggers a pause is pulling an event in order to do it, and in order to pull an event the code yields.

The two functions in the parallel API work by switching control between the functions you pass it whenever they yield (as need be). In your code, you've got two functions you're currently passing to parallel.waitForAll(): awaitTouch and mainProcess. awaitTouch spends most of its time yielding, waiting for monitor touch events. mainProcess, on the other hand, never yields; instead, it performs all the work it has to do, and then completes. You could achieve the exact same result by simply calling mainProcess and then awaitTouch directly after it - there's no need to use the parallel API here at all. Your current calling process should indeed work as-is, it's simply making a redundant step.

(Mind you, if you'd used parallel.waitForAny(), then that'd result in your script terminating nearly instantly, as mainProcess() completes within an instant…)
areuz #6
Posted 04 December 2015 - 06:50 PM
Your understanding is correct, I don't get any output even If I right-click the monitor. Also, this is a W.I.P. code, so I was already planning to address some of the suggestions you mentioned. Currently I'm concentrating on the fact that the code should in fact work, but it doesn't. I haven't yet tried to reinstall the mod-pack, but I will right after I finish writing this. After hearing that it should in fact work, I'm more encouraged to re-install.

I'm going to post an update after that.
areuz #7
Posted 04 December 2015 - 07:05 PM
I just discovered the problem, It was totally off topic (topic of programming). I was running a modpack with the minecraft version of 1.4.2. I didn't think that this would be a problem, since it already had advanced monitors, and the purpose of advanced monitors is solely touch detection and color adjustment.

Sorry for bothering you, I should be good now.
Thank you for your answer!