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

[LUA]Functions

Started by BlackPerks, 08 July 2014 - 07:07 AM
BlackPerks #1
Posted 08 July 2014 - 09:07 AM
Hello,

I have written a function to mine a set number of blocks and I'm calling this function from another programe, the only way I can get the function to be called is to run the function prior to runing the programe.

I'm using a disk and disk drive and running the programe from that.

My question is what do I need to do to avoid having to run the function first.

Thanks
Lyqyd #2
Posted 08 July 2014 - 04:14 PM
Please post the code from both files.
BlackPerks #3
Posted 08 July 2014 - 05:07 PM
May take me a little while to get a reply as I managed to drop the disk into lava.
Lyqyd #4
Posted 08 July 2014 - 05:35 PM
If it is a single player game and you remember the disk ID, just go to the save folder and look in computer/disk/(ID) for the files.
BlackPerks #5
Posted 08 July 2014 - 07:15 PM
function digOne()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnRight()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end


while turtle.detect() do
digOne()
end
flaghacker #6
Posted 08 July 2014 - 08:56 PM
You can make an API, see http://computercraft.info/wiki/Os.loadAPI for more info.

But why would you need to have 2 different files? Just put all of that code in one file.

In your current setup you have to run the program with the function first, because it creates a global function which is used by your other program. I recommend reading this to learn more about "scopes": http://lua-users.org/wiki/ScopeTutorial
Edited on 08 July 2014 - 07:00 PM
BlackPerks #7
Posted 08 July 2014 - 09:57 PM
Ok thanks thats, it makes sense now. :D/>