Nested if (or not...)

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
jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Nested if (or not...)

#1 Post by jagharhemma »

Hello,

Trying to avoid nested if statements when presenting random images on entering a new area.

What i have is a number of images related to a specific area:

corridor1
corridor2
corridor3
and so on...

Right now i use nested if statements and a random variable to control what image (and message) that will show up.

But it is so tedious to create the nested ifs...

SO, i wonder how i can use the random variable to populate the number? I haev been trying to find a way to concatenate the two, but with no luck...

Also, is it possible to utilise the same random variable to read of an external text file and a specific row? I would like to tailor the text to the specific image, as you can in the nested ifs

In my mind it is "simple"(i.e i know exactly what i want...) but my coding skills are very limited, unfortunately.

Hope someone can help!

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

Re: Nested if (or not...)

#2 Post by IrinaLazareva »

Perhaps, it will help
https://www.renpy.org/doc/html/displaya ... tionSwitch

Code: Select all

default myset = renpy.random.randint(1,3)
image corridor = ConditionSwitch(
    "myset==1", "corridor1.png",
    "myset==2", "corridor2.png",
    "myset==3", "corridor3.png",
    )

label start:
    scene corridor
    "1"
    $ myset = renpy.random.randint(1,3)
    "2...etc"
    return

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

Re: Nested if (or not...)

#3 Post by IrinaLazareva »

little UPD.

Code: Select all

default myset = renpy.random.randint(1,3)
image corridor = ConditionSwitch(
    "myset==1", "corridor1.png",
    "myset==2", "corridor2.png",
    "myset==3", "corridor3.png",
    
    "myset==4", "road1.png",
    "myset==5", "road2.png",    
    "myset==6", "road3.png",    
    ##etc
    
    )

label start:
    scene corridor
    "in random corridor 1"
    $ myset = renpy.random.randint(1,3)
    "in second random corridor"     
    $ myset = renpy.random.randint(4,6)
    "on random road"
    "etc"
    return

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Nested if (or not...)

#4 Post by jagharhemma »

Thanks but it would be so much easier to just be able to convert the variable to a string and concatenate to a show statement.

$ hit = renpy.random.randint(1,20)

show corridor + str[hit]

I have found some (old) references that "str" could/should convert the random integer (i presume) to a string, not got it to work though...

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Nested if (or not...)

#5 Post by jagharhemma »

Some progress...

hit is: $ hit = renpy.random.randint(1,20)

I can get it to work in text:
"{image=corridor[hit]}"

This will show a random image taken from a set of images where the name conform to the "naming convention" based on the random variable [hit]. But it is in a textbox, so that is not really what i try to achieve...

So, what syntax do i need to use to get it to work in a show statement???

show corridor[hit] (does not work)
show corridor+[hit] (does not work)
show corridor&[hit] (does not work)
And many other permutations is equally unsuccessful....

Grateful for any help! It can't be that hard, or has no one tried something like this before?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Nested if (or not...)

#6 Post by Ocelot »

You can define image as a result of interpolation:

Code: Select all

define random_corridor = DynamicImage("corridor[hit]")
# . . . 
$ hit = renpy.random.randint(1,20)
show random_corridor 
< < insert Rick Cook quote here > >

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Nested if (or not...)

#7 Post by jagharhemma »

Thanks for the support!

But nope, can't get it to work.

I get the "default image" ( the grey outline) so i am not sure what is wrong.

It does state that it is showing "random_corridor", so far so good, but it does not seems to understand the syntax, properly.

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: Nested if (or not...)

#8 Post by Remix »

DynamicImage( "corridor[hit].png" ) ## add .png
Frameworks & Scriptlets:

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Nested if (or not...)

#9 Post by jagharhemma »

Nope, same result, see my code below.

Code: Select all

    "Test"
    $ hit = renpy.random.randint(1,1)
    "hit is [hit]" ## verifying variable
    define random_corridor = DynamicImage( "corridor[hit].png" ) ## add .png
    # . . . 
    # $ hit = renpy.random.randint(1,1)## removed since it initiates the variable too late
    show random_corridor ## grey outline "standard image"
    "----"
    show corridor1 ## verifying image
    "...."
    "{image=corridor[hit]}" ## show image in text, works.
    "End test"

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: Nested if (or not...)

#10 Post by Remix »

the define should be outside of any label, not define or default either, use image

Code: Select all

default hit = 1
image random_corridor = DynamicImage( "corridor[hit].png" ) 
label a:
    scene random_corridor
    "start"
    $ hit = 2
    "end"
Frameworks & Scriptlets:

jagharhemma
Newbie
Posts: 14
Joined: Wed Nov 29, 2017 10:49 am
Contact:

Re: Nested if (or not...)

#11 Post by jagharhemma »

Got it! One thing that confused me, i had to direct to folder... Also, i changed from scene to show

So, the final code looks like (annoyingly easy!)

Code: Select all


$ hit = renpy.random.randint(1,28) ## initiates a random variable

image street1 = DynamicImage( "street/street1/1street [hit].jpg" ) ## "binds" the image to a dynamic image that uses the "hit variable", in this case all images are named 1street and a number.

show street1 ## Shows random image


OK, thanks a lot for the support!!

Post Reply

Who is online

Users browsing this forum: No registered users