←  Tutorials

ComputerCraft | Programmable Computers for Minecraft

»

Automaticly declare new functions in Lua p...

qwerty's Photo qwerty 14 Aug 2018

This is a simple way of integrating an API without actually having a separate file(except the mask file).

Why would you want this if you can just load APIs? - Well, let's suppose you have some simple functions like terminal clearing that aren't worth making an API but too much hassle to manually enter.

Well the simple solution to this would be to just make a file labeled 'lua' (for ease of differentiation or something).
The contents would look something like:
--[[this is a simple function to clear the terminal]]
function clear()
  term.clear()
  term.setCursorPos(1,1)
end

--[[another example]]
function betterClear(bool)
  if not bool then
	clear()
  else
	term.setBackgroundColor(colors.black)
	term.setForegroundColor(colors.white)
	clear()
  end
end


shell.run("rom/programs/lua.lua")

otherwise you can just put all that in an API and have a startup file with:
os.loadAPI(-name of API-)

But that creates some incompatibilities with other programs under certain circumstances but you can just simply edit the startup file.

-edited for a better tutoring experience-
Edited by qwerty, 10 June 2020 - 06:16 PM.
Quote