Using a function to edit a variable [SOLVED]

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
Norezza
Newbie
Posts: 19
Joined: Tue Oct 17, 2017 7:51 pm
Contact:

Using a function to edit a variable [SOLVED]

#1 Post by Norezza »

Hello again, good people.

I'm currently trying to call a function which changes a variable. When the time of day is a specific value, I want the location value to change to a certain place.

I have a file initializing the aLoc value, part of it is

declarations.rpy >

Code: Select all

init python:
     aLoc = 
screens2.rpy >

Code: Select all

init python:
    def timeLoc():
        if tod == 15:
            aLoc == "dining"
            
dorm.rpy >

Code: Select all

label stranger:
    show hall
    $timeLoc()
    "[tod]"
    "[aLoc]"
    "I can't just knock on some random persons door..."
    jump hall
    return
    
The label is just to test the tod (time of day) and aLoc (Ariel's location).

The issue is that the value, aLoc is apparently unchanged. What am I doing wrong?
Thanks for the help :)
Last edited by Norezza on Mon Feb 26, 2018 5:40 pm, edited 1 time in total.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Using a function to edit a variable

#2 Post by xavimat »

You need to tell the function that is must use the global variable, not create a local one. Also, there is an error in the last line.

Code: Select all

init python:
    def timeLoc():
        global aLoc
        if tod == 15:
            aLoc = "dining" # <- Here only one = not two ==
On the other hand, it seems that part of the code is missing in the declarations file.
It's better practice to initialize variables with "default". No need "init".

Code: Select all

default aLoc = 0
default tod = 15
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Norezza
Newbie
Posts: 19
Joined: Tue Oct 17, 2017 7:51 pm
Contact:

Re: Using a function to edit a variable

#3 Post by Norezza »

Awesome, thank you!

Time to write 100 lines of code :D

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]