Ren'Py 101: Writing Your Script Part II
by Rio
Here, we'll finish up learning the rest of the things you may want to put in your script and we'll end with preparing for the release of your game by covering how to make a README file. As usual, what I'll be covering:
1. Variables and If-Else Logic
To make a variable, it follows the same general outline of a python statement which is: $ variable_name = value
It is best to set up the main variables to be used within the game as it starts (i.e. the first label at the start of the story). To change the value of the variable, you have to make the change somewhere in the game. To change the values, just set the variable to True for the True-False type and incrementally increase or set another value to the variables that require numbers.
Below, we have a short-handed example of the variables being set at the beginning of a script, how it is changed in the game, and how it will ultimately decide the ending of the story.
The ending makes use of the if-else logic. Basically, when one statement is not met (i.e. True), Ren'Py will keep going down the list until one of the conditions is met or it reaches the last statement (else) and executes it. If one of the condition(s) is met, it will follow the statement after it. Here is the standard format for the if-else logic:
Yellow denotes that it is optional. You can have just a simple and direct if-else logic but in most cases, you will need to make use of an if-elif-else logic. You can have as many eif as you want, btw, but just make sure you start with if and you end with an else. Don't forget that the if, eif, and else line ends with a colon! For more complex conditions and operators* you may use, here are some examples:
*Operators are those used in math: =, +, -, <, >, %, *, !, and, or, xor, etc. Much like you have learned in math, operators are executed in a logical order like those in parenthesis are evaluated before other operators. Back to Dev Corner | Back to Top
2. MPEG Movies
Playing MPEG-movies is a recent feature added into Ren'Py. So far, it has not been used in any released Ren'Py game aside from the demo but I'm sure it will be used soon. Basically, to play a movie in fullscreen mode with Ren'Py, you just have to follow the python statement with yellow being optional: moviename.mpeg - the name of the mpeg file you want to play. delay - the amount of seconds before ending the cutscenes. Usually the length of the movie. Optional. loops=value - Number of times the movie will show again. 0 means once (default). -1 will loop movie infinitely. Optional.
Above is the actual script used from the Ren'Py demo. Notice that "delay" is used but "loops" is not.
Playing a movie fullscreen is not the only way you can display an mpeg movie. You can also have a displayable which shows the movie along with the rest of your game, complete with overlays and underlays as shown to the right. Like Eileen says in the demo, "Ren'Py can even overlay rendered images on top of a movie, The actual structure of the displayable python statement is as follows: size_width/height - the width and height of how you would like to display the video. For example, (320, 240). loops=value - much like previously stated, this is the number of times the movie will show. 0 means once (default value). -1 for a continuos loop. This is optional to put in, btw. To put it in a displayable like in the Ren'Py demo, you have to declare a value for movie first in an init label like this:
Then you can set the layers, including the movie, the displayable command, and proceed with the script from there. Here is an excerpt directly from the Ren'Py demo:
To stop the movie, you have to put in the $ renpy.movie_stop() python statement. Also note the use of "hide movie" which hides that movie layer much like it would for a character layer. There is no set order to do these two - you can have hide movie first if you want - but just make sure you have these two declared in the end. Note: A player can click to interrupt the cutscene and continue the story. They can also rollback and re-play the cutscene if they want. While the movie is playing, overlays and underlays are disabled (i.e. background scenes, dialogue boxes, etc) unless it's in the displayable mode. If you're playing the movie in windowed mode and you minimize the screen, the movie will still remain where it is and quite viewable (this applies for Windows, not sure if applicalbe to other OS's). Back to Dev Corner | Back to Top
3. Music and Sound Files
Music and sound files are that extra feature that gives your ren'ai game that extra punch when released. There is a big difference between the two though they may sound the same at first glance. Sound files takes care of short files such as those for the interface (button click sound, error sound, etc) whereas music files takes care of the longer, more in-depth files (i.e. background music). There are two ways sound files may be set into the game. The first kind is for the interface, to which Ren'Py already comes packaged with. The sounds and the actual setting of them are already in the script file, right under the mainmenu and frame lines under the init label.
The second way to set a sound file is inside the script itself. This could be a short sound effect like clashing of swords, punching, running, and the like. This is the basic python structure of a sound file in-game: There are some important notes regarding sound files. They include: 2. Once it is played, there is no way to stop the sound file.
Now let's get to the more useful part, playing music! As stated in the Reference Manual, Ren'Py can technically support playing "any format SDL_mixer supports (mp3, ogg, midi, mod, and more)", but mp3, ogg, and midi support are the only ones used so far. No worries, they are what you will most likely be using anyways. There are some things to note about music files as well, namely: 1. Music files cannot be archived. They will not work properly, otherwise. 2. Music files must be placed within the game directory. They may be put in their own folders but it must be under the game directory, though. 3. If the filename is renamed or mistyped within the game, Ren'Py will not play the music file. The game will proceed without any traceback or notices unlike for missing image or scene files so be noteful of this and be careful (i.e. test like crazy before releasing) Now that you've got those down, here is the two most important python statements regarding music files:
Music_start() will play your music file automatically once Ren'Py reaches this line. If another music file is already playing, the new music_start() will stop the older music and play the new called music file. Version 4.8 of Ren'Py introduces a new parameter "fadeout=0.0" to both music_start() and music_stop(). Basically, this is the number of seconds the music will take to fade out or fade in the currently playing music. If set to "None", music will stop and start abruptly. If you would prefer not having your music stop abruptly in between changes of music, you can use the python statement:
Back to Dev Corner | Back to Top
4. Text Styles with Text Tags
Text styles offer the control needed to emphasize words and expressions in dialogue. With it, you can punctuate instances by bolding, underlining, or italicizing them. You can also manipulate the size of the text from small to large and change it's color if you like. If you're familiar with HTML, it's much the same format but it uses {} instead of <>. If not, all you have to remember when using text tags is to start it with {command} and end it with {/command} which tells Ren'Py to stop applying the style to the text. List of Text Tags. (Name of ability - opening tag and then closing tag - notes) Size - {size=value} {/size} - value may be positive or negative in number and adds/substracts from the default font size which is a 22 on Vera.ttf. If subtracting {size=-2}, for example, then the font size will decrease to 20. Color - {color=#rgb} {/color} - RGB is the value you must declare to change the color. There is no fourth number/letter to worry about which is for opacity. For this, you have to declare the hexadecimal value which is comprised of letters (A to F) and numbers (0 to 9). Web safe colors usually have the numbers 0, 3, 6, and 9 with letters C, and F within it. Note: If, for some reason, you want to use a left brace in your script, just double it into so {{, and a single left brace will show up.
Should you decide to mix the text tags, make sure you start and end them in order. For example, if you open with a color tag and then follow it with a bold tag, then you should close it with the bold tag first and follow it up with the color tag like so: {color=#fff}{b}Correct way to open and close text tags{/b}{/color}. Back to Dev Corner | Back to To
5. Restarting the Game
There is only one way to restart the game. Just type out the words as shown in the example below and it'll automatically do it's function.
I use this when ending the game after the credits but if you'd rather just quit the game entirely, that is possible as well using the "return" command like so:
<<Writing Your Script Part I | Back to Dev Corner | Releasing Your Game Part I>> |