Re: How to use my function in the game console?
Posted: Sat Apr 06, 2013 10:44 am
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:
Naturally, the foo mod has to be enabled. Now you can play around in the console:
To get a car:
To get some turrets:
Testprint into the console.
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:
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:
There is no print function in the character table. It is in player directly.
Or using wrong names for items:
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).
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)
To get a car:
Code: Select all
remote.call("foo", "gimmecar")
Code: Select all
remote.call("foo", "gimme", "gun-turret", 10)
Code: Select all
remote.call("foo", "testprint", "this is a test message")
Code: Select all
game.player.print(remote.call("foo", "testprint", "this is a test message"))
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")
Or using wrong names for items:
Code: Select all
game.player.character.insert{name="ammo-turret", count=1}
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).