Page 1 of 1

Question about remote interfaces

Posted: Thu Jul 24, 2014 9:57 am
by Reygan
One question about remote interfaces. Why function should be created directly in remote.addinterface? When i just use function name as parameter it is not working. Also no errors appears too.

Re: Modding Info References

Posted: Thu Jul 24, 2014 10:11 am
by Dark
Reygan wrote:One question about remote interfaces. Why function should be created directly in remote.addinterface? When i just use function name as parameter it is not working. Also no errors appears too.
It's not required to define stuff inline for remote.addinterface, you just doing something wrong. For example calling said function instead of actually referencing it.
Also, wrong thread to post such questions.

Re: Modding Info References

Posted: Thu Jul 24, 2014 10:16 am
by Reygan
Dark wrote: It's not required to define stuff inline for remote.addinterface, you just doing something wrong. For example calling said function instead of actually referencing it.
Also, wrong thread to post such questions.
Amm, okay :)
Could you give me example?

Re: Modding Info References

Posted: Thu Jul 24, 2014 10:22 am
by Dark

Code: Select all

local function print_foo(str)
  game.player.print("Foo: "..str)
end

local methods = {
  print_me = print_foo,
}

remote.addinterface("FOO", methods)
--remote.call("FOO", "print_me", "BAR")

Re: Modding Info References

Posted: Thu Jul 24, 2014 11:16 am
by Reygan
Dark wrote:

Code: Select all

local function print_foo(str)
  game.player.print("Foo: "..str)
end

local methods = {
  print_me = print_foo,
}

remote.addinterface("FOO", methods)
--remote.call("FOO", "print_me", "BAR")
Oh, understood, parameter "methods" should be a table with list of assignments, not list of function identifiers.
Thank you.

Re: Question about remote interfaces

Posted: Sun Aug 10, 2014 6:45 am
by Reygan
Alright. Then, second quesion.
Why remote interfaces could not be accessed like this:

Code: Select all

-- check whether there is the "myinterface" interface and whether it contains the "mygetter" function
if remote.interfaces.myinterface and remote.interfaces.myinterface.mygetter then
-- the remote call for the function is safe to use
end
This is example from wiki (https://forums.factorio.com/wiki/inde ... interfaces)

Re: Question about remote interfaces

Posted: Sun Aug 10, 2014 1:32 pm
by FreeER
Reygan wrote:parameter "methods" should be a table with list of assignments, not list of function identifiers.
Hm, to me 'assignment' doesn't imply "only functions" which (I believe) script interfaces are limited to. What the methods really gets is an associative array, storing functions, indexed by strings (aka names, or identifiers, of those functions); which admittedly, isn't much different from a list in that it stores data (and functions are just callable data in Lua), but it is a bit in that it's easy to get the names of that data (and easy to get data from names).
Reygan wrote:Why remote interfaces could not be accessed like this:
The code given is just checking that the interface was previously defined (and the comment would imply use of remote.call("myinterface", "mygetter")), the reason that addinterface and call are used is because they do extra work to make sure that other mods (and their Lua states) actually know about added interfaces (and it's list of functions). At least, I've assumed so since the interfaces would require C++ to transfer the calls between Lua states; I've never attempted to use just remote.interfaces.x.y(z) (admittedly it'd probably work if called within the script that defined it, probably not for other scripts however).