[solved] can somebody test this code and tell me the result please

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
User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

[solved] can somebody test this code and tell me the result please

#1 Post by Kia »

I tried this simple code expecting 1,2,3:

Code: Select all

default dutst = {
        "1": 1,
        "2": 2,
        "3": 3,
    }
screen dictest:
    vbox:
        for i in dutst:
            text i
but it gives me 1,3,2
can somebody test it and make sure it's not just my computer doing it?
Last edited by Kia on Mon Sep 02, 2019 6:10 am, edited 1 time in total.

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: can somebody test this code and tell me the result please

#2 Post by drKlauz »

Python uses hashtables for dicts, while integers may appear in 1-2-3 order, strings and pretty much any other thing are in random order.
Use dutst_order list ["1","2","3"], iterate over it then read actual value from dict.
Or try OrderedDict, tho not sure how good it behave with RenPy save/rollback systems.
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: can somebody test this code and tell me the result please

#3 Post by isobellesophia »

I dont think it is related to this one...

https://www.renpy.org/doc/html/language ... ion-syntax
I am a friendly user, please respect and have a good day.


Image

Image


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

Re: can somebody test this code and tell me the result please

#4 Post by hell_oh_world »

Kia wrote: Sun Sep 01, 2019 11:51 am I tried this simple code expecting 1,2,3:

Code: Select all

default dutst = {
        "1": 1,
        "2": 2,
        "3": 3,
    }
screen dictest:
    vbox:
        for i in dutst:
            text i
but it gives me 1,3,2
can somebody test it and make sure it's not just my computer doing it?
Quite not sure what you're trying to iterate over. If its the keys then you could do it like this.

Code: Select all


for i in dutst.keys(): # use keys() method to iterate over the keys, or if you're iterating over the dictionary's values use values() method instead.
   text i

I think the dictionary should appear the way it was declared. So it should display 1, 2, 3. But I'm not quite sure though if python sorts the dictionary afterhand. Because if it will be sorted and the basis of sorting is a string then it will be based on ascii characters rather than on numerical sorting. So weird things might happen. I'm not quite sure though.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: can somebody test this code and tell me the result please

#5 Post by Kia »

I'm pretty sure it's not python, the same code in python returns 1, 2, 3
it seems that something happens while storing a dictionary in renpy

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: can somebody test this code and tell me the result please

#6 Post by IrinaLazareva »

Hmm, It's even more strange if the numbers are replaced with letters... (I've also tested in old versions, the same result.)

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

Re: can somebody test this code and tell me the result please

#7 Post by Alex »

Kia wrote: Sun Sep 01, 2019 3:45 pm I'm pretty sure it's not python, the same code in python returns 1, 2, 3
it seems that something happens while storing a dictionary in renpy
My two cents:
- it's not obvious what you want to show, 'cause dict has a pairs of key-value;
- where did you test this code that gave you 1,2,3? In docs for python 2.6 it said 'It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).' - https://docs.python.org/2.6/tutorial/da ... ctionaries

So, try

Code: Select all

default dutst = {
        "1": 1,
        "2": 2,
        "3": 3,
    }
screen dictest():
    hbox:
        align (0.5, 0.1)
        spacing 50
        vbox:
            for i in sorted(dutst.keys()):
                text "\'{}\' - {}".format(i, dutst[i])
            
        vbox:
            for i in dutst.keys():
                text "\'{}\' - {}".format(i, dutst[i])
            
            

# The game starts here.

label start:
    "..."
    show screen dictest
    "...  ..."
    "..."
    "?!" 

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: can somebody test this code and tell me the result please

#8 Post by trooper6 »

Dicts are unordered in Python, so those parts can come in any order.
If you want ordered, use an ordered dict.
https://www.geeksforgeeks.org/ordereddict-in-python/
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: can somebody test this code and tell me the result please

#9 Post by Kia »

I see, I tested it with python 3.
wasn't there something about renpy migrating to python 3 at version 7.0?

now the question is: is there any way to get the original order without adding an index number to re order with?

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: can somebody test this code and tell me the result please

#10 Post by trooper6 »

Did you follow the link I provided? All you have to do have your data ordered is to used an ordered dict rather than a dict.
Also I have no idea when/if Renpy will migrate to Python 3.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: can somebody test this code and tell me the result please

#11 Post by drKlauz »

They changed dicts to be sorted-by-insertion-order in Python 3.7 i think, maybe you tested it there. RenPy uses 2.7 and frankly i don't think PyTom will upgrade Python version any soon as it will require to drop legacy support for some stuff.
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: can somebody test this code and tell me the result please

#12 Post by Kia »

trooper6 wrote: Sun Sep 01, 2019 7:55 pm Did you follow the link I provided? All you have to do have your data ordered is to used an ordered dict rather than a dict.
I did, not as pretty as I like the code to be, requires a python block, importing something and creating an extra variable to pass it to a class. it does work though.
but if that's what it takes, I prefer using lists and putting the key in it as another value instead, avoiding the dictionaries all together. makes for a less messy code.
edit: I forgot to thank you all for your input on the topic. Thank you :wink:

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: [solved] can somebody test this code and tell me the result please

#13 Post by strayerror »

Just a small addition to note that for the test case a dict isn't a good choice of structure; neither the unique key nor lookup properties of a dict at being used, yet there is a desire for the data to remain ordered, if that's the extent of it then as someone already noted a simple list would solve your problem (especially since only the keys are being iterated over).

One solution that may fit this bill but I don't see mentioned is that of a tuple of tuples, which would fit some use-cases without multiple lists and lookups to retain ordering, I don't know if that will apply here, but here's an example:

Code: Select all

default data = (('1', 1, 'foo'),
                ('2', 2, 'bar'),
                ('3', 3, 'qux'))

screen test():
    vbox:
        spacing 10
        for a, b, c in data:
            hbox:
                spacing 10
                text a
                text '[b]' # interpolate to convert int
                text c
This allows use of as many or as few values per "row" as needed. The tuples could also be substituted for lists if the structure needs to be mutable.

Post Reply

Who is online

Users browsing this forum: No registered users