User-Defined Statements - Ren'Py Visual Novel Engine

documentation index ◦ reference manual ◦ function index

# User-Defined Statements

User-defined statements allow you to add your own statements to Ren'Py. This makes it possible to add things that are not supported by the current syntax of Ren'Py. User-defined statements are currently limited to a single line, and may not contain blocks.

User-defined statements must be defined in a python early block. What's more, the filename containing the user-defined statement must be be loaded earlier than any file that uses it. Since Ren'Py loads files in unicode sort order, it generally makes sense to prefix the name of any file containing a user-defined statement with 00. A user-defined statement cannot be used in the file in which it is defined.

Function: | renpy.statements.register | (name, parse=..., execute=..., predict=..., lint=..., next=...):

This registers a user-defined statement.

name is either a space-separated list of names that begin the statement, or the empty string to define a new default statement (the default statement will replace the say statement).

parse is a function that takes a lexer object. This function should parse the statement, and return an object. This object is passed as an argument to all the other functions. The lexer argument has the following methods:

- l.eol() - True if we are at the end of the line.
- l.match(re) - Matches an arbitrary regexp string.
- l.keyword(s) - Matches s.
- l.name() - Matches any non-keyword name. Note that this only counts built-in keywords.
- l.word() - Matches any word, period.
- l.string() - Matches a renpy string.
- l.integer() - Matches an integer, returns a string containing the integer.
- l.float() - Matches a floating point number.
- l.simple_expression() - Matches a simple python expression, returns it as a string.
- l.rest() - Skips whitespace, then returns the rest of the line.
- l.checkpoint() - Returns an opaque object representing the current point in the parse.
- l.revert(o) - Given the object returned by l.checkpoint(), returns there.

execute is a function that is called when the statement executes. It is passed a single object, the argument returned from parse.

predict is a function that is called to predict the images used by the statement. It is passed a single object, the argument returned from parse. It should return a list of displayables used by the statement.

lint is called to check the statement. It is passed a single object, the argument returned from parse. It should call renpy.error to report errors.

next is called to determine the next statement. It is passed a single object, the argument returned from parse. It should either return a label, or return None if execution should continue to the next statement.

## Lint Utility Functions

These functions are useful in writing lint functions.

Function: | renpy.lint.check_text_tags | (s):

Checks the text tags in s for correctness. Returns an error string if there is an error, or None if there is no error.

## Example

The creates a new statement "line" that allows lines of text to be specified without quotes.

    python early:

        def parse_smartline(lex):
            who = lex.simple_expression()
            what = lex.rest()
            return (who, what)

        def execute_smartline(o):
            who, what = o
            renpy.say(who, what)

        def lint_smartline(o):
            who, what = o
            try:
                eval(who)
            except:
                renpy.error("Character not defined: %s" % who)

            tte = renpy.lint.check_text_tags(what)
            if tte:
                renpy.error(tte)

        renpy.statements.register("l", parse=parse_smartline, execute=execute_smartline, lint=lint_smartline)

This is used like:

        line e "These quotes will show up," Eileen said, "and don't need to be backslashed."

documentation index ◦ reference manual ◦ function index