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

first time making a function

Started by samdeman22, 16 June 2012 - 08:36 PM
samdeman22 #1
Posted 16 June 2012 - 10:36 PM
ok so here is my function ( which is stored in smAPI ) I wanted to start with something simple—

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
MysticT #2
Posted 17 June 2012 - 12:42 AM
Ok, first some comments on the code:

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 then
I 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()
ardera #3
Posted 17 June 2012 - 10:46 AM
I think its easier to use:

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()
ardera #4
Posted 17 June 2012 - 10:48 AM
  term.clearScreen()

Sorry, its term.clear() not term.clearScreen()
samdeman22 #5
Posted 17 June 2012 - 09:35 PM
Ok, first some comments on the code:

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 then
I 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 ).
MysticT #6
Posted 17 June 2012 - 09:44 PM
Post your new code, so we can see the error.
samdeman22 #7
Posted 20 June 2012 - 05:02 PM
Post your new code, so we can see the error.
I'll get the code to you in a min, and this is a little off-topic, am I allowed to post in the ask a pro section to get help with forth (red power 2s computers ) ? It would be great if I could get help with redpower computers because it is so confusing, but they have few pros and cons compared to computer craft.
BigSHinyToys #8
Posted 20 June 2012 - 05:11 PM
I'll get the code to you in a min, and this is a little off-topic, am I allowed to post in the ask a pro section to get help with forth (red power 2s computers ) ? It would be great if I could get help with redpower computers because it is so confusing, but they have few pros and cons compared to computer craft.
while I can not speak for the entire user base of Computer Craft My personal preference would be that Red Power problems be solved on a Red Power forums and the the computer craft forums deal with only Computer Craft issues.
kazagistar #9
Posted 20 June 2012 - 09:22 PM
while I can not speak for the entire user base of Computer Craft My personal preference would be that Red Power problems be solved on a Red Power forums and the the computer craft forums deal with only Computer Craft issues.
I agree 100%. Not only is there no overlap between the programming languages, there is also very little overlap in terms of target audience. Computer craft uses a nice, high level scripting language to solve problem involving networking and turtles and stuff. Redpower computers are… for people who like low level optimization and reading opcode tables and stuff.
samdeman22 #10
Posted 28 June 2012 - 09:25 PM
ok, well I guess that failed.
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?
samdeman22 #11
Posted 28 June 2012 - 09:27 PM
while I can not speak for the entire user base of Computer Craft My personal preference would be that Red Power problems be solved on a Red Power forums and the the computer craft forums deal with only Computer Craft issues.
I agree 100%. Not only is there no overlap between the programming languages, there is also very little overlap in terms of target audience. Computer craft uses a nice, high level scripting language to solve problem involving networking and turtles and stuff. Redpower computers are… for people who like low level optimization and reading opcode tables and stuff.
you see the thing is, there IS no place to get help with redpower computers, I just thought this place'd be perfect, oh well.
samdeman22 #12
Posted 28 June 2012 - 09:42 PM
so yeah, my code is the same as before, but with that if statement changed to what I showed you.
Lyqyd #13
Posted 28 June 2012 - 09:47 PM
ok, well I guess that failed.
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.
samdeman22 #14
Posted 02 August 2012 - 02:11 PM
right, my code never works in making functions that take your input, I would like it if a pro could give me a tutorial of actually making an api. ( where to store it, how to load it into a program, how to make it and how to use it. ) i would have followed the other api posts around the forum, but they made no sense to me at all.