Page 1 of 1

[LIB 0.14+] Mod console

Posted: Sun Sep 18, 2016 11:11 pm
by Adil
Here's little something I've devised recently to debug and test the mods.
Standard console runs in its own environment and cannot access the inner state of the mod. That state can only be accessed by functions from within the mod. You can use loadstring inside the mod to execute an orbitrary command in the environment.
This file defines a simple interface to the above route.

To use it, simply put file in your mod and put `require "console" in control.lua
When ingame use following interface call:

Code: Select all

/c remote.call('console','show')
A textbox will appear, in which you can type your code, which will be run with a click onto the button.
Remove the lib when releasing the mod. It is not sophisticated and not designed to avoid clashes in case it is present in multiple mods.

Re: [LIB 0.14+] Mod console

Posted: Mon Sep 19, 2016 10:33 am
by aubergine18
Always wondered but not got round to testing yet, is it possible to include a file from another mod, for example:

Code: Select all

require '__SomeOtherMod__/api.lua'
Where your mod would define SomeOhterMod as a dependency.

Re: [LIB 0.14+] Mod console

Posted: Mon Sep 19, 2016 6:19 pm
by Adil
Last time I've tried something similar, it didn't work.
edit: and for the purposes of this lib it isn't actually relevant. The idea is to have a single file that can be easily dropped into the mod without much alterations, with the mod dependency way, it would require more alterations to mod to make it launch.

Re: [LIB 0.14+] Mod console

Posted: Mon Oct 10, 2016 10:50 pm
by Nexela
Thanks Adil! Here is a Slightly modified version for use with STDLib. With the added bonus that it can be left in your released mod.

So much easier to debug stuff without having to add in debug code :)

Code: Select all

local USE_FALLBACK_INTERFACE = false

--Console Code from adil modified for use with STDlib
Gui = Gui or require("stdlib.gui.gui")

local function create_gui_player(player)
	if player.gui.left.console then player.gui.left.console.destroy() end
	local c=player.gui.left.add{type='frame',name='console',direction='horizontal'}
	local t = c.add{type='textfield',name='console_line'}
  t.style.minimal_width=600
  t.style.maximal_width=600
	c.add{type='button', name='console_enter',caption='<', tooltip="Run Script"}
  c.add{type='button', name='console_clear', caption='C', tooltip="Clear Input"}
  c.add{type='button', name ='console_close', caption="X", tooltip="Close"}
end

--console.create_gui = function(player)
local function create_gui(player)
	--if not sent with a player, then enable for all players?
	if not (player and player.valid) then
		for _, cur_player in pairs(game.players) do
			create_gui_player(cur_player)
		end
	else
		create_gui_player(player)
	end
end

--local second=false
local function handler(event)
	local i=event.element.player_index
	local p=game.players[event.player_index]
	--if second then second=false return end
	local s=p.gui.left.console.console_line.text
	assert(loadstring(s))()
	game.write_file('console.log',s..'\n',true,i)
	--second=true
end
Gui.on_click("console_enter", handler)

local function close(event)
	local p = game.players[event.player_index]
	p.gui.left.console.destroy()
end
Gui.on_click("console_close", close)

local function clear(event)
  local p = game.players[event.player_index]
	p.gui.left.console.console_line.text = ""
end
Gui.on_click("console_clear", clear)

--TODO Implement "History"

--Fallback interface --- set USE_FALLBACK_INTERACE = true and
--just using a require("path.to.console") in your control will
--create the console interface, this interface is only recomended for local testing. 
--If more then 1 mod adds it, the first mod to add it will be the enviorment used
if USE_FALLBACK_INTERFACE and not remote.interfaces.console then
	remote.add_interface("console", {show = function(player) create_gui(player) end})
end

--return the create_gui function
--example usage:
--remote.add_interface("my_interface", {show=require("path.to.console")})
--/c remote.call("my_interface", "show", game.player)
return create_gui