Jump to content




run functions of other files

api utility

  • You cannot reply to this topic
11 replies to this topic

#1 theuhmu

  • Members
  • 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

#2 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 26 January 2013 - 08:48 AM

os.loadAPI("myApiFile")
myApiFile.functionName()


#3 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 26 January 2013 - 08:54 AM

View Postsjele, on 26 January 2013 - 08:48 AM, said:

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).

#4 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 26 January 2013 - 08:56 AM

Wooooop, woop. Will edit :)

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 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.

#6 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 26 January 2013 - 09:06 AM

View PostEric, on 26 January 2013 - 08:54 AM, said:

View Postsjele, on 26 January 2013 - 08:48 AM, said:

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).

View PostLyqyd, on 26 January 2013 - 08:57 AM, said:

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

#7 theuhmu

  • Members
  • 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

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 January 2013 - 11:06 AM

View PostremiX, on 26 January 2013 - 09:06 AM, said:

View PostEric, on 26 January 2013 - 08:54 AM, said:

View Postsjele, on 26 January 2013 - 08:48 AM, said:

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).

View PostLyqyd, on 26 January 2013 - 08:57 AM, said:

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".

#9 Eric

  • Members
  • 92 posts
  • LocationUK

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.

#10 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 26 January 2013 - 01:43 PM

View PostEric, on 26 January 2013 - 12:44 PM, said:

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()


#11 tesla1889

  • Members
  • 351 posts
  • LocationSt. 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

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 January 2013 - 02:03 PM

View PostEric, on 26 January 2013 - 12:44 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users