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

run functions of other files

Started by theuhmu, 26 January 2013 - 07:46 AM
theuhmu #1
Posted 26 January 2013 - 08:46 AM
is it possible to call functions in a program which are defined in another file(API)?

tried myself and checked the forums without success
please help
thanks in advance
sjele #2
Posted 26 January 2013 - 08:48 AM

os.loadAPI("myApiFile")
myApiFile.functionName()
Eric #3
Posted 26 January 2013 - 08:54 AM

path/to/my/file.functionName()

That's a syntax error, since that's equivalent to (path/to/my) / ( file.functionName() ), which is an expression, not a statement (in the same way that 1 + 1 is not a valid line of lua code).
sjele #4
Posted 26 January 2013 - 08:56 AM
Wooooop, woop. Will edit :)/>
Lyqyd #5
Posted 26 January 2013 - 08:57 AM
As well, the API is loaded into a table identified with the file name, not the full path to the file.
remiX #6
Posted 26 January 2013 - 09:06 AM

path/to/my/file.functionName()

That's a syntax error, since that's equivalent to (path/to/my) / ( file.functionName() ), which is an expression, not a statement (in the same way that 1 + 1 is not a valid line of lua code).
As well, the API is loaded into a table identified with the file name, not the full path to the file.

Eric, have you made sure that you're correct? Because you're not.

What Lyqyd said is true, like this will work:


os.loadAPI("myOS/apis/testAPI")
testAPI.function()

That function will run
theuhmu #7
Posted 26 January 2013 - 09:10 AM
superawesome !! thanks
since i collect my functions as an api in "rom/apis"
it works with just FILENAME.FUNCTION()
yay new possibilities for me
Lyqyd #8
Posted 26 January 2013 - 11:06 AM

path/to/my/file.functionName()

That's a syntax error, since that's equivalent to (path/to/my) / ( file.functionName() ), which is an expression, not a statement (in the same way that 1 + 1 is not a valid line of lua code).
As well, the API is loaded into a table identified with the file name, not the full path to the file.

Eric, have you made sure that you're correct? Because you're not.

What Lyqyd said is true, like this will work:


os.loadAPI("myOS/apis/testAPI")
testAPI.function()

That function will run

Eric is correct; the code he was talking about would be a syntax error. However, he was correcting code that was incorrect in another way, namely that you do not include the API path when calling functions from it. Including the path like that would probably cause something like "attempt to perform arithmetic __div on nil and nil".
Eric #9
Posted 26 January 2013 - 12:44 PM
No, you actually wouldnt even get "attempt to perform arithmetic __div on nil and nil". That code produces a syntax error, not a runtime error.

Put "3/4" in a file and run it (the CC Lua shell doesn't show this behavior) and you'll see what I mean.
tesla1889 #10
Posted 26 January 2013 - 01:43 PM
No, you actually wouldnt even get "attempt to perform arithmetic __div on nil and nil". That code produces a syntax error, not a runtime error.

Put "3/4" in a file and run it (the CC Lua shell doesn't show this behavior) and you'll see what I mean.

you can't run

(/dir/file).fn()

you have to do

os.loadAPI("/dir/file")
file.fn()
tesla1889 #11
Posted 26 January 2013 - 01:45 PM
try defining a function in /dir/file called fn
in another file put

os.loadAPI("/dir/file")
file.fn()
and see if you get an error
Lyqyd #12
Posted 26 January 2013 - 02:03 PM
No, you actually wouldnt even get "attempt to perform arithmetic __div on nil and nil". That code produces a syntax error, not a runtime error.

Put "3/4" in a file and run it (the CC Lua shell doesn't show this behavior) and you'll see what I mean.

Ah, that is true indeed! The CC Lua shell should produce the same behavior; `func = function() 3/4 end` should cause the syntax error.