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

[1.7.10/F1258/C1.65] pcall not working in startup mode

Started by Arin, 10 January 2015 - 04:26 PM
Arin #1
Posted 10 January 2015 - 05:26 PM
Hi

I tried to use the pcall in my newest program (it's a small code for controlling a stargate, see SG Craft for more info), and it usually works greatly and as intended, except in the case when I try to run the code as the 'startup' program - and that's the actual environment that I want my program to run!
Whenever I reboot the computer and have the startup program run by itself, the error will not be caught (if thrown) and will actually force the program to exit, showing as the default-style error message in the console.
If I run the 'startup' in the console by typing it, it will work as intended and will never exit when error do occur (as just stated above.)

I am baffled. Is there something I am missing or doing wrong?

The following is the code that I am currently testing (it's a mess really; I am not a proper programmer at all!):
Spoiler

sg = peripheral.wrap("left") --Gate Interface side
addr = "NN8L-G30-SA" --Gate dial address
side = "bottom" --Redstone input side
--This is a crude and temporary attempt to run the stargate.
while true do
  if( sg.stargateState() == "Idle") then
	if( redstone.getInput(side) ) then
	  print("Dialling...")
	  ok, result = pcall(sg.dial, addr) --Try to dial the address
	  --sg.dial(addr)
	  if ok then --WIll print true if successful, false if err
		print("Dial complete\n")
		sleep(300) --So that the program will wait until the gate shuts down automatically
	  else
		print("Dialling failed:  ", string.sub(result,8),"\n")
		sleep(2) --So that the message is not displayed more than once.  This is a crude implementation.
	  end
	  --sleep(300)
	end
  end
  sleep(0.5)
end


Thank you in advance.
Lyqyd #2
Posted 10 January 2015 - 08:53 PM
Might throw a sleep(0) at the very beginning. For whatever reason, code that works well when normally but fails when run as startup often ends up working with the inclusion of a sleep(0).
Arin #3
Posted 11 January 2015 - 07:51 AM
Thank you for your reply :)/> .

However, when I included sleep(0) at the start of the code as you suggested, the problem still persists. I even tried sleep(1), inserting sleep(0) at various places, but so far none has worked. T.T
Maybe I should re-write the program to pre-check if the error will occur and prevent the exception from being thrown at all, for the time being…

Could there be something that I might be forgetting?


p.s. Happy New Year! (although it's 11 days late.)
safetyscissors #4
Posted 11 January 2015 - 10:03 AM
Is it possible that pcall is working and something else is throwing a similar error? maybe sg.stargateState()
Arin #5
Posted 12 January 2015 - 02:42 PM
Hello, safetyscissors, thanks for your reply.

I was going to investigate your suggestion by wrapping stargateState() in a pcall statement, but before I did that, a minecraft *server* reboot (that was done because of another reason; it was a multiplayer world) was done and it seems to have somehow 'fixed' the problem. The program now works as intended in its original code, not spewing any error messages, up until the moment I am writing this message.
Also, I duplicated the setup in a new computer after the reboot (so as to simulate it being the 'new' computer) and it too does not throw the error. Perplexing :blink:/> .

However this may sound strange, I think maybe the best current solution is to reboot the server (not the computer), just as we restart OS (Windows™ in particular) in RL, if the issue happens. If it was a single player world, a simple quit-and-reentering the world would suffice, I presume.

Again, thank you all (Lyqyd, safetyscissors) for your help. If there is any change in situation or any new information, I will write again.

Have a nice day ;)/>

p.s. maybe the issue could be related to how computercraft behaves in MP mode?