Return displayables from Python 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
RealSwede
Newbie
Posts: 1
Joined: Tue Jul 20, 2021 2:50 pm
Contact:

Return displayables from Python function

#1 Post by RealSwede »

I'm working on a Replay event list and want to make it get its information from a list, rather than hard coding each event into the list.
That is easy and I've got it sorted, however I've got these lists in multiple different screens and so my code is duplicated several time at the moment.

I'd like to just call a python function that returns a vbox list given a tag, sort of like:

Code: Select all

def generateEventList(tag)
which could then be called at each event list with the tag for that list.

Currently my code for the function is:

Code: Select all

def generateEventList(eventTag):
	# eventList is a global list with each event in the format
	# { "trigger": "", "title": "", "tag": "", "replay": "", "color": ""}
        for item in eventList:
            if item["tag"] == eventTag:
                if globals()[item["trigger"]] == True:
                    return textbutton str("{s}"+item["title"].replace("!player_name!", player_name)+"{/s}"):
                        text_style "replaybutton"
                        action Replay(item["replay"],scope={"player_name": player_name}, locked=False )
                else:
                    return text _(str(str("{color=#eee}" if item["color"] == "" else item["color"]) + "{size=-5}"+item["title"].replace("!player_name!", player_name)+"{/size}{/color}"))
However, when I run the game, this error is thrown

Code: Select all

File "game/events.rpy", line 60: invalid syntax
                    return textbutton str("{s}"+item["title"].replace("!player_name!", player_name)+"{/s}"):
Any thoughts on how I can get this to work?

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Return displayables from Python function

#2 Post by hell_oh_world »

That wont work since that is a renpy specific syntax (screen language). When you're inside a python block, you code as you do in python, so python syntax only.
What you need are objects that you can instantiate and return.
https://www.renpy.org/doc/html/displayables.html

Post Reply

Who is online

Users browsing this forum: No registered users