Proper way of showing table/set/tuple/etc.

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
CobraPL
Newbie
Posts: 17
Joined: Wed Nov 15, 2017 9:16 am
Contact:

Proper way of showing table/set/tuple/etc.

#1 Post by CobraPL » Fri Aug 24, 2018 7:08 am

http://thomas-cokelaer.info/tutorials/p ... tures.html

So, Python stores string value in unicode form like: u'stringhere'
All elements appended to list are in such form.
When I try to print table content like this "[listname[0]]" everything is ok. EDIT: unless out of range...
But when I try to print whole list like this "[listname]" i have all elements with damn u'' included.
On top of that SET data structure can't (reliably) be IMHO printed like "[setname[0]]"

So, my question are:
1. How to print ONE (specific) element of the given data structure during dialog.
2. How to print ALL elements of the given data structure during dialog.
3. How to print such element(s) outside the dialog. Just on the screen at given position.

philat
Eileen-Class Veteran
Posts: 1853
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Proper way of showing table/set/tuple/etc.

#2 Post by philat » Fri Aug 24, 2018 7:40 am

1. set doesn't have indexes. https://docs.python.org/2/library/sets.html Don't use sets if you need to access via index.
2. Use .join() https://www.tutorialspoint.com/python/string_join.htm
3. https://www.renpy.org/doc/html/screens.html#text

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Proper way of showing table/set/tuple/etc.

#3 Post by Remix » Fri Aug 24, 2018 8:34 am

1) If the datatype is a list or set, then "[listname[integer-index]]" should work fine
or "dict_name[key_name]]" in the case of dictionaries

2) Some datatypes support method patching or overloading, otherwise just pre-interpret to a variable

Code: Select all

default my_list = ['a', 'b', 'c']
default my_set = ('a', 'b', 'c')
default my_dict = { 'a':'a val', 'b':'b val', 'c': 'c val' }
init python:
    def get_string_for_list(list_obj):
        renpy.notify('call')
        """ Take list [a,b,c] return 'a, b and c' """
        if not isinstance(list_obj, (list, tuple, set)):
            raise TypeError, "Input parameter must be list, set or tuple"
        return "".join( [ 
            "{0}{1}".format( 
                v, 
                { len(list_obj)-1 : '', 
                  len(list_obj)-2 : ' and ' }.get( i, ', ' ) 
            ) for i,v in enumerate(list_obj) ] 
        )

    class SimpleObj(object):
        def __init__(self, *args, **kwargs):
            self.__dict__.update(kwargs)

        def __repr__(self):
            return ", ".join( [
                getattr(self, k) 
                for k in self.__dict__
                if k not in object.__dict__ ] )

default my_obj = SimpleObj( a = 'a val', b = 'b val', c = 'c val')


label start:

    "Lists"
    "[my_list[1]] should be b"
    $ list_vals = ", ".join( [ str(k) for k in my_list ] )
    "[list_vals] using variable should be a, b, c"
    $ my_list.as_str = get_string_for_list(my_list)
    "[my_list.as_str] using method should be a, b and c"
    "I tried overloading __repr__ and it ran, just still used other repr"

    "Sets"
    "[my_set[1]] should be b"
    $ set_vals = ", ".join( [ str(k) for k in my_set ] )
    "[set_vals] using variable should be a, b, c"
    "Cannot patch method to set or tuple in same way as with list"

    "Dicts"
    "[my_dict[a]] should be a val"
    $ dict_vals = ", ".join( [ str(v) for v in my_dict.values() ] )
    "[dict_vals]"
    "Should be able to patch in a method if wanted"

    "Objects"
    "[my_obj.a] should be a val"
    "[my_obj] using repr"
    "Patching in methods would generally be the way to go here"
3) Check the docs for Screen Language or just --- show expression Text(" the text ", pos=(100,100) ) --- maybe
Frameworks & Scriptlets:

CobraPL
Newbie
Posts: 17
Joined: Wed Nov 15, 2017 9:16 am
Contact:

Re: Proper way of showing table/set/tuple/etc.

#4 Post by CobraPL » Sun Dec 02, 2018 12:16 pm

Thanks to Panda Penguin Games and error229 I used more understandable and working solution for my initial problem:

Code: Select all

init python:
    class NPC:
        def __init__(self):
            self.friends=[]
        @property
        def get_friends(self):
            output = ""
            for s in self.friends:
                output += str(s) + "\n"
            return output[:-2]

Code: Select all

default npcnamehere = NPC() #before label start
$ npcnamehere.friends.append(hernewfriend)#after label start

Code: Select all

text "[npcnamehere.get_friends]"

Post Reply

Who is online

Users browsing this forum: Bing [Bot]