How to use a label like a 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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

How to use a label like a function?

#1 Post by henvu50 »

I'm trying to make a function, but I get an error: "testFunction is not defined"

Code: Select all

label testFunction(val):
  if val = "one":
    return("hello")
  else:
    return("okay")

screen test:
 vbox:
  # should get set to a value of string "okay"
  $ globalVariable = testFunction("two") 

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to use a label like a function?

#2 Post by Remix »

In your use case it would be better to just use a Python function:

Code: Select all

init python:
    def testFunction(val):
        if val = "one":
            return "hello"
        else:
            return "okay"

screen test:
 vbox:
  # should get set to a value of string "okay"
  $ globalVariable = testFunction("two") 
A label as control flow like that would likely be ran using renpy.run( renpy.call( "label" ) ) or through an Action of a button using Call( "label" )
Frameworks & Scriptlets:

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to use a label like a function?

#3 Post by henvu50 »

Okay, I switched to python function, but the string value returned from the function always results in the name of the function, not the actual return value. Do you know why it's doing that?

Instead of returning "wtf", it returns the name of the function:

Code: Select all

<function testFunc at 0x08390938>
It doesn't return the actual string of "wtf".

Code: Select all


screen testScreen:
 vbox:
   $ globalVariable = testFunc(87)
   
   textButton "[globalVariable]":
     action Return(1)

init python:
    def testFunc(value):
        return("wtf")
        
The textbutton name only shows the name of the function: <function testFunc at 0x08390938> ... not the string "wtf".

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to use a label like a function?

#4 Post by henvu50 »

The following code:

Code: Select all

$ globalVariable = testFunc(87)
results in the error:

Code: Select all

TypeError: testFunc() takes no arguments (1 given)
It seems I can't have the word "return" in a function name, so this is solved.

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to use a label like a function?

#5 Post by henvu50 »

Everything is working now. I had two minor errors at the same time.

I need coffee!

Thanks for your help.

Post Reply

Who is online

Users browsing this forum: Google Feedfetcher