Page 1 of 1

Allow mods to register "slash" commands or global functions

Posted: Mon Mar 14, 2016 2:29 pm
by jheiv
Currently, it doesn't seem there is anyway to either:
a) Allow mods to register "slash" commands, e.g. /mymod dosomething 123
b) Allow mods to define global lua functions, so that something like /c myfunction("abc", 123) would work.

Of course, there are ugly workarounds, by requiring the user to copy/paste commands first, that would allow, to some extent, (b), but (a) is really my suggestion.

Note that using interfaces, we can *almost* get to (b), even if it's not ideal. But I say almost because remote.call currently restricts the return value to strings.

Consider the interface defined as:

Code: Select all

local function do_something_1()
  game.player.print("Did something!")
end

local function do_something_2()
  game.player.print("Did something else!")
end

remote.add_interface("mymod",{
  export = function()
    return {
      ["something1"] = do_something_1, 
      ["something2"] = do_something_2
    }
  end
})
One might think we could access those functions with:

Code: Select all

/c mymod = remote.call("mymod", "export")
-- then --
/c mymod.something1()
But we cannot. Because the return value from the function called by remote.call is forced to be a string. If it is a string, the return value is assigned to mymod. If it is not a string, mymod is instead assigned the name of the interface function. So after the first console command above, mymod would be the string "export", not a table of functions like you might expect.

For clarity, if the interface was defined as:

Code: Select all

remote.add_interface("mymod",{
  export = function()
    return "This is my mod"
  end
})
... then after the first console command above, mymod would be the string "This is my mod".

So returning strings from interface functions works, but returning tables does not.

Lifting the strings-only restriction would allow mod authors to expose any number of "public" lua functions with only a small amount of code needed to be entered by the user (the "/c mymod = remote.call(...)" line).

Coincidentally, this approach also gives the mod's functions their own unique namespace (in the above example, mymod). So even if two mods defined a function with the same name (e.g. named "reset"), they wouldn't interfere, as each would have to be called from the function table returned by remote.call (e.g. mymod.reset() vs. hismod.reset()).

Lastly, on the topic of moving closer towards (a), allowing mods to register slash commands, I'll just point out that World of Warcraft has a relatively straightforward way of allowing mods (which are also written in Lua) to register slash commands, which may be some inspiration of how the issue might be approached. (Link to example of registering slash commands removed, as it's my first post).

For those interested, below is one other attempt at exposing mod-defined functions globally. Although this approach should work in theory, it crashes Factorio.
Additional attempt

Re: Allow mods to register "slash" commands or global functions

Posted: Mon Mar 14, 2016 3:28 pm
by Adil
Agree, console interface could use some unclogging.

Re: Allow mods to register "slash" commands or global functions

Posted: Tue Mar 15, 2016 4:33 am
by kbj0072
Agree too, since factorio does not support korean keyboard (as far as I know)

I wrote a Lua function that converts English into Korean.
jheiv wrote: Of course, there are ugly workarounds, by requiring the user to copy/paste commands.
Like the author, I have been using 'ugly workarounds, by requiring the user to copy/paste commands' for chat.

ex. 1. Paste function for translation, 2. /c k("rkskekfk gksrmf xptmxm")

I wanted to make this function as a mod. But, i could not find a way eventually.

If developers allow mods to register "slash" commands or global functions, this problem will be solved.

Re: Allow mods to register "slash" commands or global functions

Posted: Wed Mar 16, 2016 11:49 am
by Ext3h
Before taking a quick shot here on baking an API, there would be other questions to be answered first, partially related to multiplayer.

Open points would then be:
  • Permissions (since mods themselves are not MP-aware at all)
  • Related to permissions, command visibility
  • Name collisions (allowing mods to inject into arbitrary scope isn't such a great idea at all)
  • Embedded help for custom commands and functions
There's probably more than that.

Re: Allow mods to register "slash" commands or global functions

Posted: Sat Mar 19, 2016 1:20 am
by Adil
Well, current console command system wasn't hindered by all those.
And even then mods in multiplayer are completely transparent and easily changed, and if some mod introduces interfaces of questionable functionality, those are agreed upon by players.
The op by the way suggested only simpler usage of such functions with a couple of symbols instead of the remote.call(...,...)... acrobacy. The remote.call itself faces all the same questions you've raised.
(can remote be used in multiplayer, though? I haven't much experience on that.)

Re: Allow mods to register "slash" commands or global functions

Posted: Sun Mar 20, 2016 11:09 am
by RepairMan
I definitly like the suggestion,
in my opinion there should be multiple additions to the chat system in general.
That might involve adding a chat events, a command event and a way to register commands

Re: Allow mods to register "slash" commands or global functions

Posted: Sun Dec 29, 2019 6:26 pm
by kizrak
In case future me finds this post again: LuaCommandProcessor