Ren'Py Syntax Error?

WindwardGames

Newbie
Game Developer
Feb 18, 2020
34
1,123
Hi all,

I've been using Ren'Py for a little while, but pretty much for pure visual novels.
Now I'm trying to add remembered choices and am getting a "SyntaxError" when the game hits that part.
It's simple code so I'm sure it's something obvious, but what am I missing?

if $ mara_shower == 3:
jump maratalk3
if $ mara_shower == 2:
jump maratalk2
if $ mara_shower == 1:
jump maratalk1

Thanks.
 

skot205

Newbie
Mar 19, 2017
25
33
Try removing the dollar signs.

Code:
if mara_shower == 3:
    jump maratalk3
if mara_shower == 2:
    jump maratalk2
if mara_shower == 1:
    jump maratalk1
 
  • Like
Reactions: WindwardGames

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
$ is short hand for python:

It's not "this is a variable" and more "I want do a python thing".

I'm guessing you've seen something like $ mara_shower = 1, which is basically "use python to set the variable mara_shower to a value of 1.

However, your if statements don't need to invoke python to do their thing.

This is what you want...

Python:
    if mara_shower == 3:
        jump maratalk3
    if mara_shower == 2:
        jump maratalk2
    if mara_shower == 1:
        jump maratalk1

Although that too could be slightly improved by elif (short for "else if").
Python:
    if mara_shower == 3:
        jump maratalk3
    elif mara_shower == 2:
        jump maratalk2
    elif mara_shower == 1:
        jump maratalk1

Though you're only talking about a very tiny theoretical improvement in the execution speed of the code.
 

the66

beware, the germans are cumming
Modder
Donor
Respected User
Jan 27, 2017
7,808
24,389
in this case even one of those is enough:
jump expression "maratalk{}".format(mara_shower)
jump expression "maratalk" + str(mara_shower)
$ renpy.jump("maratalk{}".format(mara_shower))
$ renpy.jump("maratalk" + str(mara_shower))

 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
This is what you want...

Python:
    if mara_shower == 3:
        jump maratalk3
    if mara_shower == 2:
        jump maratalk2
    if mara_shower == 1:
        jump maratalk1

Although that too could be slightly improved by elif (short for "else if").
Python:
    if mara_shower == 3:
        jump maratalk3
    elif mara_shower == 2:
        jump maratalk2
    elif mara_shower == 1:
        jump maratalk1

Though you're only talking about a very tiny theoretical improvement in the execution speed of the code.
Just because I like to pick nits - I don't think there's any theoretical improvement in the second case. In both cases, if an "if" test succeeds, you jump away and never execute any of the subsequent tests. If the "if" test fails, in both cases you then have to execute the next test in sequence. So, in both cases, if it's "3" you get one comparison, "2" gets you 2, "1" gets you three, and anything else also gets you three (and drops through). Unless Python got REALLY tricky and turned it into a jump table, which I doubt...

Or are you seeing something I'm missing?
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
[...] I don't think there's any theoretical improvement in the second case. [...]

Or are you seeing something I'm missing?
The theoretical part in my head was that compilers and CPU architecture have all sorts of voodoo to optimize execution of code, especially in multi-core environments. I'd be surprised if Python didn't implement some sort of code optimization, but I don't know that it DOES either. It just struck me as the sort of code that could be optimized to not run in the same sequence it's typed into the code under certain circumstances (even if that's at the CPU core level) and that elif was more likely to trigger that optimization than a series of straight up if statements.

So. no, you aren't missing anything. Just me connecting theoretical dots of knowledge that may or may not apply here.

If anything, it was just my excuse to introduce the use of elif for anyone reading this who may not have been aware of it.
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
The theoretical part in my head was that compilers and CPU architecture have all sorts of voodoo to optimize execution of code, especially in multi-core environments. I'd be surprised if Python didn't implement some sort of code optimization, but I don't know that it DOES either. It just struck me as the sort of code that could be optimized to not run in the same sequence it's typed into the code under certain circumstances (even if that's at the CPU core level) and that elif was more likely to trigger that optimization than a series of straight up if statements.
Fair point. You never know what kind of interesting optimization the gurus that write the compilers will come up with. :)
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,966
16,215
Or are you seeing something I'm missing?
Yes. You're missing the implicit jump.

It's :
  • Test the first condition, it's false
  • Test the second condition, it's true
  • do something
  • Test the third condition, it's false
  • do whatever is after that.
versus :
  • Test the first condition, it's false.
  • Test the second condition, it's true.
  • do something.
  • jump outside of the /if/ structure.
  • do whatever is after that.

The if/elif structure is a better approach and a time saver one, as long as the order of each condition is thought seriously.
If the most common condition is the last of your if/elif structure, you'll gain nothing. But at the opposite, if you put it as first condition, you avoid a lot of unnecessary tests.


This said, globally speaking, with each condition having an equal chance to happen, the gain will be sensible, especially if you have a lot of tests in the structure.

With 4 calls, a pure if structure of four conditions will lead to 16 tests
You don't have permission to view the spoiler content. Log in or register now.
This while the same, but expressed as an if/else structure, would only need 10 tests.
You don't have permission to view the spoiler content. Log in or register now.

It's a geometrical progression, so the more conditions you'll have, the more you'll gain if you use the if/elif structure. But the gain is sensible even with the most basic structure. With an equal distribution of the values, even something as simple as :
Code:
if a is True: do something
else: do another thing
would be 1/4 faster than :
Code:
if a is True: do something
if a is False: do another thing
After, it don't mean that the pure if approach is effectively bad. It's just slower, but to be effectively sensible on a modern computer, you need to have a lot of complex conditions, and to call them frequently.
So, it's only to avoid in screens, since they are refreshed more or less once every two seconds. Yet only if really you use a tons of complex conditions.
 
  • Like
Reactions: Rich

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
Yes. You're missing the implicit jump.
(snip)
All extremely true, but in the specific case presented in the OP, the "do something" is a jump elsewhere, and the "if" tests are all testing the same variable, which collapses the number of real cases from 8 (three independent tests) to 4 (three specific values on the single variable, plus "none of the above")

I completely agree that if the "do something's" are NOT jumps, and if the tests are independent of one another, that the behavior of the two cases is entirely different, but I was reacting to the very specific set of code presented.
 
  • Like
Reactions: anne O'nymous

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,966
16,215
All extremely true, but in the specific case presented in the OP, the "do something" is a jump elsewhere, [...]
Oh, yeah... my bad, I have too much things on my mind actually, I didn't noticed this particularity.