How to use my function in the game console?

Place to get help with not working mods / modding interface.
slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

Re: How to use my function in the game console?

Post by slpwnd »

The main reason why we have separated the control.lua into the mods is that the mods could cooperate and stack on each other. It places certain restrictions on the mods, however it also gives guarantees that the scenario files and other mod files will behave the same.

When you want to propagate something from the console into the Lua using the interfaces is indeed the way to go. I have just played with the following and works fine for me:

control.lua in foo mod:

Code: Select all

remote.addinterface("foo",
{
  testprint = function(msg)
    game.player.print(tostring(msg))
    return glob.foo
  end,

  gimme = function(name, count)
    game.player.character.insert{name=name, count=count}
  end,

  gimmecar = function()
    game.player.character.insert{name="car", count=1}
  end
})

game.onload(function()
  glob.foo = "foo"
end)
Naturally, the foo mod has to be enabled. Now you can play around in the console:

To get a car:

Code: Select all

remote.call("foo", "gimmecar")
To get some turrets:

Code: Select all

remote.call("foo", "gimme", "gun-turret", 10)
Testprint into the console.

Code: Select all

remote.call("foo", "testprint", "this is a test message")
On top of this the previous command returns a value of glob variable foo (which is "foo"). We can use the return value and reprint it on the screen:

Code: Select all

game.player.print(remote.call("foo", "testprint", "this is a test message"))
Now the BAD parts:

1) Stay away from using the remote.interfaces for discovering the interfaces. If it is used in the mod that defines an interface itself the game will most probably crash. This is because of internal corruption of different lua states.
2) The commands that are run in the interface function must not produce ANY error. That is not handled well at all right now. The inner lua states get corrupted and soonish the whole game crashes. What I mean here are "innocent" mistakes like:

Code: Select all

game.player.character.print("xxx")
There is no print function in the character table. It is in player directly.

Or using wrong names for items:

Code: Select all

game.player.character.insert{name="ammo-turret", count=1}
There is no "ammo-turret" it is "gun-turret".

I am aware of these two issues as of now. If you find any more please report them. We will fix these and they will be part of the next release. The question remains whether the next release will be 0.3.3 (soonish with these bugfixes) or 0.4.0 (end of April with the new features).

ManselD
Inserter
Inserter
Posts: 26
Joined: Mon Apr 01, 2013 10:43 pm
Contact:

Re: How to use my function in the game console?

Post by ManselD »

I will test this later, I've been up all night and couldn't get to sleep and I've been to the market >.< as soon as it hits 10pm I'm gone. But then i'll test the next day xD

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: How to use my function in the game console?

Post by ficolas »

FreeER wrote:Additional functionally would probably be especially useful for people (not pointing fingers lol) who have mods that they are splitting up into several, or intend to create several mods that work fine on their own but are intended to complement each other :D
Cof cof me cof cof :)

Also, but there isnt any way to do it without using remote?

Post Reply

Return to “Modding help”