Jump to content




Stoping a Loop inside a Function


  • You cannot reply to this topic
7 replies to this topic

#1 gustavowizard

  • Members
  • 94 posts

Posted 06 July 2014 - 01:53 AM

hey guys im trying to stop a loop inside a function(on), using another function (0ff), on the code below:

--- Decorative Sonar by Gustavo Wizard
--- Monitor 2x2, btn API
os.loadAPI("btn")
m = peripheral.wrap("top")
local commandBlock = peripheral.wrap("left")
currentCommand = commandBlock.getCommand()
m.setTextScale(0.5)
m.setBackgroundColor(colors.black)
m.clear()
m.setTextColor(colors.red)
m.setCursorPos(8,2)
m.write("TYPHOON TK-208 SONAR")
term.redirect(m)
m.setTextColor(colors.white)
function fillTable()
btn.setTable("SONAR ON", on, 6,16,20,22)
btn.setTable("SONAR OFF", off,20,30,20,22)
btn.screen()
end
function getClick()
event,side,x,y = os.pullEvent("monitor_touch")
btn.checkxy(x,y)
end

function on()
btn.flash("SONAR ON")
while true do     --- here is what i need to change, this loop must stop when i press the button of the function off 
local image = paintutils.loadImage("r.nfp")
paintutils.drawImage(image,8,4)
sleep(.3)
local image = paintutils.loadImage("r2.nfp")
paintutils.drawImage(image,8,4)
sleep(.3)
local image = paintutils.loadImage("r3.nfp")
paintutils.drawImage(image,8,4)
sleep(.3)
local image = paintutils.loadImage("r4.nfp")
paintutils.drawImage(image,8,4)
commandBlock.setCommand("playsound wizard.sonar_low @a")
commandBlock.runCommand()
sleep(.3)
end
end
m.setBackgroundColor(colors.red)
m.setTextColor(colors.white)
function off()
btn.flash("SONAR OFF")
end
fillTable()
while true do
getClick()
end

http://pastebin.com/0BmmcrEs

can anybody give a hand? thanks!

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 06 July 2014 - 02:31 AM

In Lua there is a keyword break which you can use to exit loops. If you also wish to exit the function at the same time you could also use return. Lua Manual page.

#3 gustavowizard

  • Members
  • 94 posts

Posted 06 July 2014 - 04:51 AM

yes but where i put it to make it work on the code? i must put some code inside the function off() so it can tell the function on() to stop

#4 Bomb Bloke

    Hobbyist Coder

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

Posted 06 July 2014 - 05:08 AM

At the moment, your on() function is running a loop that repeats while "true" is true (that is to say, indefinitely).

Instead, how about making it repeat so long as a variable - eg "running" - is true, then allow the off() function to toggle the state of that variable?

#5 gustavowizard

  • Members
  • 94 posts

Posted 06 July 2014 - 05:57 AM

here is the API im using, from direwolf20; what i need is a way to use the btn.toggleButton function to set the Loop true when its toggle and false when its not, i cant do with repeat to beacuse the in what way wont work i need to use 1 function toggle instead of 2 flash, what i just need to know is how to say like 'if toggle is true then' but still aint found the right code

local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
	
function setTable(name, func, xmin, xmax, ymin, ymax)
   button[name] = {}
   button[name]["func"] = func
   button[name]["active"] = false
   button[name]["xmin"] = xmin
   button[name]["ymin"] = ymin
   button[name]["xmax"] = xmax
   button[name]["ymax"] = ymax
end
function funcName()
   print("You clicked buttonText")
end
	   
function fillTable()
   setTable("ButtonText", funcName, 5, 25, 4, 8)
end	
function fill(text, color, bData)
   mon.setBackgroundColor(color)
   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
   for j = bData["ymin"], bData["ymax"] do
	  mon.setCursorPos(bData["xmin"], j)
	  if j == yspot then
		 for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
		    if k == xspot then
			   mon.write(text)
		    else
			   mon.write(" ")
		    end
		 end
	  else
		 for i = bData["xmin"], bData["xmax"] do
		    mon.write(" ")
		 end
	  end
   end
   mon.setBackgroundColor(colors.black)
end
	
function screen()
   local currColor
   for name,data in pairs(button) do
	  local on = data["active"]
	  if on == true then currColor = colors.lime else currColor = colors.red end
	  fill(name, currColor, data)
   end
end
function toggleButton(name)
   button[name]["active"] = not button[name]["active"]
   screen()
end	
function flash(name)
   toggleButton(name)
   screen()
   sleep(0.15)
   toggleButton(name)
   screen()
end
											
function checkxy(x, y)
   for name, data in pairs(button) do
	  if y>=data["ymin"] and  y <= data["ymax"] then
		 if x>=data["xmin"] and x<= data["xmax"] then
		    data["func"]()
		    return true
		    --data["active"] = not data["active"]
		    --print(name)
		 end
	  end
   end
   return false
end
	
function heading(text)
   w, h = mon.getSize()
   mon.setCursorPos((w-string.len(text))/2+1, 1)
   mon.write(text)
end
	
function label(w, h, text)
   mon.setCursorPos(w, h)
   mon.write(text)
end


#6 gustavowizard

  • Members
  • 94 posts

Posted 07 July 2014 - 07:43 PM

View PostBomb Bloke, on 06 July 2014 - 05:08 AM, said:

At the moment, your on() function is running a loop that repeats while "true" is true (that is to say, indefinitely).

Instead, how about making it repeat so long as a variable - eg "running" - is true, then allow the off() function to toggle the state of that variable?

i try that dont work, it still stay on the loop and the button dont work to stop it while it is on the loop

#7 Bomb Bloke

    Hobbyist Coder

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

Posted 08 July 2014 - 12:24 AM

You'd need to implement something like the parallel API to catch clicks while the on() function was running.

But really the whole structure of the script could be reworked to simplify things:

Spoiler


#8 gustavowizard

  • Members
  • 94 posts

Posted 08 July 2014 - 06:42 PM

View PostBomb Bloke, on 08 July 2014 - 12:24 AM, said:

You'd need to implement something like the parallel API to catch clicks while the on() function was running.

But really the whole structure of the script could be reworked to simplify things:

Spoiler

son of gun it works! lol thanks man, i see you are familiar with the direwolf20 button API :D
when i first look and the code i was like hum its missing some function calls but it happens it was me that dont know enough about it ;)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users