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

Functions within Java?

Started by cant_delete_account, 06 May 2012 - 05:36 PM
cant_delete_account #1
Posted 06 May 2012 - 07:36 PM
Is there a way to call Java code when a certain function is called inside of a computer?
(I'm making a peripheral)
Cloudy #2
Posted 06 May 2012 - 07:59 PM
That is the entire point of peripherals and how they actually work.

You just insert the Java code you want in the peripheral callMethod function if it is the right command.
MysticT #3
Posted 06 May 2012 - 08:04 PM
You have implement that in the callMethod() function of your peripheral.

public Object[] callMethod( IComputerAccess computer, int method, Object[] arguments ) throws Exception;
It is called when someone uses peripheral.call() with your peripheral.
Xfel #4
Posted 06 May 2012 - 09:03 PM
I think he meant to create a lua function pointer and return it, which is not possible because of the strange peripheral api design.
Cloudy #5
Posted 06 May 2012 - 10:10 PM
I think he meant to create a lua function pointer and return it, which is not possible because of the strange peripheral api design.

You can create an API in Lua which calls your function. You can even mount the Lua API directly into the computers filesystem on attach - I do that on my speaker peripheral.

The peripheral API isn't strange when you think of it in terms of what it actually is in the context of the mod - an external device that you need to communicate with.
Xfel #6
Posted 07 May 2012 - 09:48 AM
Well I think it would not be totally unlogical if your peripheral methods would be able to process tables and functions.
Cloudy #7
Posted 07 May 2012 - 01:42 PM
When you connect a new, unrecognised device to your computer, does it "just work" or does it require drivers?

The Lua side is almost like providing a driver.
Xfel #8
Posted 07 May 2012 - 04:37 PM
That's true, but it's just unlogical and annoying that I can't return complex data structures, but I can return a string describing one, which is then decoded with textutils.deserialize. that is slow and not a good programming style, but functionally the same as if I would just return a table.
Cloudy #9
Posted 07 May 2012 - 05:49 PM
I agree that being able to return a Lua table - even if it just converts our returned object array to a table if asked to. I will ask dan about the possibility when I next see him online.
Xfel #10
Posted 07 May 2012 - 10:02 PM
I also see that passing functions would cause problems as you might call them from outside any computer thread. And that could also happen when accessing table fields via metatags. The array conversion might be the best solution. but there should also be a way to use named table fields - maybe wrap it in a java.util.Map or something like that.
cant_delete_account #11
Posted 17 May 2012 - 01:53 AM
I agree that being able to return a Lua table - even if it just converts our returned object array to a table if asked to. I will ask dan about the possibility when I next see him online.

I also see that passing functions would cause problems as you might call them from outside any computer thread. And that could also happen when accessing table fields via metatags. The array conversion might be the best solution. but there should also be a way to use named table fields - maybe wrap it in a java.util.Map or something like that.
You could create a thread about this instead of cluttering my thread with it.
This is about "Is there a way to call Java code when a certain function is called inside of a computer?", not returning Lua tables in Java.
Cloudy #12
Posted 17 May 2012 - 12:19 PM
I agree that being able to return a Lua table - even if it just converts our returned object array to a table if asked to. I will ask dan about the possibility when I next see him online.

I also see that passing functions would cause problems as you might call them from outside any computer thread. And that could also happen when accessing table fields via metatags. The array conversion might be the best solution. but there should also be a way to use named table fields - maybe wrap it in a java.util.Map or something like that.
You could create a thread about this instead of cluttering my thread with it.
This is about "Is there a way to call Java code when a certain function is called inside of a computer?", not returning Lua tables in Java.

And your thread was answered, if you looked. Have you ever heard of a discussion forum? It is where discussion happens. You don't just own a thread, just because you posted it. The discussion was VERY related to your question.