Allow mods to register "slash" commands or global functions

Suggestions that have been added to the game.

Moderator: ickputzdirwech

Post Reply
jheiv
Manual Inserter
Manual Inserter
Posts: 3
Joined: Mon Mar 14, 2016 1:49 pm
Contact:

Allow mods to register "slash" commands or global functions

Post 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

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

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

Post by Adil »

Agree, console interface could use some unclogging.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

kbj0072
Manual Inserter
Manual Inserter
Posts: 1
Joined: Tue Mar 15, 2016 4:21 am
Contact:

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

Post 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.

Ext3h
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Mon May 19, 2014 12:40 pm
Contact:

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

Post 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.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

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

Post 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.)
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

RepairMan
Burner Inserter
Burner Inserter
Posts: 19
Joined: Sat Oct 03, 2015 12:00 pm
Contact:

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

Post 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
TODO List:
1. Build a von Neumann Probe Factory
2. Build a AI wich controls the factory
3. Send it all into space and watch as planets get eaten by it
4. PROFIT

User avatar
kizrak
Long Handed Inserter
Long Handed Inserter
Posts: 75
Joined: Thu Jul 19, 2018 1:27 am
Contact:

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

Post by kizrak »

In case future me finds this post again: LuaCommandProcessor

Post Reply

Return to “Implemented Suggestions”