Jump to content




sigle time execution in a loop. HELP!


17 replies to this topic

#1 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 13 May 2017 - 10: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.

#2 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 13 May 2017 - 03:09 PM

Just put the statement outside of the loop

#3 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 13 May 2017 - 06: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 by Lupus590, 13 May 2017 - 06:06 PM.


#4 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 14 May 2017 - 03: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

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 May 2017 - 05: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.

#6 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 14 May 2017 - 04:05 PM

View PostBomb Bloke, on 14 May 2017 - 05:12 AM, said:

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 by qwerty, 14 May 2017 - 04:11 PM.


#7 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 14 May 2017 - 04: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 by qwerty, 14 May 2017 - 04:26 PM.


#8 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 14 May 2017 - 04:27 PM

View Postqwerty, on 14 May 2017 - 04:19 PM, said:

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.

#9 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 14 May 2017 - 05:08 PM

View PostSquidDev, on 14 May 2017 - 04:27 PM, said:

View Postqwerty, on 14 May 2017 - 04:19 PM, said:

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).

#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 May 2017 - 11:23 PM

View Postqwerty, on 14 May 2017 - 05:08 PM, said:

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".

View Postqwerty, on 14 May 2017 - 05:08 PM, said:

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.

#11 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 15 May 2017 - 12: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

#12 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 15 May 2017 - 03:09 PM

View Postqwerty, on 15 May 2017 - 12:37 PM, said:

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 by Lupus590, 15 May 2017 - 03:10 PM.


#13 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 15 May 2017 - 04:28 PM

View PostLupus590, on 15 May 2017 - 03:09 PM, said:

View Postqwerty, on 15 May 2017 - 12:37 PM, said:

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 by qwerty, 15 May 2017 - 04:30 PM.


#14 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 15 May 2017 - 05:30 PM

View Postqwerty, on 15 May 2017 - 04:28 PM, said:

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 by Lupus590, 15 May 2017 - 05:31 PM.


#15 houseofkraft

  • Members
  • 170 posts
  • LocationUSA

Posted 15 May 2017 - 08:07 PM

You want to replace
while true do
with
repeat

More Information Here

Edited by houseofkraft, 15 May 2017 - 08:11 PM.


#16 valithor

  • Members
  • 1,053 posts

Posted 16 May 2017 - 02:53 AM

View Posthouseofkraft, on 15 May 2017 - 08:07 PM, said:

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.

View Postqwerty, on 14 May 2017 - 03:17 AM, said:

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 by valithor, 17 May 2017 - 03:28 AM.


#17 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 16 May 2017 - 01:42 PM

View PostLupus590, on 15 May 2017 - 05:30 PM, said:

View Postqwerty, on 15 May 2017 - 04:28 PM, said:

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 by qwerty, 16 May 2017 - 02:02 PM.


#18 qwerty

  • Members
  • 72 posts
  • LocationThe Sand Dunes

Posted 16 May 2017 - 02:15 PM

View Postvalithor, on 16 May 2017 - 02:53 AM, said:

View Posthouseofkraft, on 15 May 2017 - 08:07 PM, said:

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.

View Postqwerty, on 14 May 2017 - 03:17 AM, said:

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 by qwerty, 16 May 2017 - 02:16 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users