Generating a formatted table?

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
newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Generating a formatted table?

#1 Post by newbiemate »

How can I generate a table so that the player can see organized data? Something like this:

Code: Select all

name          cost       action
item1          34        [use]  [sell]
item2          34        [use]  [sell]
item3         234        [sell] [drop]
Where the action column has textbuttons, but the rest are just text. Does renpy have a displayable I can use to render this? If not, could someone give an example?

User avatar
Andredron
Miko-Class Veteran
Posts: 700
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Generating a formatted table?

#2 Post by Andredron »

newbiemate wrote: Fri Feb 16, 2018 11:45 am How can I generate a table so that the player can see organized data? Something like this:

Code: Select all

name          cost       action
item1          34        [use]  [sell]
item2          34        [use]  [sell]
item3         234        [sell] [drop]
Where the action column has textbuttons, but the rest are just text. Does renpy have a displayable I can use to render this? If not, could someone give an example?
You create a screen and assign a grid.
And the internal data through the variables (what is changing) or through the text see the training ripping that goes in the launcher

ReDZiX
Regular
Posts: 32
Joined: Thu Jan 04, 2018 12:42 pm
Contact:

Re: Generating a formatted table?

#3 Post by ReDZiX »

You can use vboxes and setup each column, or maybe use vpgrid. Something like this:


Code: Select all

init python:

    class Item():

        def __init__(self, name, price):
            self.name = name 
            self.price = price

    def use_item(i):
        pass #for now, this is a function just for testing purposes
        
default item1 = Item("apple", 10)
default item2 = Item("orange", 5)
default item3 = Item("mango", 15)
default inv = [item1, item2, item3]

screen inventory_table():

    vbox:
        xalign .3
        yalign .5
        label "Name"
        for i in inv:
            text "{}".format(i.name)

    vbox:
        label "Price"
        xalign .5
        yalign .5
        for i in inv:
            text "{}".format(i.price)

    vbox:
        label "Action"
        xalign .7
        yalign .5
        for i in inv:
            textbutton "Use" action Function(use_item(i))
Not the most elegant or optimized way of doing it but hopefully you get the idea.

newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: Generating a formatted table?

#4 Post by newbiemate »

I see, basically just stitching together displayables that form a bunch of rows. Thanks for the hint.

If I'm using vbox though, how do I on hover change the color of all the vboxes on that row?

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Generating a formatted table?

#5 Post by Alex »

Try something like

Code: Select all

screen my_scr():
    $ bg_hover = Solid("#c0c0c0")
    
    side "c r":
        area (100, 100, 550, 200)

        viewport id "vp":
            draggable True

            vbox:
                hbox:
                    button:
                        background Solid("#FF9900cc")
                        xsize 250
                        text "Name"
                        action None
                    button:
                        background Solid("#FF9900cc")
                        xsize 150
                        text "Score"
                        action None
                    button:
                        background Solid("#FF9900cc")
                        xsize 150
                        text "Level"
                        action None
                    
                for i,l in enumerate(my_list):
                    if i%2 == 0:
                        $ bg = Solid("#FADFADcc")
                    else:
                        $ bg = Solid("#FFBF00cc")
                    
                    if l["state"] == 0:
                        $ bg_button = bg
                    else:
                        $ bg_button = bg_hover
                        
                    hbox:
                        button:
                            background bg_button
                            xsize 250
                            text str(l["name"])
                            hovered SetDict(l,"state", 1)
                            unhovered SetDict(l,"state", 0)
                            action [[]]
                        button:
                            background bg_button
                            xsize 150
                            text str(l["score"])
                            hovered SetDict(l,"state", 1)
                            unhovered SetDict(l,"state", 0)
                            action [[]]
                        button:
                            background bg_button
                            xsize 150
                            text str(l["level"])
                            hovered SetDict(l,"state", 1)
                            unhovered SetDict(l,"state", 0)
                            action [[]]
                            
        vbar value YScrollValue("vp")

# The game starts here.
label start:
    $ my_list = [ {"name":"Name_1", "score": 10, "level": 5, "state":0},
                  {"name":"Name_2", "score": 100, "level": 15, "state":0},
                  {"name":"Name_3", "score": 10, "level": 5, "state":0},
                  {"name":"Name_4", "score": 100, "level": 15, "state":0},
                  ]
    "..."
    show screen my_scr
    
    "*"
    $ my_list.append({"name":"Name_5", "score": 999, "level": 99, "state":0})
    
    "?"

Post Reply

Who is online

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