[LIB 0.14+] Mod console

Doesn't fit in the other categories, like libraries, small fixes or modpacks.
Post Reply
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

[LIB 0.14+] Mod console

Post 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.
Attachments
console.lua
(906 Bytes) Downloaded 110 times
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.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: [LIB 0.14+] Mod console

Post 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.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

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

Re: [LIB 0.14+] Mod console

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

Nexela
Smart Inserter
Smart Inserter
Posts: 1822
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: [LIB 0.14+] Mod console

Post 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

Post Reply

Return to “Mod Packs / Libs / Special Interest”