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

[question] function names

Started by tesla1889, 13 July 2012 - 07:53 AM
tesla1889 #1
Posted 13 July 2012 - 09:53 AM
i read through all of the source code provided for CraftOS, and basically every function was a script name, followed by a period, followed by the name of the command. i didn't see any point in the code where the OS prohibited that for future use, but whenever i include a period in a function name, i get an error about an unexpected symbol. how is the OS getting away with this and how can i get around this problem?

thanks
jay5476 #2
Posted 13 July 2012 - 10:24 AM
i dont really know what your talking about but i do know how to use functions if thats what you mean then try

function functionName()
code
code
code
end

then later on in code
if input == Something then
functionName()
-- then it will run the function
Blunty #3
Posted 13 July 2012 - 10:36 AM
i think tesla wants to progam his own api
tesla1889 #4
Posted 13 July 2012 - 11:40 AM
i know about function declaration and everything. i was just wondering why its not letting me use a period in the name
Zuriki #5
Posted 13 July 2012 - 02:36 PM
Because a period denotes a break between an "object" and it's function.

ie. rednet.receive()

rednet is the "object" and receive() is the function

If I were to definite a function with a period in the name, then it would break the lua syntax. If you want to have a set of functions grouped together look up writing your own API.
MysticT #6
Posted 13 July 2012 - 03:23 PM

string.gsub(abovePost, "object", "table")
:)/>/>
That's right, the functions are called like <apiName>.<functionName>(). You can either create an api (a file with some functions) and load it with os.loadAPI, or crate a table in your program and add the functions there, like:

local myFunctions = {}

function myFunctions.sayHello()
  print("Hello")
end

Basically, it's just indexing a table, so this lines:

myFunctions.sayHello()
myFunctions["sayHello"]()
do the same thing (get the value from the table with the key "sayHello" and call it).