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

Function Calling

Started by Adjgamer, 17 December 2012 - 03:00 PM
Adjgamer #1
Posted 17 December 2012 - 04:00 PM
This question is not specifically about CC but it is about lua and I don't want to join another forum to ask this.
I wrote a function ( getName() ) for a little text RPG in one file, then I made a main file where i am gonna pull the game together.
I want to use the getName() function from another file but I don't know how… Do I just type import "getName()"; at the top before any other code?
Thanks -Adjgamer

PS: Why are some posts water buckets and some posts just buckets /w a diamond next to it?
etopsirhc #2
Posted 17 December 2012 - 04:06 PM
if you set it up as an api file you could just use os.loadAPI("file")
if you set up the other file as just a bunch of functions you could just call shell.run("file") , witch is kinda easier as you dont need to use the file.function() just function()
and it you really feel out there you could make an object oriented one where the file can also hold variables , lemme know if u want that , as its more involved than the others
Adjgamer #3
Posted 17 December 2012 - 04:14 PM
I am not going to be using CC at all for now I just want to have the code in clean sections but I want to write all my functions in one file and paste them together in a main file. I just need to be able to acess the functions from my functions file in my main file.
theoriginalbit #4
Posted 17 December 2012 - 04:21 PM
This question is not specifically about CC but it is about lua and I don't want to join another forum to ask this.
I wrote a function ( getName() ) for a little text RPG in one file, then I made a main file where i am gonna pull the game together.
I want to use the getName() function from another file but I don't know how… Do I just type import "getName()"; at the top before any other code?
Thanks -Adjgamer

PS: Why are some posts water buckets and some posts just buckets /w a diamond next to it?

as etopsirhc said you could load it as an API. however how to do that outside CC im not to sure. sorry :(/>

ok so the buckets:

Empty bucket — You have read the thread
Empty bucket with diamond — You have read the thread and made a reply in it
Water bucket — You have not read the thread
2 Water buckets — You have previously read this thread and now there are new replies to read
2 Water buckets with diamond — You have previously read and replied to this thread and now there are new replies to read
KaoS #5
Posted 17 December 2012 - 04:21 PM
ok, we first need to know which version of Lua you are running this from
Adjgamer #6
Posted 17 December 2012 - 04:28 PM
ok, we first need to know which version of Lua you are running this from

5.2.1 or 5.1.2, whatever the newest is.
KaoS #7
Posted 17 December 2012 - 04:34 PM
ok, the latest is ahead of us in CC so I am not 100% sure whether our advice will be accurate, I will do my best

I suggest that you try executing your other files as programs and make sure that all of their functions are global, you can then use their environments to store their functions. do you know how to do this?

regrettably I will not be able to post again soon as I will be sleeping now, it is 5:30 am and I am working later in the day. good luck
Adjgamer #8
Posted 17 December 2012 - 04:40 PM
Yeah I am a bit too noob to take that on but if you pm me after you get some sleep that would be appreciated :)/>
Orwell #9
Posted 17 December 2012 - 05:56 PM
This could be useful:

Modules: http://www.lua.org/manual/5.2/manual.html#6.3
Halekio #10
Posted 20 January 2013 - 05:03 AM
if you set it up as an api file you could just use os.loadAPI("file")
if you set up the other file as just a bunch of functions you could just call shell.run("file") , witch is kinda easier as you dont need to use the file.function() just function()
and it you really feel out there you could make an object oriented one where the file can also hold variables , lemme know if u want that , as its more involved than the others

When you say "set it up as an API", what does that mean exactly…. as opposed to "just a bunch of functions"

I'm totally new to both lua and CC, please bear with me.
I have a feeling that loading it as an API "should" be a better approach (tho i do not know why i have this feeling) but then…

I have a program setup as a class and to access it from another program i have to do something like (or at least thoses are the only 2 way I got it to work):

os.loadAPI("myClass")
local myInstance = myClass.MyTable:new()

OR

shell.run("myClass")
local myInstance = myTable:new()


From an easy of use perspective, I would prefer the latter ….
What are the differences, which one is better …..and are there other/better ways ?
ChunLing #11
Posted 20 January 2013 - 09:51 AM
Have you tried the "import("fileName.ext")" method? That's pretty standard for non-CC Lua, CC can't use it because of how the files have to be handled (in a sandboxed file system).