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

[Lua] How do I make my own functions from outside a program?

Started by mrSLIMEguy, 01 October 2012 - 02:32 AM
mrSLIMEguy #1
Posted 01 October 2012 - 04:32 AM
Alright, I want to make my own function thing.
I know how to make a slime.print() etc, but I want it to be in a separate file.

So the function file is:


slime = {
print = function(text, mode)
	   w, h = term.getSize()
	   if mode = "m"
			term.setCursorPos(w - (math.floor(string.len(text)) \ 2), h)
			print (text)
	   elseif mode = "wm" then
			term.setCursorPos(w - (math.floor(string.len(text)) \ 2), h)
			write text
	   else
	   error("Slime Print Function Error: slime.print() Unexpected mode")
	   end
end
}
--[ This will let me do slime.print("hello", "m") to print "hello" in the
middle of the screen. slime.print(bye, "wm") will write "bye" in the middle of the screen. I may also add more things like slime.clear() wich also resets the cursor position.
--]

While my code file will have:


load(functionfile) -- < This is what I need
slime.print("Testing Testing 123!", "m")


Also if you can, please also look at my other VERY IMPORTANT post here <——-
Found out the answer! But pleeeeease look at my other post ——-/\
Edited on 02 October 2012 - 02:15 AM
Lyqyd #2
Posted 01 October 2012 - 04:33 AM
Any reason you couldn't use os.loadAPI()?
mrSLIMEguy #3
Posted 01 October 2012 - 05:18 AM
Any reason you couldn't use os.loadAPI()?

Umm, does that work with any file? Like one I make in a slimeos folder and I put os.loadAPI(slimeos/apis/slimedotfunctions)?
Also I didn't know about the os.loadAPI() either….
Mtdj2 #4
Posted 01 October 2012 - 09:01 AM
Well, the thing he suggests is like this:

File "slime":

print = function(text, mode)
		   w, h = term.getSize()
		   if mode = "m"
				    term.setCursorPos(w - (math.floor(string.len(text))  2), h)
				    print (text)
		   elseif mode = "wm" then
				    term.setCursorPos(w - (math.floor(string.len(text))  2), h)
				    write text
		   else
		   error("Slime Print Function Error: slime.print() Unexpected mode")
		   end
end

File <WhereYouNeedYourFuntion>

os.loadAPI("slime")
slime.print("Trololo","wm")
I hope I helped make you understand.
mrSLIMEguy #5
Posted 01 October 2012 - 11:54 AM
Well, the thing he suggests is like this:

File "slime":

print = function(text, mode)
		   w, h = term.getSize()
		   if mode = "m"
					term.setCursorPos(w - (math.floor(string.len(text))  2), h)
					print (text)
		   elseif mode = "wm" then
					term.setCursorPos(w - (math.floor(string.len(text))  2), h)
					write text
		   else
		   error("Slime Print Function Error: slime.print() Unexpected mode")
		   end
end

File <WhereYouNeedYourFuntion>

os.loadAPI("slime")
slime.print("Trololo","wm")
I hope I helped make you understand.
Thanks!


—TOPIC CLOSED—