What's wrong with my python loop?

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
OrsonDeWitt
Regular
Posts: 45
Joined: Thu Dec 06, 2018 3:23 am
Contact:

What's wrong with my python loop?

#1 Post by OrsonDeWitt »

So I have a variable X
in screen say(who, what) I have an hbox with the following:

Code: Select all

python:
      while X > 100:
            X = 100
However, when I go through the script and subtract 3, the value goes correctly down to 97. I then add 5, and it shows correctly as 100. However, when I subtract 3 again, it does not go down to 97 as previously but goes only to 99.
This is a very strange behavior and I've been scratching my head for some time now. Could someone more knowledgeable chime in and help me out? Thanks! :)
I cannot reply to messages anymore.
> Your IP has been blocked because it is blacklisted.

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

Re: What's wrong with my python loop?

#2 Post by hell_oh_world »

i think doing python statements inside screens are quite unstable, not really sure on why, but I observed that most of the times. so I tend to avoid doing any python statements if I can, such as setting a variable if condition is met, etc.
you can try to prove that by doing a print statement inside the screen... and after the screen shows up try hitting shift + o to see the console to see the printed message.

Code: Select all

screen sample:
  $ print("this should be printed only once right?")
I bet that the code above will make the statement be printed more than once and not only once in the console.
and that's what I mean by unstable.
also note that you don't need to use while loop at all, you can use normal for loop inside screens. but yeah, for while loops you really need a python: statement for that.

Code: Select all

screen sample:
  vbox:
    for i in range(100):
      text "text-{}".format(i + 1)

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

Re: What's wrong with my python loop?

#3 Post by Alex »

OrsonDeWitt wrote: Wed Jun 17, 2020 5:13 am So I have a variable X
in screen say(who, what) I have an hbox with the following:

Code: Select all

python:
      while X > 100:
            X = 100
...
What's the goal and how the actual code of say screen looks like?
This might be that you need to re-code this part.

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

Re: What's wrong with my python loop?

#4 Post by strayerror »

Depending on what you're doing, it might be a better idea to use a callback. Without more context however, only you can decide if it's appropriate or not - I'm guessing your real code is more complex.

The code below will run the statement adjusting the X value each time a say occurs, but outside of the screen. Hope it helps!

Code: Select all

def say_arguments_callback(who, **kwargs):
    X = min(X, 100) # caps X at 100
    return (), kwargs

config.say_arguments_callback = say_arguments_callback

OrsonDeWitt
Regular
Posts: 45
Joined: Thu Dec 06, 2018 3:23 am
Contact:

Re: What's wrong with my python loop?

#5 Post by OrsonDeWitt »

I was trying to invent a horse. What I needed was this: viewtopic.php?t=29339
Guess I had better searched for it. Thanks to everyone who replied!
strayerror wrote: Wed Jun 17, 2020 1:53 pm The code below will run the statement adjusting the X value each time a say occurs, but outside of the screen. Hope it helps!
Thanks! This is like the code from the url, and it works.
I still don't understand though what's wrong with that python code. I mean, thanks to hell_oh_world I understand that it's not a good idea to write loops in screens, but I still don't understand why and what is the difference between writing it in a screen and writing it as "init". Shouldn't it be exactly the same?
I cannot reply to messages anymore.
> Your IP has been blocked because it is blacklisted.

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: What's wrong with my python loop?

#6 Post by trooper6 »

For loops are totally fine in screens and used all the time...I'm just not certain about while loops considering screens refresh regularly.
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

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]