Running a function from within dialogue

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
arti
Newbie
Posts: 7
Joined: Sat Jun 25, 2016 11:34 pm
Contact:

Running a function from within dialogue

#1 Post by arti » Sun Sep 04, 2022 11:08 pm

Hello, sorry if this has been asked or is obvious, but I couldn't find anything.

Is there a way to run a function within dialogue like there is to use a variable?
If I want a variable it's as simple as:

Code: Select all

e "This will display a [variable]!"
But trying something similar for a function doesn't work:

Code: Select all

e "It's [a_or_an(unknown_string)] [unknown_string].
Appreciate any help

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: Running a function from within dialogue

#2 Post by enaielei » Mon Sep 05, 2022 1:24 am

Ren'Py's way of interpolation really has its flaws.
You can try a workaround using renpy.say which is a pythonic equivalent of a say statement.

Code: Select all

$ renpy.say(e, "It's {} [unknown_string]".format(a_or_an(unknown_string)))
# or for the latest version of Ren'Py, using  f strings
$ renpy.say(e, f"It's {a_or_an(unknown_string)} [unknown_string]")
Or maybe even using custom text tags.

Code: Select all

init python:
  def ev_tag(tag, argument):
    return [(renpy.TEXT_TEXT, renpy.eval(argument))]

  config.self_closing_custom_text_tags["ev"] = ev_tag

label start:
  e "It's {ev=a_or_an(unknown_string)} [unknown_string]."
This works as long as your a_or_an or other function returns a string.

User avatar
m_from_space
Veteran
Posts: 302
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Running a function from within dialogue

#3 Post by m_from_space » Mon Sep 05, 2022 2:42 am

It's really simple:

Code: Select all

$ w = a_or_an(unknown_string)
e "It's [w] [unknown_string]."
On the other hand your function could return the combination of "a" or "an" and the word.

arti
Newbie
Posts: 7
Joined: Sat Jun 25, 2016 11:34 pm
Contact:

Re: Running a function from within dialogue

#4 Post by arti » Mon Sep 05, 2022 8:17 pm

Thanks enaielei. Something like those solutions looks like what I'm after. I couldn't quite get them to work, but I'm not the best programmer and will keep trying.

And thank you too m_from_space. That's the solution I was going with when I decided to ask here. It does work, but it's kind of clunky and not ideal doing very often. I was worried about how it would work with saves and rollback as well.
You're right about combining the two, I just separated them here to try and give a clearer example of what I was trying to do.

Post Reply

Who is online

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