Jump to content




Functions within Java?


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

#1 cant_delete_account

  • Members
  • 484 posts

Posted 06 May 2012 - 06: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)

#2 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 06 May 2012 - 06: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.

#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 May 2012 - 07: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.

#4 Xfel

    Peripheral Designer

  • Members
  • 515 posts

Posted 06 May 2012 - 08: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.

#5 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 06 May 2012 - 09:10 PM

View PostXfel, on 06 May 2012 - 08:03 PM, said:

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.

#6 Xfel

    Peripheral Designer

  • Members
  • 515 posts

Posted 07 May 2012 - 08:48 AM

Well I think it would not be totally unlogical if your peripheral methods would be able to process tables and functions.

#7 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 07 May 2012 - 12: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.

#8 Xfel

    Peripheral Designer

  • Members
  • 515 posts

Posted 07 May 2012 - 03: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.

#9 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 07 May 2012 - 04: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.

#10 Xfel

    Peripheral Designer

  • Members
  • 515 posts

Posted 07 May 2012 - 09: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.

#11 cant_delete_account

  • Members
  • 484 posts

Posted 17 May 2012 - 12:53 AM

View PostCloudy, on 07 May 2012 - 04:49 PM, said:

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.

View PostXfel, on 07 May 2012 - 09:02 PM, said:

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.

#12 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 17 May 2012 - 11:19 AM

View Postthesbros, on 17 May 2012 - 12:53 AM, said:

View PostCloudy, on 07 May 2012 - 04:49 PM, said:

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.

View PostXfel, on 07 May 2012 - 09:02 PM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users