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

function X

Started by RadimBur, 01 June 2015 - 08:42 PM
RadimBur #1
Posted 01 June 2015 - 10:42 PM
Hi, help me please.

How do ?


local x = test

if x == nil then
os.reboot ()
else
x () --- as test () -- how do ?
end

function test ()
--code--
end
Creator #2
Posted 01 June 2015 - 10:59 PM
Declare test before the other code.
RadimBur #3
Posted 01 June 2015 - 11:07 PM
??

There is a easy program with problem.

13th is a error


local function prc(text, y)
  local w = term.getSize()
  local _, cy = term.getCursorPos()
  term.setCursorPos(math.ceil((w-#text)/2), y or cy)
  write(text)
end
function start ()
if fs.exists ("/system/RAM/test") then
  local data = fs.open ("/system/RAM/test", "r")
   funkce = data.readLine ()
   data.close ()
   (funkce.." ()")
   else
	 menuc ()
   end
end
function minimalizace ()
if fs.exists ("/system/RAM/test") then
fs.delete ("/system/RAM/test")
end

local data = fs.open ("/system/RAM/test", "w")
data.writeLine(funkce)
data.close ()
shell.run ("/system/desktop")
end

function ukonceni ()
if fs.exists ("/system/RAM/test") then
   fs.delete ("/system/RAM/test")
end
shell.run ("/system/desktop")
end
function menuc ()
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.black)
    term.clear ()
prc ("Choose a color", 2)
term.setCursorPos(5,5)
print "- Blue"
term.setCursorPos(5,7)
print "- Red"
term.setCursorPos(5,9)
print "- Orange"
term.setCursorPos(5,11)
print "- Magenta"
term.setCursorPos(5,13)
print "- Green"
  while true do
	   
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
	    if x >= 5 and x <= 10 and y == 5 and button == 1 then
  blue ()
	    elseif x >= 5 and x <= 10 and y == 7 and button == 1 then
  red ()
	    elseif x >= 5 and x <= 10 and y == 9 and button == 1 then
  orange ()
  elseif x >= 5 and x <= 10 and y == 11 and button == 1 then
  magenta ()
  elseif x >= 5 and x <= 10 and y == 13 and button == 1 then
  green ()
  else
  paintutils.drawLine(1, 1, 51, 1, colors.black)
  paintutils.drawPixel (51,1, colors.red)
  term.setTextColor(colors.white)
  term.setCursorPos (51,1)
  print "*"
    while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
 
  if x == 51 and y == 1 then
   shell.run ("/system/desktop")
	    else
   menuc()
   end
   end
    end
end
end
function blue ()
funkce = "blue"
term.setBackgroundColor(colors.blue)
    term.setTextColor(colors.white)
    term.clear ()
term.setCursorPos(5,5)
print "- Blue"
term.setCursorPos(5,7)
print "- Red"
term.setCursorPos(5,9)
print "- Orange"
term.setCursorPos(5,11)
print "- Magenta"
term.setCursorPos(5,13)
print "- Green"

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
	    if x >= 5 and x <= 10 and y == 5 and button == 1 then
  blue ()
	    elseif x >= 5 and x <= 10 and y == 7 and button == 1 then
  red ()
	    elseif x >= 5 and x <= 10 and y == 9 and button == 1 then
  orange ()
  elseif x >= 5 and x <= 10 and y == 11 and button == 1 then
  magenta ()
  elseif x >= 5 and x <= 10 and y == 13 and button == 1 then
  green ()
  else
  paintutils.drawLine(1, 1, 51, 1, colors.black)
  paintutils.drawPixel (51,1, colors.red)
  term.setTextColor(colors.white)
  term.setCursorPos (51,1)
  print "*"
  paintutils.drawPixel (50,1, colors.lightGray)
  term.setCursorPos (50,1)
  print "<"
  paintutils.drawPixel (49,1, colors.lightGray)
  term.setCursorPos (49,1)
  print "_"
 
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
  if x == 51 and y == 1 then
   shell.run ("/system/desktop")
  elseif x == 50 and y == 1 then
   menuc ()
  elseif x == 49 and y == 1 then
   minimalizace ()
	    else
   blue ()
    end
end
end
end
function red ()
funkce = "red"
term.setBackgroundColor(colors.red)
    term.setTextColor(colors.white)
    term.clear ()
term.setCursorPos(5,5)
print "- Blue"
term.setCursorPos(5,7)
print "- Red"
term.setCursorPos(5,9)
print "- Orange"
term.setCursorPos(5,11)
print "- Magenta"
term.setCursorPos(5,13)
print "- Green"

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
	    if x >= 5 and x <= 10 and y == 5 and button == 1 then
  blue ()
	    elseif x >= 5 and x <= 10 and y == 7 and button == 1 then
  red ()
	    elseif x >= 5 and x <= 10 and y == 9 and button == 1 then
  orange ()
  elseif x >= 5 and x <= 10 and y == 11 and button == 1 then
  magenta ()
  elseif x >= 5 and x <= 10 and y == 13 and button == 1 then
  green ()
  else
  paintutils.drawLine(1, 1, 51, 1, colors.black)
  paintutils.drawPixel (51,1, colors.red)
  term.setTextColor(colors.white)
  term.setCursorPos (51,1)
  print "*"
  paintutils.drawPixel (50,1, colors.lightGray)
  term.setCursorPos (50,1)
  print "<"
  paintutils.drawPixel (49,1, colors.lightGray)
  term.setCursorPos (49,1)
  print "_"
 
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
  if x == 51 and y == 1 then
   shell.run ("/system/desktop")
  elseif x == 50 and y == 1 then
   menuc ()
  elseif x == 49 and y == 1 then
   minimalizace ()
	    else
   red ()
    end
end
end
end
function orange ()
funkce = "orange"
term.setBackgroundColor(colors.orange)
    term.setTextColor(colors.white)
    term.clear ()
term.setCursorPos(5,5)
print "- Blue"
term.setCursorPos(5,7)
print "- Red"
term.setCursorPos(5,9)
print "- Orange"
term.setCursorPos(5,11)
print "- Magenta"
term.setCursorPos(5,13)
print "- Green"

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
	    if x >= 5 and x <= 10 and y == 5 and button == 1 then
  blue ()
	    elseif x >= 5 and x <= 10 and y == 7 and button == 1 then
  red ()
	    elseif x >= 5 and x <= 10 and y == 9 and button == 1 then
  orange ()
  elseif x >= 5 and x <= 10 and y == 11 and button == 1 then
  magenta ()
  elseif x >= 5 and x <= 10 and y == 13 and button == 1 then
  green ()
  else
  paintutils.drawLine(1, 1, 51, 1, colors.black)
  paintutils.drawPixel (51,1, colors.red)
  term.setTextColor(colors.white)
  term.setCursorPos (51,1)
  print "*"
  paintutils.drawPixel (50,1, colors.lightGray)
  term.setCursorPos (50,1)
  print "<"
  paintutils.drawPixel (49,1, colors.lightGray)
  term.setCursorPos (49,1)
  print "_"
 
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
  if x == 51 and y == 1 then
   shell.run ("/system/desktop")
  elseif x == 50 and y == 1 then
   menuc ()
  elseif x == 49 and y == 1 then
   minimalizace ()
	    else
   orange ()
    end
end
end
end
function magenta ()
funkce = "magenta"
term.setBackgroundColor(colors.magenta)
    term.setTextColor(colors.white)
    term.clear ()
term.setCursorPos(5,5)
print "- Blue"
term.setCursorPos(5,7)
print "- Red"
term.setCursorPos(5,9)
print "- Orange"
term.setCursorPos(5,11)
print "- Magenta"
term.setCursorPos(5,13)
print "- Green"

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
	    if x >= 5 and x <= 10 and y == 5 and button == 1 then
  blue ()
	    elseif x >= 5 and x <= 10 and y == 7 and button == 1 then
  red ()
	    elseif x >= 5 and x <= 10 and y == 9 and button == 1 then
  orange ()
  elseif x >= 5 and x <= 10 and y == 11 and button == 1 then
  magenta ()
  elseif x >= 5 and x <= 10 and y == 13 and button == 1 then
  green ()
  else
  paintutils.drawLine(1, 1, 51, 1, colors.black)
  paintutils.drawPixel (51,1, colors.red)
  term.setTextColor(colors.white)
  term.setCursorPos (51,1)
  print "*"
  paintutils.drawPixel (50,1, colors.lightGray)
  term.setCursorPos (50,1)
  print "<"
  paintutils.drawPixel (49,1, colors.lightGray)
  term.setCursorPos (49,1)
  print "_"
 
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
  if x == 51 and y == 1 then
   shell.run ("/system/desktop")
  elseif x == 50 and y == 1 then
   menuc ()
  elseif x == 49 and y == 1 then
   minimalizace ()
	    else
   magenta ()
    end
end
end
end
function green ()
funkce = "green"
term.setBackgroundColor(colors.green)
    term.setTextColor(colors.white)
    term.clear ()
term.setCursorPos(5,5)
print "- Blue"
term.setCursorPos(5,7)
print "- Red"
term.setCursorPos(5,9)
print "- Orange"
term.setCursorPos(5,11)
print "- Magenta"
term.setCursorPos(5,13)
print "- Green"

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
	    if x >= 5 and x <= 10 and y == 5 and button == 1 then
  blue ()
	    elseif x >= 5 and x <= 10 and y == 7 and button == 1 then
  red ()
	    elseif x >= 5 and x <= 10 and y == 9 and button == 1 then
  orange ()
  elseif x >= 5 and x <= 10 and y == 11 and button == 1 then
  magenta ()
  elseif x >= 5 and x <= 10 and y == 13 and button == 1 then
  green ()
  else
  paintutils.drawLine(1, 1, 51, 1, colors.black)
  paintutils.drawPixel (51,1, colors.red)
  term.setTextColor(colors.white)
  term.setCursorPos (51,1)
  print "*"
  paintutils.drawPixel (50,1, colors.lightGray)
  term.setCursorPos (50,1)
  print "<"
  paintutils.drawPixel (49,1, colors.lightGray)
  term.setCursorPos (49,1)
  print "_"
 
  local event, button, x, y = os.pullEvent("mouse_click")
  xy = x..","..y
  if x == 51 and y == 1 then
   shell.run ("/system/desktop")
  elseif x == 50 and y == 1 then
   menuc ()
  elseif x == 49 and y == 1 then
   minimalizace ()
	    else
   green ()
    end
end
end
end

start ()
mrpoopy345 #4
Posted 01 June 2015 - 11:10 PM
Lua will not take into account functions that you have not declared yet in your code.
You must tell the code what test() is before you use it.
By the way, are you trying to make x a function or what test returns? If you are trying to make x what test returns than use this code:

function test()
--Code here
end
local x = test()
if x == nil then
os.reboot()
else
test()
end
Edited on 01 June 2015 - 09:10 PM
RadimBur #5
Posted 01 June 2015 - 11:23 PM
I have got many function. Program will read a file in folder RAM and after run some function.

in RAM is file:

blue

convertor


--must read "blue" and run function blue ()

function blue ()
--code--
end
function red ()
--code--
end
function green ()
--code--
end
function magenta ()
--code--
end
Bomb Bloke #6
Posted 02 June 2015 - 01:06 AM
Place the functions in a table, and use the filename to index into that.

Eg:

local myFuncs = {
	["blue"] = function()
		--code--
	end,
	
	["red"] = function()
		--code--
	end,
	
	["green"] = function()
		--code--
	end,
	
	["magenta"] = function()
		--code--
	end
}

local x = "green"

myFuncs[x]()  -- Runs the "green" function in the "myFuncs" table