Debug Tool

It's often useful to output debug messages during development work. In Ren'Py it is often associated with a label in the script. Ideally it should be:

This Python function satisfies these conditions. File names are suggestions only.

util/debug.rpy

define  dbg = Character("Debug")        # The voice of debug

default debugLabels = False             # Set True for dbgLabel to say debug lines

init python:

    def dbgLabel(gLbl, lLbl=None, sfmt=None, *args, **kwargs):
        """
        Output a debug say statement if debugLabels is True.

        :param gLbl:    Global label name.
        :param lLbl:    Local label name, default None.
        :param sfmt:    Format string, default None.
        :param args:    Args passed to sfmt.format.
        :param kwargs:  Keyword args passed to sfmt.format.
        """
        if not debugLabels:
            return
        msg = "Label " + gLbl
        if lLbl is not None:
            msg += '.' + lLbl
        if sfmt is not None:
            msg += ": " + sfmt.format(*args, **kwargs)
        dbg(msg)

Usage

The first step is likely to be to turn on debug when the game runs in developer mode (from the Ren'Py launcher). Outside developer mode debug is left off. Alternatively use some other method to set and clear debugLabels.


label start:
    if config.developer:
        $ debugLabels = True
    $ dbgLabel('start')
    ...

To debug entrance to a label include a single line:


label act1Scene1:
    $ dbgLabel('act1Scene1')
    ...
Debug
Label act1Scene1

For a local label provide two parameters, both the global and the local:


label bar:
    ...
label .drink:
    $ dbgLabel('bar', 'drink')
    ...
Debug
Label bar.drink

To provide additional information use the third parameter. If you aren't using a local label, provide None as the second parameter.


label bar:
    $ dbgLabel('bar')
    ...
    $ dbgLabel('bar', None, 'done')
    return

Debug
Label bar
Debug
Label bar: done

As the third parameter is a format string both args and kwargs can be used for interpolation.


label bar:
    ...
label .travelTo(destLoc):
    $ dbgLabel('bar', 'travelTo', 'destLoc="{}"', destLoc)
    ...
Debug
Label bar.travelTo: destLoc="home"