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

sigle time execution in a loop. HELP!

Started by qwerty, 13 May 2017 - 09:56 AM
qwerty #1
Posted 13 May 2017 - 11:56 AM
So I'm working on a reactor controller from ic2 and I want to know how to execute a line of code once until a condition is met. Like instead of spamming the console with "Message sent…" until I can't see anymore what's going on and to toggle the reactor like a hundred times a second and also not to overshoot the energy storage cap I have in the setup. I have wasted more than 3 hours on it and I wold appreciate if you could reply A.S.A.P. I need it done by tomorrow of the date of posting because school starts tomorrow! (I live in Egypt ad here friday is sunday)

With regards
Qwerty.
Emma #2
Posted 13 May 2017 - 04:09 PM
Just put the statement outside of the loop
Lupus590 #3
Posted 13 May 2017 - 07:05 PM
It's hard to give more help when we can't see code. Upload it to pastebin.com and post the link in this thread
Edited on 13 May 2017 - 05:06 PM
qwerty #4
Posted 14 May 2017 - 04:17 AM
HI, so here's the code https://pastebin.com/4hDvzCKb and I don't mean executing when the loop breaks but instead to execute it once in the loop until a condition is met like you send a message and when a condition is true i.e. the energy storage is full the rednet message gets sent again btw the "sides" API is just to open the rednet sides made by the one, the only Lyqyd! Anyways thank you for your future help.

with regards
Qwerty
Bomb Bloke #5
Posted 14 May 2017 - 06:12 AM
Looks like you want something along these lines:

peripheral.find("modem", rednet.open)

while true do 
	if not rs.getInput("left") then
		rednet.send(1, "")
		print("Message sent...")
	end
	
	os.pullEvent("redstone")
end

If you truly want it to send one message, full stop, then stick a "break" after the "print" line.
qwerty #6
Posted 14 May 2017 - 05:05 PM
Looks like you want something along these lines:

peripheral.find("modem", rednet.open)

while true do
	if not rs.getInput("left") then
		rednet.send(1, "")
		print("Message sent...")
	end
	
	os.pullEvent("redstone")
end

If you truly want it to send one message, full stop, then stick a "break" after the "print" line.
it's good but I edited a little of the code since it didn't do what I hoped for, tho now it works, so thank you!
Edited on 14 May 2017 - 03:11 PM
qwerty #7
Posted 14 May 2017 - 05:19 PM
Can I ask you Mr.Bloke, what is the purpose of os.pullEvent() at the end of the code? I see no use for it…
Edit: Never mind it crashes minecraft if removed.
Edited on 14 May 2017 - 03:26 PM
SquidDev #8
Posted 14 May 2017 - 05:27 PM
Can I ask you Mr.Bloke, what is the purpose of os.pullEvent() at the end of the code? I see no use for it…
That instructs the computer to wait until the redstone input changes again. If you didn't have that then your computer will be trying to check the redstone input several thousand times a second which, as you can imagine, is rather inefficient. As you've noticed, these loops can also cause all sorts of other problems. ComputerCraft generally will abort them if it detects them, though this doesn't always work.
qwerty #9
Posted 14 May 2017 - 06:08 PM
Can I ask you Mr.Bloke, what is the purpose of os.pullEvent() at the end of the code? I see no use for it…
That instructs the computer to wait until the redstone input changes again. If you didn't have that then your computer will be trying to check the redstone input several thousand times a second which, as you can imagine, is rather inefficient. As you've noticed, these loops can also cause all sorts of other problems. ComputerCraft generally will abort them if it detects them, though this doesn't always work.
Thank you for your sorta, not really usable information. tho how do I get it to send the message when the redstone gets toggled on to off because right nw I just restart the computer to achive what I want (extremeley in-efficient, I know).
Bomb Bloke #10
Posted 15 May 2017 - 12:23 AM
Thank you for your sorta, not really usable information.

See here, then. Basically the line translates to "wait here and do nothing until a redstone signal changes".

tho how do I get it to send the message when the redstone gets toggled on to off

That's what the code I showed you does. Hard to comment on your modified version without seeing it.
qwerty #11
Posted 15 May 2017 - 01:37 PM
well since I succ at describing my troubles I meant when the redstone gets turned on or off it sends a message only once per loop basically when redstone changes send a message (I'm really sorry if I've wasted anyone's time asking these (useless and dumb) questions)

with regards
Qwerty
Lupus590 #12
Posted 15 May 2017 - 04:09 PM
well since I succ at describing my troubles I meant when the redstone gets turned on or off it sends a message only once per loop basically when redstone changes send a message (I'm really sorry if I've wasted anyone's time asking these (useless and dumb) questions)

with regards
Qwerty

Adapting BomeBlokes code:


peripheral.find("modem", rednet.open)
local previousState
while true do
		if previousState ~= rs.getInput("left") then
				previousState = rs.getInput("left")
				rednet.send(1, "")
				print("Message sent...")
		end
	  
		os.pullEvent("redstone")
end
Edited on 15 May 2017 - 02:10 PM
qwerty #13
Posted 15 May 2017 - 05:28 PM
well since I succ at describing my troubles I meant when the redstone gets turned on or off it sends a message only once per loop basically when redstone changes send a message (I'm really sorry if I've wasted anyone's time asking these (useless and dumb) questions)

with regards
Qwerty

Adapting BomeBlokes code:


peripheral.find("modem", rednet.open)
local previousState
while true do
		if previousState ~= rs.getInput("left") then
				previousState = rs.getInput("left")
				rednet.send(1, "")
				print("Message sent...")
		end
	  
		os.pullEvent("redstone")
end
nice program but it doesn't work could you explain why it doesn't work? I read the code and seems logic to me but it just doesn't want to work… or maybe it's because I'm playing 1.10.2 with the pre-release of 1.9.4 with unborked
Edited on 15 May 2017 - 03:30 PM
Lupus590 #14
Posted 15 May 2017 - 06:30 PM
nice program but it doesn't work could you explain why it doesn't work? I read the code and seems logic to me but it just doesn't want to work… or maybe it's because I'm playing 1.10.2 with the pre-release of 1.9.4 with unborked

what does the code do? explain what is not working.
Edited on 15 May 2017 - 04:31 PM
houseofkraft #15
Posted 15 May 2017 - 09:07 PM
You want to replace
while true do
with
repeat

More Information Here
Edited on 15 May 2017 - 07:11 PM
valithor #16
Posted 16 May 2017 - 03:53 AM
You want to replace
while true do
with
repeat

More Information Here

What is the point in using a repeat loop? It would just end up being trading one infinite loop for another.

HI, so here's the code https://pastebin.com/4hDvzCKb and I don't mean executing when the loop breaks but instead to execute it once in the loop until a condition is met like you send a message and when a condition is true i.e. the energy storage is full the rednet message gets sent again btw the "sides" API is just to open the rednet sides made by the one, the only Lyqyd! Anyways thank you for your future help.

with regards
Qwerty

From my interpretation of what you are wanting to do, it would be something more like this:


local sent = false --# variable to keep track of whether or not we've sent a message
peripheral.find("modem", rednet.open)

while true do

  if redstone.getInput("left") == false then
	if sent == false then --# only send the message if the variable is false
	  sent = true
	  rednet.send(1,"")
	  print("Message sent...")
	end
  else
	sent = false
  end

  os.pullEvent("redstone") --# pullEvent to make it not constantly check
end

For future reference… "it just doesn't want to work… " does not help us help you. Please give how it does not work. Give what it does, such as it errors, or it doesn't send a message (if it doesn't send a message is it printing that a message is sent). We can't begin to help you fix the problem if you don't tell us the problem.
Edited on 17 May 2017 - 02:28 AM
qwerty #17
Posted 16 May 2017 - 02:42 PM
nice program but it doesn't work could you explain why it doesn't work? I read the code and seems logic to me but it just doesn't want to work… or maybe it's because I'm playing 1.10.2 with the pre-release of 1.9.4 with unborked

what does the code do? explain what is not working.
everything except the first time it sends the message and then nothing happens.
I even added an os.pullEvent printer but that showed nothing except key strokes.
Edited on 16 May 2017 - 01:02 PM
qwerty #18
Posted 16 May 2017 - 03:15 PM
You want to replace
while true do
with
repeat

More Information Here

What is the point in using a repeat loop? It would just end up being trading one infinite loop for another.

HI, so here's the code https://pastebin.com/4hDvzCKb and I don't mean executing when the loop breaks but instead to execute it once in the loop until a condition is met like you send a message and when a condition is true i.e. the energy storage is full the rednet message gets sent again btw the "sides" API is just to open the rednet sides made by the one, the only Lyqyd! Anyways thank you for your future help.

with regards
Qwerty

From my interpretation of what you are wanting to do, it would be something more like this:


local sent = false --# variable to keep track of whether or not we've sent a message
peripheral.find("modem", rednet.open)

while true do

  if redstone.getInput("left") == false then
	if sent == false then --# only send the message if the variable is false
	  sent = true
	  rs.send(1,"")
	  print("Message sent...")
	end
  else
	sent = false
  end

  os.pullEvent("redstone") --# pullEvent to make it not constantly check
end

For future reference… "it just doesn't want to work… " does not help us help you. Please give how it does not work. Give what it does, such as it errors, or it doesn't send a message (if it doesn't send a message is it printing that a message is sent). We can't begin to help you fix the problem if you don't tell us the problem.
HI valithor, did you test it because when I try to run it, it gives me this error: "<program name>:7: attempt to call nil" anyways I am going to stop to waste anyone's time so feel free to not reply to this post!

Best regards
qwerty
Edited on 16 May 2017 - 01:16 PM