Writing your own turtle functions

Started by Inksaver, Dec 17, 2020, 01:11 PM

Previous topic - Next topic

Inksaver

Just a little tip when you are writing your own functions for a multiple purpose, eg turtle.inspect()

local function getBlockType(direction)
local blockType = "" -- default return value

local inspect = turtle.inspect -- NO BRACKETS! -- re-assigh turtle API function to variable
if direction:lower() == "up" then
inspect = turtle.inspectUp -- NO BRACKETS!
elseif direction:lower() == "down" then
inspect = turtle.inspectDown -- NO BRACKETS!
else
if direction:lower() ~= "forward" then -- wrong or missing parameter supplied so exit with error message
print("Incorrect parameter 'direction' supplied")
error()
end
end

local success, data = inspect() --WITH BRACKETS! will now use correct version of turtle API
if success then -- inspect ok, not air in chosen direction
blockType = data.name
end

return blockType, data -- either empty string or block name, data either nil or other info
end
use: blockType, data = getBlockType("up")

the ability to assign a function to a variable is useful. It also works in Python, if you are studying that language as well.
Sent from a computer using a keyboard and mouse.

Lupus590

It would be better to use the message arg of error.

You might also want to use the second arg which is the level to blame as the source of the error, 1 is the default and is the function which calls error, 2 would be the caller of that function which is useful for errors caused by bad args. In your case, you would want to set it to 2.

QuickMuffin8782

#2
You can also use CraftOS's expect function:
local expect = require("cc.expect").expect
--expect(<arg num>, <variable for arg>, <types (can be seperated for multiple by commands)>)

-- Example expect: --
expect(1, 0, "string")
expect(1, 0, "string", "number", "nil")
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord

Inksaver

@Lupus590
As this was posted as a tutorial, I felt it was easier to understand a printed message stating what the developer had done wrong, followed by the error() function used as a procedure simply to bring the script to an abrupt halt, but you raised some salient points useful in a more advanced discussion.

@QuickMuffin8782
I was not aware of this function, which looks very useful. As I say to my students, I may have been programming for 40 years, but I learn something new every day. Has this been implemented since the beginning of CraftOS, or is it something new? It is important that anything I produce is compatible with all versions, as I use MC 1.7.10 for teaching purposes (along with Project Red for logic gates).
Sent from a computer using a keyboard and mouse.

Lupus590

Expect is newer than the last CC for MC 1.7.10, you might have some luck with my ROM backport though.

https://github.com/Lupus590-CC/ROM-BackPort

QuickMuffin8782

#5
No problem. This function has been implemented into CraftOS for a very long time. If you want to learn all the Lua 5.1 functions and CC functions you can study the rom/modules directory and have a go at https://tweaked.cc/.
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord