Debugging during runtime?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Debugging during runtime?

Post by pauls313 »

The lua function debug.debug() stops everything as it debugs, but my game is a multiplayer one, and allowing realtime commands to be given by the server administrators would be incredibly helpful. Instantiating new items wherever they wish, changing the health variable of players, creating new items on the go. Is this possible? Or, at least, would it be possible to, with one single command, run debug, run a line of code, and immediately go back to the game?
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Debugging during runtime?

Post by pedrosgali »

I don't think you need debug.debug(). For server commands I tend to put a lot of functions in a table, this way I can send a string to the server and it can use the first word of that string to see if there is a command in my table with that key, if there is you can use the rest of the string as arguments.
So if I send "spawn [itemname] [playerId]" the server splits that down into a table of 3 words separating at the spaces then uses the first word to lookup the spawn command and passes the rest of the table to the command as arguments. These arguments are always passed as strings so if [playerId] is a number it's on you in the spawn command to check that the data sent can be turned into a number. If it can't then you need to send a reply telling them they input the command incorrectly. Here's a very quick and dirty example...

Code: Select all

--Split a given string at the spaces to give a list of words.
local function split(str)
  local words = {}
  for w in str:gmatch("%S+") do
    table.insert(words, w)
  end
  return words
end

--Generic function to spawn a thing, this could be any server command you want.
local function spawn(args)
	if type(args[1]) == "string" and tonumber(args[2]) then
		--Spawn a thing
	else
		--Return an error
	end
end

--Put those commands into a table
local commands = {
	spawn = spawn,
}

--When you receive a message
local args = split(message)
local com = args[1]
table.remove(args, 1) --Don't forget to remove the first word as that has now been used.
if commands[com] ~= nil then
	commands[com](args)
else
	return "Invalid command."
end
If you want to be able to send arbitrary code to the server then try this function:

Code: Select all

local function try(f)
  local status, exception = pcall(f)
  if not status then
    print("ERROR: "..exception)
  end
end
You pass try a function (f) which it tries to execute and on failure will print the error without crashing the game. With a server you should print the error and send it back to the player that called the command to inform them that there was an error. This type of function is very insecure unless you sandbox the console as they could send "os.shutdown()" or much much worse...

Code: Select all

try(loadstring("os.execute('sudo rm -rf /')") --Lol, don't ever type this into a linux command line. :P

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Debugging during runtime?

Post by pgimeno »

There are some debug libraries out there allowing you to enter commands. Here are a few:

REPLer (for the system console, works in a separate thread, but the commands are executed in the main thread):
https://github.com/bartbes/love-misc-li ... er/docs.md

Lovebird (includes a mini-webserver to enter commands through a browser window):
https://github.com/rxi/lovebird

LOVEDEBUG - this is the original, but take a look at the README because it links to an 11.1 version
https://github.com/Ranguna/LOVEDEBUG
Post Reply

Who is online

Users browsing this forum: No registered users and 222 guests