65 posts
Posted 14 August 2014 - 08:22 PM
Let's saying I want to make an API which enhances movement functions and I want to overwrite the default forward() with my forward(). How would I do this? Does the program need to move itself somewhere? When someone uses go or forward in the shell outside of a program, is there a way I can overwrite those as well? What stores them and how can I save to it?
Edited on 14 August 2014 - 06:43 PM
3790 posts
Location
Lincoln, Nebraska
Posted 14 August 2014 - 08:48 PM
You can make a file with the same name as the ones you're replacing, and place them in the root directory. Since the CraftOS shell searches for absolute paths first, before aliases, you can "replace" the default programs by stepping in front of them in line.
36 posts
Location
USA, east coast
Posted 14 August 2014 - 08:48 PM
so the way it would work, from what your saying, would require you to do an os.loadApi function and then call the functions, like this
api_name.function(parameters)
--eg
signify.forward(3)
which would if it's set up would move the turtle forward 3 times
65 posts
Posted 14 August 2014 - 08:53 PM
You can make a file with the same name as the ones you're replacing, and place them in the root directory. Since the CraftOS shell searches for absolute paths first, before aliases, you can "replace" the default programs by stepping in front of them in line.
While that may work for executing programs from the shell, what about when programming? I want to overwrite turtle.forward(), since putting it in the root directory would not automatically load the API as turtle, how exactly would I do this? Could I replace go and refuel without spamming their root with a ton of replacements? Or would I have to have the api named turtle and somehow replace their startup script so it automatically loads 'turtle' overwriting the previous one? Since it is an API, I can't use shell.getRunningProgram() to get it's name, rename it to turtle only the fly, move it to the root directory, then load it.
And if I loaded turtle over the current one, does that mean I'd have to provide ALL of the turtle functions or would it just overwrite the ones I've got and when it couldn't find it from mine, it'd look elsewhere after that?
Edited on 14 August 2014 - 06:58 PM
3790 posts
Location
Lincoln, Nebraska
Posted 14 August 2014 - 08:57 PM
Well, when programming, you'd simply replace the function.
local oldForward = turtle.forward
function turtle.forward(num)
return oldForward(num)
end
Basically.
65 posts
Posted 14 August 2014 - 09:01 PM
Well, when programming, you'd simply replace the function.
local oldForward = turtle.forward
function turtle.forward(num)
return oldForward(num)
end
Basically.
So in my API, I could literally say:
turtle.forward = myForward?
Or would I have to:
holderFunction = turtle.forward
turtle.forward = myforward
function myforward()
holderFunction()
end
Edited on 14 August 2014 - 07:05 PM
656 posts
Posted 14 August 2014 - 09:06 PM
Well, when programming, you'd simply replace the function.
local oldForward = turtle.forward
function turtle.forward(num)
return oldForward(num)
end
Basically.
So in my API, I could literally say:
turtle.forward = myForward?
No. First, you have to make a local backup of the turtle function, because you need to use it in your code, and second when replacing functions don't use brackets.
You should make your function like Cranium said:
local oldForward = turtle.forward
turtle.forward = function()
-- logic, loops, bla bla bla
oldForward()
end
Edited on 14 August 2014 - 09:08 PM
65 posts
Posted 14 August 2014 - 09:09 PM
No. First, you have to make a local backup of the turtle function, because you need to use it in your code, and second when replacing functions don't use brackets.
You should make your function like Cranium said:
local oldForward = turtle.forward
turtle.forward = function()
-- logic, loops, bla bla bla
turtle.forward()
end
If you're making a local backup, why wouldn't it be:
local oldForward = turtle.forward
turtle.forward = function()
-- logic, loops, bla bla bla
oldForward()
end
But even if I overwrite those, since this is happening within an API, wouldn't the program still executing my api still have to reference them as api.turtle.function() ?
Edited on 14 August 2014 - 07:19 PM
108 posts
Posted 14 August 2014 - 09:45 PM
You could always do something similar to what's done in the actual turtle api, they override what they want(which is nothing in this case), and then copy all the original stuff to the result of getfenv. (
turtle API)
If you're making a local backup, why wouldn't it be:
local oldForward = turtle.forward
turtle.forward = function()
-- logic, loops, bla bla bla
oldForward()
end
But even if I overwrite those, since this is happening within an API, wouldn't the program still executing my api still have to reference them as api.turtle.function() ?
Yeah, that'd be the correct way to do it. And (I don't think) api.turtle.function() would need to be done, since you'll just be setting a value in the global table called turtle (well, unless your api is called turtle, in which case that global table is overridden when your api finishes loading)