Using a function with text

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
GMHLee
Newbie
Posts: 16
Joined: Thu Aug 25, 2022 7:00 am
Contact:

Using a function with text

#1 Post by GMHLee » Sat Sep 03, 2022 3:02 am

Hi all,

Trying to write my own function that will display text for character cards in my game. Given the size and how often i'll probably be using the code for seperate things I'd like to know how get the text working in a function.

Primarily it consists of this:

Code: Select all

text "Affection:  " style "ultd":
        xanchor 1.0
        xpos 280
        ypos 650
    text "[Ava_stats.affeection]" style "ultd":
        xanchor 0
        xpos 290
        ypos 650    
with multiple stats and multiple characters. I could probably clean up my overall code quite a bit with a function call and probably some defined variables for the x and y pos which will come later.

As usual, tried to google it but so hard to find anything resembling an example.

My test attempt consisted of:

Code: Select all

intit python:
    def test_func():
        text("something")
Bbviously text is not a python syntax.
The best thing I found was print("xx"), and that obviously doesn't do anything.

Help appreciated as always.

User avatar
Angelo Seraphim
Regular
Posts: 32
Joined: Tue May 21, 2019 8:00 am
Completed: Enamored Risks (NaNoReNo 2020)
Projects: 616 Charagma
Organization: GLSUoA
Deviantart: glsuoa
itch: glsuoa
Location: London, UK
Discord: Just A Concept#9599
Contact:

Re: Using a function with text

#2 Post by Angelo Seraphim » Sat Sep 03, 2022 3:07 am

Hm... Maybe try something like this:

Code: Select all

init python:
    def test_func():
        return "something"
        ## However your function or class is set up.
... And then use the .format() method.

Code: Select all

 text "{}".format(Ava_stats.affeection())
See if that works for you.
Image

GMHLee
Newbie
Posts: 16
Joined: Thu Aug 25, 2022 7:00 am
Contact:

Re: Using a function with text

#3 Post by GMHLee » Sat Sep 03, 2022 3:29 am

Hmm... not sure it works, but then I'm not sure if i'm understanding it right or if I mis explained?
from your func example, would the return not exit the function?
what I'm trying to accomplish is instead of this:

Code: Select all

screen girl:
    text "Affection:  " style "ultd":
            xanchor 1.0
            xpos 280
            ypos 650
    text "[Ava_stats.affection]" style "ultd":
            xanchor 0
            xpos 290
            ypos 650
  ... repeat countless times
I'm looking for "something" (obviously this code won't work) like this:

Code: Select all

init python:
    def funcTextPos(xpos1, ypos1,xpos2, ypos2,.....xpos10, ypos10, style(would this even work?)):
         text "Affection:  " style "ultd":
            xanchor 1.0
            xpos xpos1
            ypos ypos1
    text "[Ava_stats.affection]" style "ultd":
            xanchor 0
            xpos xpos2
            ypos ypos1
            
Then in screen instead of what my previous scren girl example, it'd be:

Code: Select all

screen girl:
    funcTextPos(290,650,etc,etc...)
making it a lot more compact and cleaner.
Though granted perhaps a little harder to read and understand but eh, easier to manipulate without have to copy and past a wall of text and variables...

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Using a function with text

#4 Post by enaielei » Sat Sep 03, 2022 4:48 am

You can utilize screen's use statement.

Code: Select all

screen girl(**kwargs):
    text "Affection:  " style "ultd":
            xanchor 1.0
            properties kwargs
    text "[Ava_stats.affection]" style "ultd":
            xanchor 0
            properties kwargs

screen test():
  use girl(xpos=200, ypos=650)
A few notes.
text "Affection: " style "ultd" and stuff that you usually do inside screens wont work in python, that syntax is specifically made for renpy.
once you're inside a python block the program expects you to only write valid python statements and expressions.
You can however still do the same by using pythonic functions/classes builtin with Ren'Py. text is equivalent to a Text displayable. Using this kind of function/class works since they're pythonic.
Other displayables can be found here.

Code: Select all

init python:
  def girl(**kwargs):
    return Text("some text", **kwargs)
    
screen test():
  # You add a displayable using the add statement.
  add girl(xpos=200, ypos=650)
  # or just...
  add Text("something", xpos=200, ypos=650)
Last edited by enaielei on Sat Sep 03, 2022 5:00 am, edited 1 time in total.

GMHLee
Newbie
Posts: 16
Joined: Thu Aug 25, 2022 7:00 am
Contact:

Re: Using a function with text

#5 Post by GMHLee » Sat Sep 03, 2022 4:59 am

Thanks, I'll check out both options and docs and see if I can come up with something.
Worse case scenario what i have right now works, if just a bit clunky.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]