printTable() (and maybe print()) could return a string?

background

I have run into a hard crash error that I'm trying to debug. I am trying to print useful info to a file.

The files we have access to are within our game's folder. I have a script that fetches the data I want from there and puts it in a folder in my current working directory. Script and relevant code below. This works great for simple strings.

tryna print to a file

The wall I'm running into is I'd like to easily be able to adapt print() and printTable() statements to printing to a file. My gut feeling is I just want them to return a string. I've been thinking of workarounds and played with pd.datastore for a bit but it doesn't seem to offer the granular control that a raw file does, and imposes json for the whole debug structure which isn't ideal.

How would this request improve your experience developing for Playdate? you might ask.

When there's a hard crash I lose sight of the console and can't see debug info at a critical time in development.

note

The SDK says

Printed text is also copied to stdout, which is helpful if you run the simulator from the command line

I have played with this and failed to get it to print anything for me. It's likely I've not read something pertinent here. But I have cat'd the stdout file on my Mac (ARM chip) via the terminal and have not seen anything output as the game runs and print statements display in the pd console.


code

This example of in-game debug code and the following shell script work in tandem to produce debug files that last beyond simulator crashes. This code just prints the current date-time at the top of the debug.txt file. Script can be modified to suit your needs but is pretty generic and versatile.

 if pd.buttonJustPressed('b') then
  -- CODE THAT WORKS
  if debug then
    local debug_file = pd.file.open("debug.txt", pd.file.kFileWrite)
    local current_datetime = pd.getTime()
    local time_string = ""..current_datetime.year.."-"..current_datetime.month.."-"..current_datetime.day.." "..current_datetime.hour..":"..current_datetime.minute..":"..current_datetime.second.."\n"
    debug_file:write(""..time_string.."\n")
    debug_file:close()
  end

  -- CODE I WISH WORKED
  if debug then 
    local debug_file = pd.file.open("debug.txt", pd.file.kFileWrite)
    local time_as_string = printTable(current_datetime)
    debug_file:write(""..time_as_string.."\n")
    debug_file:close() 
  end
end


fetch_debug.sh

#!/bin/bash
# Get the name of the current directory
current_dir=$(basename "$PWD")
# Set the source directory to ~/Developer/PlaydateSDK/Disk/Data/current_dir
source_dir=~/Developer/PlaydateSDK/Disk/Data/"$current_dir"
# Set the destination directory to current_dir/debug
destination_dir="$PWD"/debug
# Check if the destination directory exists
if [ ! -d "$destination_dir" ]; then
  # If not, create it
  mkdir "$destination_dir"
else
  # If yes, move all current files that are not directories into a folder
  # Get the current system timestamp in a human-readable format
  timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
  # Create a folder with the timestamp as the name
  mkdir "$destination_dir"/"$timestamp"
  # Move all files that are not directories into the folder
  find "$destination_dir" -maxdepth 1 -type f -exec mv {} "$destination_dir"/"$timestamp" \;
fi
# Loop through all the files in the source directory that have the word "debug" in them
for file in "$source_dir"/*debug*; do
  # Copy the file to the destination directory
  cp "$file" "$destination_dir"
done


details on my specific problem

This hard crash bug has since magically disappeared... ill leave it spoilered here, for science, but yeah. Spooky.

In my game a player is able to walk up to a something and interact with it. As it is right now the game runs fine before interacting with objects of this type in particular, but once I press the button to interact the simulator closes before I have a chance to see any debug info. These are "objects" in the sense that they are using the playdate object syntax to init and such.

Annoying to debug but not impossible. Currently the object looks like this (below, in json). I'm assuming my problem stems for this "RECURSION ERROR". Not sure where that is coming from, this is new.

{
	"center":"userdata 0x10deeaa60",
	"column":13,
	"hasPlayer":false,
	"hasReflection":false,
	"height":20,
	"id":213,
	"puzzlepieces": [
	],
	"row":11,
	"selected":true,
	"super": {
		"__index":"RECURSION ERROR",
		"class":"RECURSION ERROR",
		"className":"Tile",
		"init":"function 0x10de0a0b0",
		"super": {
			"__index":"RECURSION ERROR",
			"baseObject":"function 0x10ddcff50",
			"class":"RECURSION ERROR",
			"className":"Object",
			"init":"function 0x10ddcff20",
			"isa":"function 0x10ddd0090",
			"tableDump":"function 0x10ddd00c0"
		}
	},
	"width":20,
	"x":240,
	"y":200
}

RECURSION ERROR means that there's a cycle in the graph. If the json encoder continued recursing through the tables it'd get stuck in an infinite loop. That's not too surprising in this case--class objects typically have __index values pointing back at themselves.

But anyway, the printTable() function is in CoreLibs/object.lua in the SDK. If you want to customize it, just copy the code and do whatever you want! :slight_smile:

2 Likes

thank you for the quick reply. if that's normal that helps narrow down the debugging. appreciated.

i'll look into adding the return value myself, also might find other workarounds as it would be more convenient on future updates. Til I sort out the perfect solution strings with this persistent debug file writing/fetching will be plenty.