Questions regarding Coding

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
Eiliya
Regular
Posts: 148
Joined: Tue Dec 04, 2012 6:21 am
Contact:

Questions regarding Coding

#1 Post by Eiliya »

Code: Select all

label calculation:
    $ result = renpy.random.randint(1,50)
    if result > 40:
        if super == 2:     
            pass
        else:                  
            $ result = 40
    if result > 32:
        if super > 0:       
            pass
        else:                  
            $ result = 32
    show text "The result is [result]!" at truecenter
    pause
    hide text with dissolve
So, in this code, we start by generating a random number between 1 and 500. If the result is over 40, we check if the player has two levels of super human. If they do, the result remains unchanged. If not, the result is reduced to 40. After this, we check if the result is over 32. If so, we check if the player has at least one level of super human. If so, the result remains unchanged but if not, then the result is limited to 32. Lastly, we show the final result to the player and then fade it out when the player clicks the mouse button. Now, as most people can probably tell, this code is not really ideal. So I've made a different one that looks like this:

Code: Select all

label calculation:
    $ result = renpy.random.randint(1,50)
    if result > 40 and super < 2:
        $ result = 40
    if result > 32 and super < 1:
        $ result = 32
    show text "The result is [result]!" at truecenter
    pause
    hide text with dissolve
In my testings, these codes work as I intended them to do, but I'm curious if there are any hidden differences that I've been unable to discover. Could someone kindly explain to me if there are any differences (except for the amount of code used) between these two labels and what those differences are, if so?

As I mentioned up above, the player can get a "result" on certain actions they take, and that result spans from 1 to 50. However, unless the player has attained super-human status, they are unable to attain results above 32 and unless their super-human level is 2, they cannot get results above 40. This is the system I am looking for.

User avatar
Nylan
Regular
Posts: 47
Joined: Tue Mar 08, 2016 4:11 am
Completed: Segmentation_Fault (Nano16)
Projects: Currently free
Organization: MagicEngineering
Contact:

Re: Questions regarding Coding

#2 Post by Nylan »

It's better form to use elif and else:

Code: Select all

if result > 40 and super < 2:
    $ result = 40
elif result > 32 and super < 1:
    $ result = 32
else:
but I won't supply an "else" clause for you. If I were coding it, I would do it like so:

Code: Select all

label calculation:
    if super < 1:
        $ result = renpy.random.randint(1,32)
    elif super < 2:
        $ result = renpy.random.randint(1,40)
    else:
        $ result = renpy.random.randint(1,50)
    show text "The result is [result]!" at truecenter
    pause
    hide text with dissolve
The order is important in this one. Also, you only assign $result a value once. In your second example, $result might get assigned a value 3x before the "show text" statement (think about what happens if $result=50 and $super=0)
Image

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: Questions regarding Coding

#3 Post by Onishion »

Yeah, I mean all the above work. The reason you would use one over the other is if you want more things to happen. Since you want only those things to happen, the simplest approach is the best, but if you wanted, say, something else entirely to happen when you originally have "pass" in there, then you'd have to do it the first way, right? And yeah, using the if/elif/else chains is more efficient, because it'll cause it to skip everything else once it find the right answer. Setting up flow efficiently is really fun, it's like a puzzle game.

Now Nylan:
but I won't supply an "else" clause for you. If I were coding it, I would do it like so:
I'm curious on this one, and I honestly don't know the answer so I'm hoping you do, but which takes more processing, the "running random int once and then changing the variable a few times," or the "running random int three times?" I'd just assumed that processes like that were higher overhead than changing variables.

User avatar
Nylan
Regular
Posts: 47
Joined: Tue Mar 08, 2016 4:11 am
Completed: Segmentation_Fault (Nano16)
Projects: Currently free
Organization: MagicEngineering
Contact:

Re: Questions regarding Coding

#4 Post by Nylan »

Onishion wrote:Yeah, I mean all the above work. The reason you would use one over the other is if you want more things to happen. Since you want only those things to happen, the simplest approach is the best, but if you wanted, say, something else entirely to happen when you originally have "pass" in there, then you'd have to do it the first way, right? And yeah, using the if/elif/else chains is more efficient, because it'll cause it to skip everything else once it find the right answer. Setting up flow efficiently is really fun, it's like a puzzle game.

Now Nylan:
but I won't supply an "else" clause for you. If I were coding it, I would do it like so:
I'm curious on this one, and I honestly don't know the answer so I'm hoping you do, but which takes more processing, the "running random int once and then changing the variable a few times," or the "running random int three times?" I'd just assumed that processes like that were higher overhead than changing variables.
Random most definitely incurs more overhead than simple string/int assignment. It has to generate the int and assign it to the variable. I'd also wager that random x3 without saving the value is more overhead than random x1 + assignment x3, given that random calls a module and usually has to interact with the hardware in some way, but I'd be lying if I said I knew the details.

As you state above, the (completely unnecessary in this use case) ideal is to not execute code you don't need to, which is why I tried to set it up in such a way that you'd only assign $result once and random would only be called once.

Depending on the language, sometimes the compiler figures that out for you.
Image

User avatar
Nylan
Regular
Posts: 47
Joined: Tue Mar 08, 2016 4:11 am
Completed: Segmentation_Fault (Nano16)
Projects: Currently free
Organization: MagicEngineering
Contact:

Re: Questions regarding Coding

#5 Post by Nylan »

(If you don't care about random number generation, skip to the end)

So I looked it up, and Python uses a variant of the Mersenne Twister using hardware drivers, so my guess was correct. Random takes quite a bit more effort than x=1.


Eiliya, did we provide a good enough answer for you? The code is functionally the same, but some versions are less efficient than others. It probably isn't that noticeable in a Ren'py VN, but arranging things in a way to not run code you don't need to will make you a better coder, if that's what you're after. You've already demonstrated good healthy curiosity just by posting this sort of question. :D
Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Questions regarding Coding

#6 Post by xela »

Nylan wrote:The code is functionally the same
Your second code version does mess up the original intent (if there was such a thing) a little bit, putting anything "super" < 2 at disadvantage when compared to the OP's design. It's not humanly possible to notice the speed difference in a context of simple VN statements, so it's code readability only that matters here.
Like what we're doing? Support us at:
Image

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: Questions regarding Coding

#7 Post by Onishion »

As you state above, the (completely unnecessary in this use case) ideal is to not execute code you don't need to, which is why I tried to set it up in such a way that you'd only assign $result once and random would only be called once.
You're right, I think I had a mental blindspot in this where when you had three randints down I was thinking all three would get considered, but of course only one would get activated at a time, so as long as the number is only accessed once, doing it that way would be fine.

Eiliya
Regular
Posts: 148
Joined: Tue Dec 04, 2012 6:21 am
Contact:

Re: Questions regarding Coding

#8 Post by Eiliya »

Thank you for all the feedback on this one, everyone. I actually have some questions related to the if/elif/else functions, but seing as they are a different topic from this one, I will type that in a different thread.

Thank you for your time.

Post Reply

Who is online

Users browsing this forum: voluorem