Jump to content




Leave function

api computer lua

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

#1 solber

  • New Members
  • 1 posts

Posted 21 May 2015 - 02:40 PM

Hi guys, i got a problem and I need your help to fix it, so i'm working on program, it work but i cant leave function so my program not work totaly, this is the program : http://pastebin.com/3WsQrvGX

And this is the function :

  • function getbtn()


  • request(81, requestG[i])


  • id, message = rednet.receive()


  • if(id==81) then


  • if(message=="true") then


  • can=false


  • if(i==1) then


  • bt1ac()


  • elseif(i==2) then


  • bt2ac()


  • elseif(i==3) then


  • bt3ac()


  • end


  • else


  • can=false


  • if(i==1) then


  • bt1inac()


  • elseif(i==2) then


  • bt2inac()


  • elseif(i==3) then


  • bt3inac()


  • end


  • end


  • end


  • if(i==3) then


  • print(can)


  • end


  • if(i <= 3) then


  • i=i+1


  • getbtn()


  • end


  • end -- end function getbtn


#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 21 May 2015 - 03:23 PM

Moved to Ask a Pro.

#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 21 May 2015 - 03:28 PM

put your code in code tags

#4 Kaikaku

  • Members
  • 46 posts

Posted 21 May 2015 - 05:35 PM

The function should already end when i becomes 5 (for the pastebin version or 4 for the version in this thread).

If you want to exit earlier you can add for example 'return true'.

Edited by Kaikaku, 21 May 2015 - 05:37 PM.


#5 valithor

  • Members
  • 1,053 posts

Posted 21 May 2015 - 06:11 PM

I am actually suprised no one else has said this yet...

The function you posted could be better written as this:

function getbtn()
  for i = 1, 4 do
	request(81, requestG[i])

	id, message = rednet.receive(5)

	if(id==81) then
	  if(message=="true") then
		can=false
		if(i==1) then
		   bt1ac()
		elseif(i==2) then
		   bt2ac()
		elseif(i==3) then
		   bt3ac()
		elseif(i==4) then
		   bt4ac()
		end
	  else
		can=false
		if(i==1) then
		   bt1inac()
		elseif(i==2) then
		   bt2inac()
		elseif(i==3) then
		   bt3inac()
		elseif(i==4) then
		   bt4inac()
		end
	  end
	end
	if(i==4) then
	  print(can)
	  print(i)
	end
  end
end -- end function getbtn

This will do the exact same thing, but without the need to keep track of the variable yourself, or to use recursion.

This will utilize a for loop. The i=1 is defining i as the counter variable. Each iteration it will increase by one until it reaches the second number in this case 4, and it will run all code between the start of the for loop and the end associated with it until then.

As KaiKaku said, this code will only run 4 times. There is no reason it wouldn't end after the 4th itteration.

Edited by valithor, 21 May 2015 - 06:30 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users