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

Two Questions?

Started by Ristyo, 20 September 2013 - 02:16 AM
Ristyo #1
Posted 20 September 2013 - 04:16 AM
So i am still new to Lua after a long time, and i still dont know many useful functions.

Is there a function that will execute a line of code?
Also what does the Coroutine API does?
immibis #2
Posted 20 September 2013 - 04:24 AM
Execute a line of code (or multiple lines):

ok, result = loadstring(stringWithCode)
if not ok then
  -- Syntax error. Error message is stored in "result"
  print("Error: ", result)
else
  -- OK. Code is stored in a "result" as a function, as if you had done "function result() codegoeshere end"
  -- so you can call it whenever. If you want to catch errors here then use pcall (a built-in function).
  print("Running code")
  result()
  print("Finished running code")
end

Coroutines: let you pause code and resume it later. The CC event system uses them.
theoriginalbit #3
Posted 20 September 2013 - 05:28 AM
It should also be noted that coroutines allow you to have the illusion of multitasking, even though they only run one at a time, they run so quickly that it looks like both are running at the same time.
Ristyo #4
Posted 21 September 2013 - 12:10 PM
No I mean is there a function that will execute a certain line of code even it has been executed? Example : make that function run line 15 even if it has been executed

Is there a function that converts strings into number values?
Also is there a function that picks a random number from a defined parameter that can be stored into a variable?
electrodude512 #5
Posted 21 September 2013 - 12:33 PM
How about a while loop?

i=0
while i<10 do
  print(i)
  i=i+1
end
prints:

0
1
2
3
4
5
6
7
8
9

To convert a string to a number use: tonumber(string)
Ristyo #6
Posted 21 September 2013 - 12:38 PM
No I mean if the program has executed line 15 is there a way to tell the program to execute line 15 again without using functions or loops?
theoriginalbit #7
Posted 21 September 2013 - 12:40 PM
No I mean is there a function that will execute a certain line of code even it has been executed? Example : make that function run line 15 even if it has been executed
No I mean if the program has executed line 15 is there a way to tell the program to execute line 15 again without using functions or loops?
No, I am glad to tell you there is no goto in the version of Lua that CC uses. Use a function, there is no instance where you cannot use a function.

Is there a function that converts strings into number values?
As electrodude512 stated use tonumber, it should also be noted that tonumber can also be given a base that specifies the base the string is in, for example to convert a hexadecimal (base-16) string to a number you'd do tonumber("FFF", 16)

Also is there a function that picks a random number from a defined parameter that can be stored into a variable?
math.random
Ristyo #8
Posted 21 September 2013 - 12:54 PM
Well, I am making some sort of menu, and if that menu ran some function or program, and when it's done would not return to the menu itself, but when I make the executed program from the menu ran the menu itself, I fear it would cause a stack overflow and I don't know how to do this because, like you said there's no goto function. Does coroutine do what I exactly needed?

I used the math.random once, but it did not work as expected. I used math.random like this :
number = 1
random = math.random(1,3)
if number == random then
print("It is the same!")
end
(sorry if the code is not in the code forum function, I am posting from my iPad)
It doesn't work, instead of printing "It is the same!" or do nothing, instead it will return the random number it got.

(Wow this forum replies really fast, compared to the BSF forums.)
theoriginalbit #9
Posted 21 September 2013 - 01:04 PM
Well, I am making some sort of menu, and if that menu ran some function or program, and when it's done would not return to the menu itself, but when I make the executed program from the menu ran the menu itself, I fear it would cause a stack overflow and I don't know how to do this because, like you said there's no goto function. Does coroutine do what I exactly needed?
Not in the slightest. The as I stated before coroutines are there to give the sense of multitasking even though it isn't. You are correct that you would have got a stack overflow from the function call stack filling up from the recursive call. A loop is definitely your solution here.

An example menu in a loop:

local function foo()
  term.clear()
  print("bar!")
  sleep(2)
end

while true do --# infinitely loop
  term.clear()
  term.setCursorPos(1,1)
  print("Menu")
  print("1. Foo")
  print("2. Exit")
  write("Selection: ")
  local input = tonumber(read())
  if not input then
	print("Please enter a number only")
	sleep(1)
  elseif input == 1 then
	foo()
  elseif input == 2 then
	print("Goodbye")
	return
  else
	print("Invalid selection")
  end
end

I used the math.random once, but it did not work as expected. I used math.random like this :

number = 1
random = math.random(1,3)
if number == random then
  print("It is the same!")
end
It doesn't work, instead of printing "It is the same!" or do nothing, instead it will return the random number it got.
Chances are you had more code than that, that code would work perfectly.

sorry if the code is not in the code forum function, I am posting from my iPad
You can manually type the BBCode for the code tags, its [code][/code]
Ristyo #10
Posted 21 September 2013 - 01:17 PM
No it's a different type of menu, that I tried a few months ago and I still couldn't figure it out
It is the type of menu that looks like the one in the Turtle OS (damn I forgot the code)
theoriginalbit #11
Posted 21 September 2013 - 01:20 PM
Menu in Turtle OS? o.O Do you mean the shell? Where you can type in commands or program names and it runs?
Ristyo #12
Posted 21 September 2013 - 01:26 PM
No the one that shows the menu selections and it has a pointer on the selected menu, if you press the arrow keys up or down, the menu selection pointer will move or change position depending on the key press
(well its kinda hard to explain)
theoriginalbit #13
Posted 21 September 2013 - 01:30 PM
What default program has that (other than edit)?

Its still just as simple. Have an infinite loop, detect key presses and redraw the menu when there was a key press.
Ristyo #14
Posted 21 September 2013 - 01:33 PM
Yes I already figured that out but how to make it run a program in the selection in a way that doesn't cause a stack overflow
TurtleSHELL OS that is what I mean
http://www.computercraft.info/forums2/index.php?/topic/6859-14-turtleshell-customizable-os-with-screen-savers-and-more/page__hl__turtleshell__fromsearch__1

(sorry if I was very confusing)
Ristyo #15
Posted 24 September 2013 - 09:03 AM
So i got another two questions :
1. How to make a multitasking program?
2. How to block people from accessing certain files?
Thanks, but i hope posting in a thread last updated three days ago doesn't count as bumping or necroing.
Lyqyd #16
Posted 24 September 2013 - 12:21 PM
Look at the parallel API after learning about coroutines to get a better idea of how multitasking is done in ComputerCraft.
robotica34 #17
Posted 25 September 2013 - 07:27 AM
No I mean if the program has executed line 15 is there a way to tell the program to execute line 15 again without using functions or loops?
I might be late to reply to this, but you can use coroutines and functions together to create things like exiting out of the function, call another function, and pass some information to the suspended coroutine (or function). That might work just like the goto command in batch.
immibis #18
Posted 25 September 2013 - 08:21 PM
So i got another two questions :
1. How to make a multitasking program?
Use the parallel API. You don't need to understand coroutines to use parallel, it's dead simple.
2. How to block people from accessing certain files?
Set fs.open to your own function that checks the filename and then calls the old fs.open.
Ristyo #19
Posted 27 September 2013 - 02:29 AM
Hmm, i have another question. It is quite off topic from computercraft, but it is related to Lua programming, so i think it is okay to ask here.
If i make aprogram outside of CC with some LuaIDE (or in this case ZeroBrane) what APIs i can use? Can i load an API someone made or i've made myself?