[solved] Python Function

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
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

[solved] Python Function

#1 Post by Kaen » Tue Sep 23, 2014 6:30 pm

I'm trying to make a python function that changes the value of a variable:

Code: Select all

# The game starts here.
label start:

python:
    num = 1

    def add_num():
        num += 1
        return num

label testing:
    $ newnum = add_num()
    "[newnum]"
But I'm receiving an error:

Code: Select all

UnboundLocalError: local variable 'num' referenced before assignment
What should I do so I don't need to write $ num += 1 everytime I want this to happen through my game? I wish I could use a function to do this to me...
Last edited by Kaen on Wed Sep 24, 2014 11:04 am, edited 1 time in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 2565
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Python Function

#2 Post by Alex » Wed Sep 24, 2014 2:00 am

You need to treat your "num" variable as global one

Code: Select all

python:
    num = 1

    def add_num():
        global num
        num += 1
        return num
http://www.renpy.org/doc/html/python.ht ... -the-store

User avatar
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

Re: [solved] Python Function

#3 Post by Kaen » Wed Sep 24, 2014 11:04 am

Thank you very much Alex, it worked!

Post Reply

Who is online

Users browsing this forum: Atsyun, Bing [Bot], Google [Bot], Jackkel Dragon, Jacksono