Resources:
The Ren'Py project and script file are attached to this post. Download either one and look through it, it's got working code in there that you can expand on.
What is ConditionSwitch?
This is best answered with an example. Suppose I'm putting some sort of fighting system in my game, be it a standard RPG-style fighting game or something more real-time. Either way, it's easy to imagine that I want the picture display of an enemy or the player's character to change when he gets damage. At first he'd be healthy, but the more damaged he gets, the worse the picture should look.
John
In this example we'll call our hapless target John. He will have three pictures. Apologies for the crappy quality of the pictures, but I needed something quick to use in the project. They're a little large, but I think the general idea comes across.
Picture 1 – undamaged – more then 50% hit points
John is doing fine. He is not hurt at all. It's just a fleshwound. Come back, coward! I can still bite!
Picture 2 – hurt – between 50% and 10% of hit points
John is hurt. He's not doing very well and could use a potion or some such.
Picture 3 – very hurt – Less then 10% of hit points
John is near death. Poor John.
The 'wrong' way to do it
As you can see, in this example we're giving John hit points. We're defining a variable for this: current_hp. At the beginning of the fight, it is set as follows:
Code: Select all
$ current_hp = 100
Now there are two ways of displaying a different picture for John when he gets progressively more hurt. With or without ConditionSwitch. To illustrated the difference, let's go with the 'wrong' way first: without ConditionSwitch. You'd code something like the following in Ren'Py.
Code: Select all
image john = “john.png”
image john hurt = “john_hurt.png”
image john_very_hurt = “john_very_hurt.png”
Code: Select all
if (current_hp < 10):
show john_very_hurt
elif (current_hp < 50):
show john_hurt
The ConditionSwitch way to do it
All the above together works, in theory. However, it means we have to run the check for John’s hitpoints whenever he gets damage. Also, if we have multiple images of John getting more damaged, one at 80%, one at 60%, one at 40%, etcetera, then this gets a bit more complicated. It’s much more elegant to use the ConditionSwitch function in Ren’Py. It doesn’t take away the need to check if certain conditions are true, but it will take the work of switching out of your hands. How does it work? The wiki page is here:
http://www.renpy.org/wiki/renpy/doc/ref ... tionSwitch
And gives us the following example:
Code: Select all
image bg waterfront = ConditionSwitch(
"time_of_day == 'day'", "waterfront_day.jpg",
"time_of_day == 'night'", "waterfront_night.jpg",
)
Let’s take the example of John and his constant fighting. Really, I worry about John. We are going to add a total of three different lines to the ConditionSwitch of John.
Line 1
Code: Select all
“current_hp < 10”, john_very_hurt.png”
Line 2:
Code: Select all
“current_hp/max_hp<0.5”, “john_ hurt.png”
Line 3:
Code: Select all
"True", “john.png”
Code: Select all
While running game code:
File "game\script.rpy", line 26, in script
Exception: Switch could not choose a displayable.
Code: Select all
image john = ConditionSwitch (
"current_hp < 10","john_very_hurt.png",
"current_hp < 50","john_hurt.png",
"True", "john.png"
)
Troubleshooting - Commas after picture names
Try not to forget the commas at the end of the lines (the ones after declaring the picture names)! It’s an easy thing to forget and then your code won't work. Doing this...
Code: Select all
image john = ConditionSwitch (
"current_hp < 10","john_very_hurt.png" <--- NO COMMA
"current_hp < 50","john_hurt.png" <--- NO COMMA
"True", "john.png"
)
Code: Select all
While running game code:
File "game\script.rpy", line 26, in script
NameError: name 'john_hurt' is not defined
As already described, the order is important. If you put the lines in in a different order, like so...
Code: Select all
image john = ConditionSwitch (
"current_hp < 50","john_hurt.png",
"current_hp < 10","john_very_hurt.png",
"True", "john.png"
)