[Solved] UnboundLocalError: local variable 'vllager' referenced before assignment

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

[Solved] UnboundLocalError: local variable 'vllager' referenced before assignment

#1 Post by renardjap »

Hello :)

I want to create a function to create new villager in my game if there is enough food. But the game return me UnboundLocalError: local variable 'vllager' referenced before assignment. But I made a villager variable, I don't understand why the game doesn't find it...

Code: Select all

screen board:
    vbox:
        xalign 0.5
        yalign 0.5
        text "Villageois inactifs: [villager]"
        textbutton "Créer villageois":
            action Function(creat_villager)

init python:
    def creat_villager():
        if food >= 10:
            villager += 1
        else:
            renpy.notify("You don't have enough food")

define e = Character('Eileen', color="#c8ffc8")
default villager = 0
default food = 10

label start:

    show screen board
    "[villager]"
    "[food]"
    e "Hello"

    return
Thnaks for help
Last edited by renardjap on Sat Mar 07, 2020 12:29 pm, edited 1 time in total.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: UnboundLocalError: local variable 'vllager' referenced before assignment

#2 Post by RicharDann »

You need to use global statement to let Ren'Py know you want to modify a global variable (created with default):

Code: Select all

default villager = 0
init python:
    def creat_villager():
        global villager # <--- access the variable 
        if food >= 10:
            villager += 1
Otherwise the function will treat villager as a local variable (one created inside the function), that hasn't been created yet.
A more detailed explanation: viewtopic.php?t=50964#p491426
The most important step is always the next one.

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: UnboundLocalError: local variable 'vllager' referenced before assignment

#3 Post by renardjap »

Works very well ! Thank you for your help !!!

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot]