Can a label return data like functions in other languages?

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
Wap
Newbie
Posts: 12
Joined: Wed Jul 13, 2016 6:11 pm
Contact:

Can a label return data like functions in other languages?

#1 Post by Wap »

Hi there. Aside from making a few little mods for existing games, I'm pretty new to RenPy. I'm also not super experienced with programming - getting programming languages to do what I want is always a bit of a struggle, but it's generally a fun one. Just giving you this info so you know where I'm at, experience and skill-wise.

In other languages I've seen, you can have a function return some kind of data. I think of labels in RenPy as functions, since they seem to work like they do. What I mean is, well I'll show you an example. The following is a label I wrote that converts a numerical value to a string (if there's a way of doing this natively, please let me know, but it's not really the point of this question). So the way I have it working now is as follows:

Code: Select all


label numWord(number=0):
    $ returnData = ""
    $ ifPlural = "s"
    $ returnData = "DUNNO"

    if number == 0:
        $ returnData = "no"
    if number == 1:
        $ returnData = "one"
        $ ifPlural = ""
    if number == 2:
        $ returnData = "two"
    if number == 3:
        $ returnData = "three"
    if number == 4:
        $ returnData = "four"
    if number == 5:
        $ returnData = "five"
    if number == 6:
        $ returnData = "six"
    
    return

call numWord(3)
$ whatToSay = "I ate "+returnData+" cakes"+ifPlural+"."
# "I ate three cakes."
The label is called to turn '3' into 'three'. (Ignore 'ifPlural' for the sake of this example.) What I want to know is if you can do something closer to the below. I've tried a few things and can't get it to work, but I've seen it in other languages:

Code: Select all


label numWord(number=0):
    $ returnData = ""
    $ ifPlural = "s"
    $ returnData = "DUNNO"

    if number == 0:
        $ returnData = "no"
    if number == 1:
        $ returnData = "one"
        $ ifPlural = ""
    if number == 2:
        $ returnData = "two"
    if number == 3:
        $ returnData = "three"
    if number == 4:
        $ returnData = "four"
    if number == 5:
        $ returnData = "five"
    if number == 6:
        $ returnData = "six"
    
    return returnData 

$ whatToSay = "I ate "+numWord(3)+" cakes"+ifPlural+"."
# "I ate three cakes."
I've fiddled with this for a bit and I get errors so I'm not sure if it's possible.

User avatar
andrewngn13
Regular
Posts: 65
Joined: Thu Nov 13, 2014 2:41 pm
Projects: Recast
Skype: andrewngn13
Location: Glued to my desktop
Contact:

Re: Can a label return data like functions in other language

#2 Post by andrewngn13 »

I'm pretty foggy minded at the moment, but...can't you write up a python function instead?
"Feel feel to idea-bounce off me."
No, like seriously, just send a pm and I'll respond what I think. I'm open to reading anything.

Wap
Newbie
Posts: 12
Joined: Wed Jul 13, 2016 6:11 pm
Contact:

Re: Can a label return data like functions in other language

#3 Post by Wap »

It does not appear to be the case. I googled how to do that, made the following function:

Code: Select all

def functionName():
    "The function has been called."
    return 1
And got the following error:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/toolbox.rpy", line 176: expected statement.
    def functionName():
                    ^

Ren'Py Version: Ren'Py 6.99.10.1227

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

Re: Can a label return data like functions in other language

#4 Post by trooper6 »

Yeah, labels aren't functions. You want to write an actual function.

This is a working example, in context, of what you seem to want to do:

Code: Select all

init python:        
    def numWord(num=0):
        if num == 0:
            return "no", "s"
        if num == 1:
            return "one", ""
        if num == 2:
            return "two", "s"
        if num == 3:
            return "three", "s"

# The game starts here.
label start:
    "The Test Starts"
    
    $ w,p = numWord(0)
    "I ate [w] cake[p]."

    $ w,p = numWord(1)
    "I ate [w] cake[p]."
    
    $ w,p = numWord(2)
    "I ate [w] cake[p]."
    
    $ w,p = numWord(3)
    "I ate [w] cake[p]."
    
    "The Test Ends"
    return
Now...I don't know why you would want to do this...but there you go.
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

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Can a label return data like functions in other language

#5 Post by gas »

Natively convert a number (maybe an index) to a string: use a list.

$ mylist=["Ohi","Ahoi","Hadoken","Shoryuken"]
$ myindex=2
$ mymove=mylist[myindex]
e "I'll destroy you with my [mymove]"

List index start to count from 0, so in that given case, myindex=2 refer to the third element, "Hadoken".

Also you can try to use a dictionary like this:

Code: Select all

nums = {'1':'one',
        '2':'two',
        '3':'three',
        '4':'four',
        '5':'five',
        '6':'six',
        '7':'seven',
        '8':'eight',
        '9':'nine'}
and use

Code: Select all

nums["1"]
to return "one".

I think lists are cuter, anyway.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

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

Re: Can a label return data like functions in other language

#6 Post by xavimat »

https://www.renpy.org/doc/html/label.ht ... -statement
The return statement can return data. This is stored in the _return variable.
But, I would use a function.
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)

Wap
Newbie
Posts: 12
Joined: Wed Jul 13, 2016 6:11 pm
Contact:

Re: Can a label return data like functions in other language

#7 Post by Wap »

Thanks for all the input, people! Including those who PM'd me. Y'all are a nice bunch.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Ocelot