Jump to content




How to overwrite default programs/commands


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

#1 Noiro

  • Members
  • 65 posts

Posted 14 August 2014 - 07: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 by Signify, 14 August 2014 - 07:43 PM.


#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 14 August 2014 - 07: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.

#3 Neywiny

  • Members
  • 36 posts
  • LocationUSA, east coast

Posted 14 August 2014 - 07: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

#4 Noiro

  • Members
  • 65 posts

Posted 14 August 2014 - 07:53 PM

View PostCranium, on 14 August 2014 - 07:48 PM, said:

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 by Signify, 14 August 2014 - 07:58 PM.


#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 14 August 2014 - 07:57 PM

Well, when programming, you'd simply replace the function.
local oldForward = turtle.forward
function turtle.forward(num)
  return oldForward(num)
end
Basically.

#6 Noiro

  • Members
  • 65 posts

Posted 14 August 2014 - 08:01 PM

View PostCranium, on 14 August 2014 - 07:57 PM, said:

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 by Signify, 14 August 2014 - 08:05 PM.


#7 flaghacker

  • Members
  • 655 posts

Posted 14 August 2014 - 08:06 PM

View PostSignify, on 14 August 2014 - 08:01 PM, said:

View PostCranium, on 14 August 2014 - 07:57 PM, said:

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 by flaghacker, 14 August 2014 - 10:08 PM.


#8 Noiro

  • Members
  • 65 posts

Posted 14 August 2014 - 08:09 PM

View Postflaghacker, on 14 August 2014 - 08:06 PM, said:

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 by Signify, 14 August 2014 - 08:19 PM.


#9 natedogith1

  • Members
  • 110 posts

Posted 14 August 2014 - 08: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)

View PostSignify, on 14 August 2014 - 08:09 PM, said:

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)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users