Page 1 of 1

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

Posted: Wed Jan 24, 2018 12:55 am
by sparkleface
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?

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

Posted: Wed Jan 24, 2018 11:29 am
by DragoonHP
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.

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

Posted: Wed Jan 24, 2018 1:14 pm
by trooper6
I’m on my phone and not able to look up the reference, but maybe check out ScreenVariables in the documentation.