[1.9.x] Remote Interface for other mods

Adds new train stops forming a highly configurable logistic network.

Moderator: Optera

Post Reply
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

[1.9.x] Remote Interface for other mods

Post by Optera »

I've added a remote interface to register callbacks to secondary mods remote interfaces into the development branch to version 1.9.0.
This allows other mods to grab LTN's data and display it in a GUI or create a combinator showing all items currently available in a network.

Currently supported are registration for stop status and the whole dispatcher data structure. I may break the dispatcher up into smaller chunks depending on feedback.

control.lua of a demo mod grabbing stop status and dispatcher and writing the data with serpent.block to logfile:

Code: Select all

-- LTN callback handers, triggered by LTN during runtime
remote.add_interface("ltn_interface_demo",	{
  -- log recieved stop data
  stop_updates = function(data)
    if data then
      log("Stop Data:"..serpent.block(data) )
    end
  end,

  -- log recieved dispatcher data
  dispatcher_updates = function(data)
    if data then
      log("Dispatcher Data:"..serpent.block(data) )
    end
  end
})

---- Initialisation  ----
do
script.on_init(function()
  -- register callbacks with LTN
  -- format remote.call("ltn_interface", "register_reciever_stop_status", <this-mod-interface-name>, <this-mod-interface-function>)
  remote.call("ltn_interface", "register_reciever_stop_status", "ltn_interface_demo", "stop_updates")
  remote.call("ltn_interface", "register_reciever_dispatcher_status", "ltn_interface_demo", "dispatcher_updates")
end)

script.on_configuration_changed(function(data)
  -- register callbacks with LTN
  -- format remote.call("ltn_interface", "register_reciever_stop_status", <this-mod-interface-name>, <this-mod-interface-function>)
  remote.call("ltn_interface", "register_reciever_stop_status", "ltn_interface_demo", "stop_updates")
  remote.call("ltn_interface", "register_reciever_dispatcher_status", "ltn_interface_demo", "dispatcher_updates")
end)
end
I'm open for feedback on this feature.

melancoleeca
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Sep 28, 2018 8:57 am
Contact:

Re: Remote Interface for other mods

Post by melancoleeca »

This looks very helpfull.
I hopefully will try it on the weekend.

Thank you :)

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

[1.9.2] Remote Interface for other mods

Post by Optera »

I reworked the interface from double remote calls to a single remote call to register an event. Huge thanks to Bilka for telling me this was possible.

Here's control .lua for the demo doing the same log serpent dump as before with the event system.

Code: Select all

-- LTN interface event functions
function OnStopsUpdated(event)
  if event.data then
    log("Stop Data:"..serpent.block(event.data) )
  end
end

function OnDispatcherUpdated(event)
  if event.data then
    log("Dispatcher Data:"..serpent.block(event.data) )
  end
end

---- Initialisation  ----
do
script.on_init(function()
  -- register events from LTN
  if remote.interfaces["ltn_interface"] then
    script.on_event(remote.call("ltn_interface", "get_on_stops_updated_event"), OnStopsUpdated)
    script.on_event(remote.call("ltn_interface", "get_on_dispatcher_updated_event"), OnDispatcherUpdated)
  end
end)

script.on_load(function(data)
  -- register events from LTN
  if remote.interfaces["ltn_interface"] then
    script.on_event(remote.call("ltn_interface", "get_on_stops_updated_event"), OnStopsUpdated)
    script.on_event(remote.call("ltn_interface", "get_on_dispatcher_updated_event"), OnDispatcherUpdated)
    --[[ alternative anonymous function: 
         has to be copied between on_init and on_load
         script.on_event(remote.call("ltn_interface", "get_on_stops_updated_event"), function(event)
           do something with event.data
         end)
     ]]
  end
end)
end
The demo is also included under Interface_Demo

Post Reply

Return to “Logistic Train Network”