about random + other punctual doubts

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
herenvardo
Veteran
Posts: 359
Joined: Sat Feb 25, 2006 11:09 am
Location: Sant Cugat del Vallès (Barcelona, Spain)
Contact:

about random + other punctual doubts

#1 Post by herenvardo »

I've tried to search it on the reference, but all it tells about the random object is that it exists and a link to Python's random number generator's reference. I've taken a look on the reference, but don't feel very confident about what I've figured out from there. So, if someone can tell me if those lines do what is expected, I'll thank that a lot:
First case:

Code: Select all

renpy.random.random()
Will this return a float between 0 and 1? If so, I can do all I need from here :P

Code: Select all

renpy.random.randint(1, 100)
Will this roll a % die? (This is, generating a number between 1-100)
With this I'll have all I need about random... at least for now.
In addition, I've some unrelated questions, that I'll post here to save space:
Is there any way in Python to convert data-types? I tried to concatenate a string with a integer, and the engine crashed at my face (if it were more than a sequence of binary instructions running on my CPU, I'd say that it was angry :? ); so I guess the best way to do what I tried would be to convert the integer into a string, and then concatenate both strings.
Also, has python something like C/C++'s & php's sprintf()? This is, a function that formats a string, like in Python's print statement, but returning the resulting text instead of sending it to the screen. With that, I wouldn't need neither conversion nor concatenation :P I'd want it to get better presentation for the character sheet when it's shown to the user, like enconding the positively modified stats (attack improved by a special weapon, for example) in one color and negatively modified ones in another, and that kind of freaky features ^^
Thanks for all help in advance

PD: hmm... I bet Py'tom will be the first one to answer... will somebody be hastier? 8)

PPD: Another question, about Python:
I have a list of objects instanced from the class Monster :? If I run the sort() method on it, which sorting criteria will be applied? I want it be sorted by a data member of the class, which is an integer named speed.
I have failed to meet my deadlines so many times I'm not announcing my projects anymore. Whatever I'm working on, it'll be released when it is ready :P

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: about random + other punctual doubts

#2 Post by PyTom »

herenvardo wrote: First case:

Code: Select all

renpy.random.random()
Will this return a float between 0 and 1? If so, I can do all I need from here :P

Code: Select all

renpy.random.randint(1, 100)
Will this roll a % die?
Your understanding of both functions is correct.
Is there any way in Python to convert data-types? I tried to concatenate a string with a integer, and the engine crashed at my face (if it were more than a sequence of binary instructions running on my CPU, I'd say that it was angry :? ); so I guess the best way to do what I tried would be to convert the integer into a string, and then concatenate both strings.
Python is a typed language, and tends not to do type conversions for you. You use the int() function to convert to int, and the str(function) to convert to strings.

So:

int("100") == 100
str(100) == "100"
Also, has python something like C/C++'s & php's sprintf()? This is, a function that formats a string, like in Python's print statement, but returning the resulting text instead of sending it to the screen.
Yes, the % operator. See

http://docs.python.org/lib/typesseq-strings.html

for details, but basically one can do something like:

"%s[%d] = %d" % ("age", 1, 42)

And it will do the right thing ala sprintf.
PPD: Another question, about Python:
I have a list of objects instanced from the class Monster :? If I run the sort() method on it, which sorting criteria will be applied? I want it be sorted by a data member of the class, which is an integer named speed.
By default, cmp uses the natural ordering of objects, which is at the same time well-defined (by object id) and meaningless. To fix this, one can either do a schwartzian transform (look it up), or supply a comparison function. The latter is easier, so I'll show how to do it here.

list_of_monsters.sort(cmpfunc=lambda a, b : cmp(a.speed, b.speed))
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

herenvardo
Veteran
Posts: 359
Joined: Sat Feb 25, 2006 11:09 am
Location: Sant Cugat del Vallès (Barcelona, Spain)
Contact:

#3 Post by herenvardo »

PyTom wrote:To fix this, one can either do a schwartzian transform (look it up),
I looked it up and found it interesting... but deathclock, and my lack of experience in Python & Ren'Py, are forcing me to go 'the easy way' :lol:
About the code you gave,
PyTom wrote:list_of_monsters.sort(cmpfunc=lambda a, b : cmp(a.speed, b.speed))
well... it almost worked... it crashed complaining something about the named argument, so I removed the 'compfunc=' and it went fine :P
Anyhow, at least I didn't need to add a weird loop to do the sorting manually, which was what I wanted to avoid.
herenvardo wrote:PD: hmm... I bet Py'tom will be the first one to answer... will somebody be hastier?
:lol: it seems that I was right, which makes me think in another question: how do you do to have time to solve almost everyone's doubts and also keep improving your software? Have you discovered the way to get 30hours days, or something like that? :lol: IMHO, the atention you give to Ren'Py users must be beating records.
I have failed to meet my deadlines so many times I'm not announcing my projects anymore. Whatever I'm working on, it'll be released when it is ready :P

shaja
Regular
Posts: 73
Joined: Sun Oct 16, 2005 8:34 am
Contact:

#4 Post by shaja »

herenvardo wrote:
PD: hmm... I bet Py'tom will be the first one to answer... will somebody be hastier?
:lol: it seems that I was right, which makes me think in another question: how do you do to have time to solve almost everyone's doubts and also keep improving your software? Have you discovered the way to get 30hours days, or something like that? :lol: IMHO, the atention you give to Ren'Py users must be beating records.
Strangely enough, I wrote a reply to your questions the other day, but knowing PyTom, I refreshed before submitting and saw that he had already answered in more detail. ;)

Another way of doing the sort, assuming you always want your monster items to be sorted by speed, is to add a __cmp__ method to your monster class, like so:

Code: Select all

def __cmp__(self, other):
    return cmp(self.speed, other.speed)
You would then do

Code: Select all

list_of_monsters.sort()
without having to specify the sort every time.

See also the Sorting Howto in the python.org wiki.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#5 Post by PyTom »

herenvardo wrote:
PyTom wrote:list_of_monsters.sort(cmpfunc=lambda a, b : cmp(a.speed, b.speed))
well... it almost worked... it crashed complaining something about the named argument, so I removed the 'compfunc=' and it went fine :P
Please note that the name I gave was cmpfunc, rather than compfunc. Close only counts in horseshoes and hand grenades.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

herenvardo
Veteran
Posts: 359
Joined: Sat Feb 25, 2006 11:09 am
Location: Sant Cugat del Vallès (Barcelona, Spain)
Contact:

#6 Post by herenvardo »

PyTom wrote:
herenvardo wrote:
PyTom wrote:list_of_monsters.sort(cmpfunc=lambda a, b : cmp(a.speed, b.speed))
well... it almost worked... it crashed complaining something about the named argument, so I removed the 'compfunc=' and it went fine :P
Please note that the name I gave was cmpfunc, rather than compfunc. Close only counts in horseshoes and hand grenades.
Even I misspelled it when I posted that, I had done copy-paste into the script, and then changed list_of_monsters by the actual name of the list. So, the problem is not about my misspelling. It was that:
traceback.txt wrote:TypeError: sort() takes no keyword arguments
I perfectly know that my misspellings usually make my programs crash, but it's for that reason that I copy-paste so much :P
I have failed to meet my deadlines so many times I'm not announcing my projects anymore. Whatever I'm working on, it'll be released when it is ready :P

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#7 Post by PyTom »

Well, I actually tried it, and of course H4 is right. That's what I get for reading the documentation rather than actually trying it. My apologies for that one.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Sugar_and_rice