How to locally scope a variable in a screen vs a label?

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
sparkleface
Newbie
Posts: 9
Joined: Mon Dec 25, 2017 12:37 pm
Contact:

How to locally scope a variable in a screen vs a label?

#1 Post by sparkleface » Wed Jan 24, 2018 12:55 am

How do I declare a local variable inside a screen block? Say As a general example, say I have something like this:

Code: Select all

screen my_screen:
    for i in mylist:
        $ name = mylist[i]["name"] + some_string
    
        if name == "blah":
            text "Hello, " + name
        elif name == "blah2":
            text "Hey, " + name

However, is "name" set in the global space?

Would this lead to issues if I use the same variable name elsewhere in the code? ie,

Code: Select all

screen my_screen:
    for i in mylist:
        $ name = mylist[i]["name"] + some_string
    
        if name == "blah":
            text "Hello, " + name
        elif name == "blah2":
            text "Hey, " + name
            
            
label hello:

    $ name = get_name()
    
    call screen my_screen
    
    if name == "buns":
        pass
    if name == "bums":
        pass
    
    e "It's " + name            
            
Basically I'm worried that one block of code containing the same variable name (name in this case) will be overwritten by some other block of code, leading to confusing results.

Is there a way to locally scope the variable within a screen and a label block? Or are they already locally scoped?

User avatar
DragoonHP
Miko-Class Veteran
Posts: 710
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: How to locally scope a variable in a screen vs a label?

#2 Post by DragoonHP » Wed Jan 24, 2018 11:29 am

For screens, use default. So default name = "blah"

Labels don't have a concept of local variables. Whatever variable you declare inside a label will be available globally.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3442
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How to locally scope a variable in a screen vs a label?

#3 Post by trooper6 » Wed Jan 24, 2018 1:14 pm

I’m on my phone and not able to look up the reference, but maybe check out ScreenVariables in the documentation.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: No registered users