File: User-Defined_Statements.html

package info (click to toggle)
renpy 6.10.2.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 19,468 kB
  • ctags: 5,383
  • sloc: python: 17,801; ansic: 7,116; makefile: 127; sh: 15
file content (88 lines) | stat: -rw-r--r-- 7,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<html><head><title>User-Defined Statements - Ren'Py Visual Novel Engine</title><link href="../shared.css" rel="stylesheet"><link href="../monobook.css" rel="stylesheet"><link href="../common.css" rel="stylesheet"><link href="../monobook2.css" rel="stylesheet"><link href="../docs.css" rel="stylesheet" /></link></link></link></link></head><body><div id="bodyContent">
			<p class="docnav"><a href="../index.html">documentation index</a> &#9702; <a href="Reference_Manual.html">reference manual</a> &#9702; <a href="Function_Index.html">function index</a></p><p><a id="User-Defined_Statements" name="User-Defined_Statements"></a></p>
<h1><span class="mw-headline">User-Defined Statements</span></h1>
<p>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.</p>
<p>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.</p>
<p><span id="renpy.statements.register" /></p>
<table>
<tr>
<td valign="top">Function:</td>
<td valign="top"><b>renpy.statements.register</b></td>
<td valign="top">(name, parse=..., execute=..., predict=..., lint=..., next=...):</td>
</tr>
</table>
<div class="renpy-doc">
<p>This registers a user-defined statement.</p>
<p><i>name</i> 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).</p>
<p><i>parse</i> 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:</p>
<ul>
<li>l.eol() - True if we are at the end of the line.</li>
<li>l.match(re) - Matches an arbitrary regexp string.</li>
<li>l.keyword(s) - Matches s.</li>
<li>l.name() - Matches any non-keyword name. Note that this only counts built-in keywords.</li>
<li>l.word() - Matches any word, period.</li>
<li>l.string() - Matches a renpy string.</li>
<li>l.integer() - Matches an integer, returns a string containing the integer.</li>
<li>l.float() - Matches a floating point number.</li>
<li>l.simple_expression() - Matches a simple python expression, returns it as a string.</li>
<li>l.rest() - Skips whitespace, then returns the rest of the line.</li>
<li>l.checkpoint() - Returns an opaque object representing the current point in the parse.</li>
<li>l.revert(o) - Given the object returned by l.checkpoint(), returns there.</li>
</ul>
<p><i>execute</i> is a function that is called when the statement executes. It is passed a single object, the argument returned from parse.</p>
<p><i>predict</i> 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.</p>
<p><i>lint</i> is called to check the statement. It is passed a single object, the argument returned from parse. It should call <a class="new" href="http://www.renpy.org/w/index.php?title=renpy/doc/reference/functions/renpy.error&amp;action=edit&amp;redlink=1" title="renpy/doc/reference/functions/renpy.error (page does not exist)">renpy.error</a> to report errors.</p>
<p><i>next</i> 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.</p>
</div>
<p><a id="Lint_Utility_Functions" name="Lint_Utility_Functions"></a></p>
<h2><span class="mw-headline">Lint Utility Functions</span></h2>
<p>These functions are useful in writing lint functions.</p>
<p><span id="renpy.lint.check_text_tags" /></p>
<table>
<tr>
<td valign="top">Function:</td>
<td valign="top"><b>renpy.lint.check_text_tags</b></td>
<td valign="top">(s):</td>
</tr>
</table>
<div class="renpy-doc">
<p>Checks the text tags in s for correctness. Returns an error string if there is an error, or None if there is no error.</p>
</div>
<p><a id="Example" name="Example"></a></p>
<h2><span class="mw-headline">Example</span></h2>
<p>The creates a new statement "line" that allows lines of text to be specified without quotes.</p>
<pre>
<span class="kwa">python</span> early<span class="sym">:</span>

    <span class="kwa">def</span> <span class="kwd">parse_smartline</span><span class="sym">(</span>lex<span class="sym">):</span>
        who <span class="sym">=</span> lex<span class="sym">.</span><span class="kwd">simple_expression</span><span class="sym">()</span>
        what <span class="sym">=</span> lex<span class="sym">.</span><span class="kwd">rest</span><span class="sym">()</span>
        <span class="kwa">return</span> <span class="sym">(</span>who<span class="sym">,</span> what<span class="sym">)</span>

    <span class="kwa">def</span> <span class="kwd">execute_smartline</span><span class="sym">(</span>o<span class="sym">):</span>
        who<span class="sym">,</span> what <span class="sym">=</span> o
        renpy<span class="sym">.</span><span class="kwd">say</span><span class="sym">(</span>who<span class="sym">,</span> what<span class="sym">)</span>

    <span class="kwa">def</span> <span class="kwd">lint_smartline</span><span class="sym">(</span>o<span class="sym">):</span>
        who<span class="sym">,</span> what <span class="sym">=</span> o
        <span class="kwa">try</span><span class="sym">:</span>
            <span class="kwb">eval</span><span class="sym">(</span>who<span class="sym">)</span>
        <span class="kwa">except</span><span class="sym">:</span>
            renpy<span class="sym">.</span><span class="kwd">error</span><span class="sym">(</span><span class="str">"Character not defined: %s"</span> <span class="sym">%</span> who<span class="sym">)</span>

        tte <span class="sym">=</span> renpy<span class="sym">.</span>lint<span class="sym">.</span><span class="kwd">check_text_tags</span><span class="sym">(</span>what<span class="sym">)</span>
        <span class="kwa">if</span> tte<span class="sym">:</span>
            renpy<span class="sym">.</span><span class="kwd">error</span><span class="sym">(</span>tte<span class="sym">)</span>

    renpy<span class="sym">.</span>statements<span class="sym">.</span><span class="kwd">register</span><span class="sym">(</span><span class="str">"l"</span><span class="sym">,</span> parse<span class="sym">=</span>parse_smartline<span class="sym">,</span> execute<span class="sym">=</span>execute_smartline<span class="sym">,</span> lint<span class="sym">=</span>lint_smartline<span class="sym">)</span>
</pre>
<p>This is used like:</p>
<pre>
    line e <span class="str">"These quotes will show up,"</span> Eileen said<span class="sym">,</span> <span class="str">"and don't need to be backslashed."</span>
</pre>



<div class="visualClear" />
		<hr /><p class="docnav"><a href="../index.html">documentation index</a> &#9702; <a href="Reference_Manual.html">reference manual</a> &#9702; <a href="Function_Index.html">function index</a></p></div>
	</body></html>