4 posts
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
318 posts
Location
Somewhere on the planet called earth
Posted 26 January 2013 - 08:48 AM
os.loadAPI("myApiFile")
myApiFile.functionName()
88 posts
Location
UK
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).
318 posts
Location
Somewhere on the planet called earth
Posted 26 January 2013 - 08:56 AM
Wooooop, woop. Will edit :)/>
8543 posts
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.
2088 posts
Location
South Africa
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
4 posts
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
8543 posts
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".
88 posts
Location
UK
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.
404 posts
Location
St. Petersburg
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()
404 posts
Location
St. Petersburg
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
8543 posts
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.