Return variable from 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
newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Return variable from a label?

#1 Post by newbiemate » Thu Feb 07, 2019 10:24 pm

I have a label that receives a variable, modifies it, then passes it to another label where I calculate some result. Once that second label finishes, I'd like to use that new result in the first label. How can I get the new value when the second label returns to the first label?

ie, this illustrates what I'm trying to do:

Code: Select all

label doit(i):
    $ i = do_something(i)
    return i

label run_it(x):
    $ x = x + 1
    call doit(x)

    # How do I then get the new value from the doit() call?

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Return variable from a label?

#2 Post by philat » Thu Feb 07, 2019 10:56 pm

Why wouldn't you just use a python function?

newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: Return variable from a label?

#3 Post by newbiemate » Thu Feb 07, 2019 11:05 pm

The example above was kinda simplified to show what I was trying to do. I actually have a mini-game in the second label, and it calls a lot of other things, like showing certain screens and all. I want the result of that mini-game to be returned to the calling label, and was wondering how I could do that.

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Return variable from a label?

#4 Post by philat » Thu Feb 07, 2019 11:13 pm


newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: Return variable from a label?

#5 Post by newbiemate » Thu Feb 07, 2019 11:17 pm

If the optional expression is given to return, it is evaluated, and it's result is stored in the _return variable. This variable is dynamically scoped to each context.
Does this mean I should do this:

Code: Select all

label run_it(x):
    $ x = x + 1
    call doit(x)

    $ x = _return

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

Re: Return variable from a label?

#6 Post by DragoonHP » Sat Feb 09, 2019 4:59 am

Yeah, that should work.

One more thing, variables created in a label are available in the global scope. So you could directly reference i in the run_it label after the doit label call.

Post Reply

Who is online

Users browsing this forum: Google [Bot]