ArceusPower101
ArceusPower101

Reputation: 5

Renpy ELIF statement

I have been looking at renpy's tutorial on how to make choices, and for the most part, have it figured out except for one minor thing.

How do I correctly use an elif statement? I have looked up basic python elif statements and even an actual site on how to use one in renpy and can't get it to work.

(I have attached a screenshot of my code along with my error, any help is greatly appreciated)

Here is a snippet of my code:

define e = Character("???")
$ mage = False
$ warrior = False
$ archer = False

# The game starts here.

label start:
# Show a background.

scene bg black

# This shows a character sprite. 

show weird orb

# These display lines of dialogue.

e "Welcome human, what is your name?"

python:
    name = renpy.input(_("What's your name?"))
    name = name.strip() or __("John")

define m = Character("[name]")

e "Hmm, [name] is it?"
e "That's a wonderful name!"
m "Where am I?"
e "You'll know in good time, my child."
e "For now, tell me a bit about yourself"
menu:
    e "Which of these do you prefer?"
    "Magic":
        jump magic
    "Brute Force":
        jump force
    "Archery":
        jump archery

label magic:
    e "You chose magic."
    $ mage = True
    jump enter

label force:
    e "You chose brute force."
    $ warrior = True
    jump enter

label archery:
    e "You chose archery."
    $ archer = True
    jump enter

label enter:
    if mage:
        m "I'm a mage."
    elif warrior:
        m "I'm a warrior."
    else:
        m "I'm an archer"
return

Here's a copy of the error:

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 66, in script
    if mage:
  File "game/script.rpy", line 66, in <module>
    if mage:
NameError: name 'mage' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 66, in script
    if mage:
  File "C:\Users\ArceusPower101\Downloads\renpy-7.0.0-sdk\renpy\ast.py", line 1729, in execute
    if renpy.python.py_eval(condition):
  File "C:\Users\ArceusPower101\Downloads\renpy-7.0.0-sdk\renpy\python.py", line 1943, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "C:\Users\ArceusPower101\Downloads\renpy-7.0.0-sdk\renpy\python.py", line 1936, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 66, in <module>
    if mage:
NameError: name 'mage' is not defined

Windows-8-6.2.9200
Ren'Py 7.0.0.196
Test 1.0
Thu Aug 23 02:06:20 2018

enter image description here

Upvotes: 0

Views: 2056

Answers (1)

Blckknght
Blckknght

Reputation: 104712

Your code is giving you an exception because these three lines are never run:

$ mage = False
$ warrior = False
$ archer = False

They don't run because they appear above the start: label, which is where the code starts running.

There are a few ways to fix the issue. One is to simply rearrange the code so that the start label appears above those lines. Another option is to use a default statement for each of the assignments:

default mage = False
default warrior = False 
default archer = False

The default statements will be run once when the game starts and when a game is loaded, but only if the variable isn't already defined.

Upvotes: 2

Related Questions