Posted 02 April 2012 - 02:15 PM
You'll be surprised how many times you'll be able to answer your question on your own.
If you wonder how a function works, you can check help <api> ingame, then you can mess around with it in the lua shell:
Let's say you wanted to know what fs.open() returned, and how you could use it.
What did that just do?
We captured ALL of the arguments returned by fs.open in a table, then put that table in x.
We can now find out what fs.open returned:
Wait! But it didn't print anything?
What could this mean? Let's look in our folder
Let's try with a file that we create.
Now, we have created the file, and we're ready to test again.
Seems like fs.open() will only return a table, and only if the file exists, when using the "r" flag.
Let's see what the table contains.
Looking at those, they are pretty self-explanatory.
readLine: Probably reads a line.
readAll: Probably reads everything.
close: closes the file (VERY, VERY, important.)
Seeing this, we can try:
Would you look at that. It returned the line of text we wrote in the file earlier! And reading additional lines didn't do anything.
Let's close up shop, and continue programming $currentproject using our newfound knowledge!
You could also google a bit, and try the various pieces of code you find.
This doesn't mean that you can't ask, though. It just means you should give it a try before asking. The above example can be applied to errors, too, but you'd look for what was wrong first, and you'd probably put a lot of print("var: ", var) to see what value variables are when the program is running.
If you wonder how a function works, you can check help <api> ingame, then you can mess around with it in the lua shell:
Spoiler
CraftOS 1.3
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> _
Let's say you wanted to know what fs.open() returned, and how you could use it.
lua> x = {fs.open("test", "r")}
lua> _
What did that just do?
We captured ALL of the arguments returned by fs.open in a table, then put that table in x.
We can now find out what fs.open returned:
lua> for k, v in pairs(x) do print(k, ", " v) end
lua> _
Wait! But it didn't print anything?
What could this mean? Let's look in our folder
lua> exit()
> ls
disk rom
Hmm, test doesn't exist.Let's try with a file that we create.
edit test
<blank screen with blinking cursor>
This is a triumph.<ctrl><enter><ctrl><right><enter>
> lua
Interactive Lua prompt.
Call exit() to exit.
lua>
Now, we have created the file, and we're ready to test again.
lua> x = {fs.open("test", "r")}
lua> for k, v in pairs(x) do print(k, ", " v) end
1, table: B33FC4CE
Seems like fs.open() will only return a table, and only if the file exists, when using the "r" flag.
Let's see what the table contains.
lua> for k, v in pairs(x[1]) do print(k, ", ", v) end
readLine, function: 12ABCDEF
readAll, function: FE3DB3EF
close, function: 12345677
Looking at those, they are pretty self-explanatory.
readLine: Probably reads a line.
readAll: Probably reads everything.
close: closes the file (VERY, VERY, important.)
Seeing this, we can try:
lua> x[1].readLine()
This is a triumph.
lua>x[1].readLine()
lua>
Would you look at that. It returned the line of text we wrote in the file earlier! And reading additional lines didn't do anything.
Let's close up shop, and continue programming $currentproject using our newfound knowledge!
lua> x[1].close()
You could also google a bit, and try the various pieces of code you find.
This doesn't mean that you can't ask, though. It just means you should give it a try before asking. The above example can be applied to errors, too, but you'd look for what was wrong first, and you'd probably put a lot of print("var: ", var) to see what value variables are when the program is running.