first time making a function
#1
Posted 16 June 2012 - 09:36 PM
sm.clearScreen = function( par1, par2 )
term.clear( 1 )
if par1 | par2 == nil then
term.setCursorPos( 1, 1 )
else
term.setCursorPos( par1, par2 )
end
end
and here is the code I'm using it in---
test = "if you can read this message, the test has worked"
shell.run( "smAPI" )
print( "testing" )
sleep(2)
sm.clearScreen()
print( test )
---------------------------------------------------------
then the program just returns
bios:206: [string "smAPI"]:4: then expected
testing
test:6: attempt to index ? (a nil value)
I dont know what went wrong, the api does have a then on line 4, also I may have done something wrong with using == nil
help is appreciated
#2
Posted 16 June 2012 - 11:42 PM
sm.clearScreen = function( par1, par2 ) -- sm is not defined term.clear( 1 ) -- term.clear has no parameters if par1 | par2 == nil then -- read below term.setCursorPos( 1, 1 ) else term.setCursorPos( par1, par2 ) end end
test = "if you can read this message, the test has worked" shell.run( "smAPI" ) -- you should use os.loadAPI print( "testing" ) sleep(2) sm.clearScreen() print( test )I'm not sure of what you tried to do here:
if par1 | par2 == nil thenI guess you want to check if both are nil, it should be:
if par1 == nil or par2 == nil then
To make an api, you need to create the api file with all the functions you want it to have, something like:
function a()
print("This is an api function")
end
function f(n)
rs.setOutput("left", false)
sleep(n)
rs.setOutput("left", true)
end
Everything you want to be in the api should be global, so don't use local on them (you can, and should, use it inside the functions and for things you don't want to be visible in the api).Then, the program loads it with os.loadAPI, passing the path to the file as the argument:
os.loadAPI("testAPI")
Now the api should be loaded, and you can access it's functions and variables using it's name (the filename, without the full path):testAPI.a()
#3
Posted 17 June 2012 - 09:46 AM
function clearScreen(par1, par2)
term.clearScreen()
if par1==nil or par2==nil then
term.setCursorPos(1, 1)
else
term.setCursorPos(par1, par2)
end
end
The sm. is not needed in the Name of the functions, its automatically added by the os.loadAPI(api).here is the code to use:
os.loadAPI("FileNameOfTheApi")
FileNameOfTheApi.clearScreen()
#5
Posted 17 June 2012 - 08:35 PM
MysticT, on 16 June 2012 - 11:42 PM, said:
sm.clearScreen = function( par1, par2 ) -- sm is not defined term.clear( 1 ) -- term.clear has no parameters if par1 | par2 == nil then -- read below term.setCursorPos( 1, 1 ) else term.setCursorPos( par1, par2 ) end end
test = "if you can read this message, the test has worked" shell.run( "smAPI" ) -- you should use os.loadAPI print( "testing" ) sleep(2) sm.clearScreen() print( test )I'm not sure of what you tried to do here:
if par1 | par2 == nil thenI guess you want to check if both are nil, it should be:
if par1 == nil or par2 == nil then
To make an api, you need to create the api file with all the functions you want it to have, something like:
function a()
print("This is an api function")
end
function f(n)
rs.setOutput("left", false)
sleep(n)
rs.setOutput("left", true)
end
Everything you want to be in the api should be global, so don't use local on them (you can, and should, use it inside the functions and for things you don't want to be visible in the api).Then, the program loads it with os.loadAPI, passing the path to the file as the argument:
os.loadAPI("testAPI")
Now the api should be loaded, and you can access it's functions and variables using it's name (the filename, without the full path):testAPI.a()
hmm still says test:6: attempt to call nil there must be something wrong with the way I compare a parameter to nil in the api, I did use the way u showed me, ( if par1 == nil or par2 == nil ).
#6
Posted 17 June 2012 - 08:44 PM
#7
Posted 20 June 2012 - 04:02 PM
MysticT, on 17 June 2012 - 08:44 PM, said:
#8
Posted 20 June 2012 - 04:11 PM
samdeman22, on 20 June 2012 - 04:02 PM, said:
#9
Posted 20 June 2012 - 08:22 PM
BigSHinyToys, on 20 June 2012 - 04:11 PM, said:
#10
Posted 28 June 2012 - 08:25 PM
anyway its been far more than a min, and here is the part of the api where I compare to nil (the problem line)
if par1 == nil or par2 == nil then
so whats wrong with it?
#11
Posted 28 June 2012 - 08:27 PM
kazagistar, on 20 June 2012 - 08:22 PM, said:
BigSHinyToys, on 20 June 2012 - 04:11 PM, said:
#12
Posted 28 June 2012 - 08:42 PM
#13
Posted 28 June 2012 - 08:47 PM
samdeman22, on 28 June 2012 - 08:25 PM, said:
anyway its been far more than a min, and here is the part of the api where I compare to nil (the problem line)
if par1 == nil or par2 == nil then
so whats wrong with it?
Please post the entire changed code and the exact error you're receiving.
#14
Posted 02 August 2012 - 01:11 PM
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











