Need help implementing conditional compilation

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.
Message
Author
User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help implementing conditional compilation

#16 Post by zmook » Fri Jun 10, 2022 2:09 pm

Ocelot wrote:
Fri Jun 10, 2022 2:01 pm
You miss the part where image name is broken into tag and attributes by known separators. Underscore is one of them. Defined image name would be 'eileen dancing' without underscore.
The game I'm working on contains a file "carla_dori_tmp.webp". In console:

Code: Select all

> [x for x in renpy.list_files() if 'carla' in x]
['carla neutral', 'carla neutral speaking', 'carla_dori_tmp']
The first two are from explicit 'image' statements in my code; the third is the one renpy implicitly created for me. Note that it did *not* split on underscores.

The parsing rules are different for layeredimages, if that's what you're thinking of.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
AVNSnax
Regular
Posts: 35
Joined: Sun Feb 06, 2022 12:11 am
itch: avnsnax
Contact:

Re: Need help implementing conditional compilation

#17 Post by AVNSnax » Fri Jun 10, 2022 2:25 pm

Ocelot wrote:
Fri Jun 10, 2022 1:41 pm
You technically can generate image names at startup:

Code: Select all

init -999 python:
    # you can check existence if single .jpg file exist and set extention as you want.
    # if it is runtime speed problem and not game size problem, you can make a menu choice, which images you want to use
    # and store user choice in persistent variables 
    $ image_ext = "jpg" if (some condition) else "webp"

image bg stars = "backgrounds/fullscreen/chapter1/stars.%s" % image_ext
Nope. Same problem I had before--the conditional variable used to make the selection doesn't appear to be defined yet when the image statement is interpreted. (I assume the stray "$" was a typo...) I even tried initializing it using the same init level:

a00000.rpy:

Code: Select all

init -999 python:
	COMP_IMAGES = False
images.rpy:

Code: Select all

init -999 python:
	image_ext = "jpg" if (COMP_IMAGES) else "webp"

image fam0 = "prologue/Meet the fam/fam0.%s" % image_ext
Always comes back with the jpg extension.

Sigh. I'm back at having to construct the path at runtime via a function like zmook recommended, unless I'm missing something?
Last edited by AVNSnax on Fri Jun 10, 2022 2:42 pm, edited 1 time in total.

User avatar
AVNSnax
Regular
Posts: 35
Joined: Sun Feb 06, 2022 12:11 am
itch: avnsnax
Contact:

Re: Need help implementing conditional compilation

#18 Post by AVNSnax » Fri Jun 10, 2022 2:37 pm

zmook wrote:
Fri Jun 10, 2022 1:53 pm
By "not directly in the images directory", you mean they're in subdirectories of "images", right?
Yes, into chapters and scenes subdirectories. It's been working fine up until now.
zmook wrote:
Fri Jun 10, 2022 1:53 pm
The documented behavior is:
The image directory is named "images", and is placed under the game directory. When a file with the .jpg or .png extension is placed underneath this directory, the extension is stripped, the rest of the filename is forced to lowercase, and the resulting filename is used as the image name if an image with that name has not been previously defined.
(This specifies jpg or png only, but in my experience it does do webp also.) To make that explicit, my understanding is that if you have a file "game/images/ch1/eileen/eileen_dancing.jpg", ren'py will implicitly execute this statement for you at init time:

Code: Select all

image eileen_dancing = "game/images/ch1/eileen/eileen_dancing.jpg"
so then the name "eileen_dancing" exists as an image name and you should be able to use it straight up as

Code: Select all

show eileen_dancing
If this isn't working, what error do you get exactly?
The images show as expected, but only as explicitly specified in the image statement. If there's an extension, it only looks for that extension. If I leave the extension off, it searches for the file exactly as specified i.e. without the extension.

Code: Select all

image test 1 = "chapter1/scene1/test 1.jpg"
...
show test 1
only looks for the .jpg file, and doesn't find the .webp variant.

Code: Select all

image test 1 = "chapter1/scene1/test 1"
...
show test 1
only looks for test 1 and with no extension at all. .jpg and .webp are ignored.

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

Re: Need help implementing conditional compilation

#19 Post by Ocelot » Fri Jun 10, 2022 2:53 pm

It works for me:
https://i.imgur.com/DZdxPis.png

Image changes when I change WEBP from true to False.
I can suggest to not rely on order of evaluation (because IIRC that was set in stone relatively recently, before that order of execution between files was not defined) and set priorities in order you want. In your case you can even set image_ext in the same init statement as COMP_IMAGES at the next line.
< < insert Rick Cook quote here > >

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help implementing conditional compilation

#20 Post by zmook » Fri Jun 10, 2022 2:56 pm

AVNSnax wrote:
Fri Jun 10, 2022 2:25 pm
I even tried initializing it using the same init level:

a00000.rpy:

Code: Select all

init -999 python:
	COMP_IMAGES = False
images.rpy:

Code: Select all

init -999 python:
	image_ext = "jpg" if (COMP_IMAGES) else "webp"

image fam0 = "prologue/Meet the fam/fam0.%s" % image_ext
My understanding is that if two blocks have the same init priority, whichever renpy encounters first will be executed first. Probably that's the one in the alphabetically-first file, but let's play it safe:

Code: Select all

init -999 python:
	COMP_IMAGES = False

Code: Select all

init -998 python:
	image_ext = "jpg" if (COMP_IMAGES) else "webp"

image fam0 = "prologue/Meet the fam/fam0.%s" % image_ext
or better yet, just to make absolutely sure,

Code: Select all

init -999:
	$ COMP_IMAGES = False
	$ image_ext = "jpg" if (COMP_IMAGES) else "webp"

	image fam0 = "prologue/Meet the fam/fam0.%s" % image_ext
I'd also like to be sure what happens if you delete all of that. Make sure that "prologue/Meet the fam/fam0.jpg" and/or "prologue/Meet the fam/fam0.webp" exist in your game, and make sure that you do *not* have any statement like "image fam0" anywhere in your code.

At the top of your script, add:

Code: Select all

label start:
	$ print ([x for x in renpy.list_images() if 'fam0' in x])
	show fam0
	pause
If that doesn't show the image, open console and let us know what it printed and what error you got. It really should work.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
AVNSnax
Regular
Posts: 35
Joined: Sun Feb 06, 2022 12:11 am
itch: avnsnax
Contact:

Re: Need help implementing conditional compilation

#21 Post by AVNSnax » Fri Jun 10, 2022 8:29 pm

zmook wrote:
Fri Jun 10, 2022 2:56 pm
If that doesn't show the image, open console and let us know what it printed and what error you got. It really should work.
Well, it took awhile because Ren'Py decided to take a dump on me (don't ever let there be more than one directory associated with your app in %APPDATA%\RenPy...).

So, I got tired of messing around with my original project (I thought it was going to be a quick fix) and just generated a new project. Sure enough, your method works on files directly in the images folder.

Subdirectories in that folder, not so much.

So I went from:

Code: Select all

init -999 python:
    COMP_IMAGES = True
init -998 python:
    image_ext = "jpg" if (COMP_IMAGES) else "webp"
image youngAlex 1 =  "youngAlex1.%s" % image_ext
image youngAlex 2 =  "youngAlex2.%s" % image_ext
image youngAlex 3 =  "youngAlex3.%s" % image_ext
image youngAlex 4 =  "youngAlex4.%s" % image_ext
image youngAlex 5 =  "youngAlex5.%s" % image_ext
which worked, to:

Code: Select all

init -999 python:
    COMP_IMAGES = True
init -998 python:
    image_ext = "jpg" if (COMP_IMAGES) else "webp"
image youngAlex 1 =  "prologue/youngAlex/youngAlex1.%s" % image_ext
image youngAlex 2 =  "prologue/youngAlex/youngAlex2.%s" % image_ext
image youngAlex 3 =  "prologue/youngAlex/youngAlex3.%s" % image_ext
image youngAlex 4 =  "prologue/youngAlex/youngAlex4.%s" % image_ext
image youngAlex 5 =  "prologue/youngAlex/youngAlex5.%s" % image_ext
when I moved the files to subdirectories, which didn't. On a lark I removed the path prologue/youngAlex/ just to see what it would do and it still errors out.

So that's the bottom line. I don't want to have to keep every image file in one directory, so I guess I'm going the programmatic route.

EDIT: There must be some sort of caching weirdness going on, because I went back to the second version and this time it ran fine. I went back and forth setting COMP_IMAGES to True and False and it did the right thing. Hmmmmmmm......

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help implementing conditional compilation

#22 Post by zmook » Fri Jun 10, 2022 9:16 pm

AVNSnax wrote:
Fri Jun 10, 2022 8:29 pm
EDIT: There must be some sort of caching weirdness going on, because I went back to the second version and this time it ran fine. I went back and forth setting COMP_IMAGES to True and False and it did the right thing. Hmmmmmmm......
I have found that auto-reload is not 100% reliable at regenerating images when I change their declarations or move files around. Sometimes it's worth quitting and relaunching the project.

Try again with the images in the subdirectories, with a fresh restart of the project. It ought to work. And it ought to work with no explicit "image youngAlex" declarations, either (though in this case Renpy will declare them as "youngalex1" instead of "youngAlex 1". ("The extension is stripped, the rest of the filename is forced to lowercase, and the resulting filename is used as the image name if an image with that name has not been previously defined.")
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: Google [Bot]