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

API Help

Started by nomimg1, 06 April 2015 - 08:19 AM
nomimg1 #1
Posted 06 April 2015 - 10:19 AM
Hello, I am writing an API to make creating OS's easier, but I ran into some caveats.

CYOS API

-- Create Your Own OS by nomimg1
-- CYOS 1.0
local osEnv = ""
local osMOTD = "BLANK OS"
osType = function(ostype)
  if ostype === "cmd" then osEnv = ostype
  else if ostype === "gui" then osEnv = ostype
  else print("Unknown OS Enviroment Type")
end
osStart = function()
  if ostype = "gui" then print("GUI Os's are coming soon.")
  if ostype = "cmd" then cyosi.cmdStart()
cmdStart = function()
   --Start
  term.clear()
  term.setCursorPos(1, 1)
  --
  end
end
osMotd = function(motd)
  osMOTD = motd
end

TESTOS

os.loadAPI("cyos")
cyos.osType("cmd")
cyos.osMotd("Test OS")
cyos.osStart()

Error:

bios:367: [string "cyos"]:7: unexpected symbol
testos:3: attempt to index ? (a nil value)
HPWebcamAble #2
Posted 06 April 2015 - 04:19 PM
Its a good start, but there are a few things to fix:

This is usually how a function is defined:

function name(args) 
  -# Do stuff
end
Your method works, but it makes it harder to read

Instead of 'else if', use 'elseif'

In if-statements, only use two equals to compare things (==)

You were missing a few 'end's, make sure to indent correctly, it helps you see any missing ends

Here's your CYOS API file, reformatted, and with a few typos fixed:

-- Create Your Own OS by nomimg1
-- CYOS 1.0
local osEnv = ""
local osMOTD = "BLANK OS"

function osType(setType)
  if ostype == "cmd" then 
    osEnv = ostype
  elseif ostype == "gui" then 
    osEnv = ostype
  else 
    print("Unknown OS Enviroment Type")
  end
end

function cmdStart()
  term.clear()
  term.setCursorPos(1,1)
end

function osStart()
  if ostype == "gui" then 
    print("GUI Os's are coming soon.")
  elseif ostype == "cmd" then 
    cmdStart()
  end
end

function osMotd(motd)
  osMOTD = motd
end
nomimg1 #3
Posted 06 April 2015 - 09:06 PM
Thanks.
Its a good start, but there are a few things to fix:

This is usually how a function is defined:

function name(args)
  -# Do stuff
end
Your method works, but it makes it harder to read

Instead of 'else if', use 'elseif'

In if-statements, only use two equals to compare things (==)

You were missing a few 'end's, make sure to indent correctly, it helps you see any missing ends

Here's your CYOS API file, reformatted, and with a few typos fixed:

-- Create Your Own OS by nomimg1
-- CYOS 1.0
local osEnv = ""
local osMOTD = "BLANK OS"

function osType(setType)
  if ostype == "cmd" then
	osEnv = ostype
  elseif ostype == "gui" then
	osEnv = ostype
  else
	print("Unknown OS Enviroment Type")
  end
end

function cmdStart()
  term.clear()
  term.setCursorPos(1,1)
end

function osStart()
  if ostype == "gui" then
	print("GUI Os's are coming soon.")
  elseif ostype == "cmd" then
	cmdStart()
  end
end

function osMotd(motd)
  osMOTD = motd
end