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

Try to figure out things yourself before asking questions.

Started by Advert, 02 April 2012 - 12:15 PM
Advert #1
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:
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.
Cloudy #2
Posted 02 April 2012 - 05:32 PM
I second this. The Pro's are not there so that they can tell you how to do something from scratch - you should try doing it yourself, and then if you get stuck, feel free to ask questions. It will be a much more rewarding experience that way :)/>/>
MysticT #3
Posted 15 July 2012 - 04:40 PM
When your program doesn't work like you expected, try to comment each line explaining what it does. That way, you'll see if something is wrong, or at least people will know what you'r trying to do and will be easier to help you.