Page 1 of 1

[SOLVED] Problems with displaying variable images

Posted: Mon Oct 24, 2022 1:19 pm
by Team1Player2
Hello,

I am trying to show scene images based on the current location in the game and got some issues. I found a post on these forums dealing with this but am unable to implement it correctly.

I have a dictionary with possible places:

Code: Select all

default current_place = {
    "room_1":False,
    "room_2":False,
}
And I have enter_place() function:

Code: Select all

# enter place

    def enter_place(place):
        global bg_code
        for x in current_place: # safeguard, leave all and any current places
            current_place[x] = False
        current_place[place] = True # switch to True for chosen location
        bg_code = current_place.index(place)
bg_code is my attempt for variable images. In the init python block are also:

Code: Select all

    room_1 = "bg_room_1.png",
    room_2 = "bg_room_2.png",
    
    places_list = [
    room_1,
    room_2,
    ]
What I am trying to achieve is:

Code: Select all

$ enter_place("room_1")
scene places_list[bg_code] # display correct picture
This (I hope I put here all the relevant code) results in a grey screen with "places_list X" on top where X is the number of bg image. In the example above it would be 0.

It feels like I am almost there but fail to pair the bg_code with choosing the correct image. Thank you for your time.

Re: Problems with displaying variable images

Posted: Mon Oct 24, 2022 2:34 pm
by Ocelot
First question, why list, dict, indexes, why not simply:

Code: Select all

default current_place = "" # No current place

# Establishes relationship between place name and image.
# Note that in your case right now you do not even need dictionary, as place name is the same as image name
define places_bg = {
    "room_1": room_1,
    "room_2": room_2,
}

# setting pace and getting place image is as simple as:
$ current_place = "room_1"
$ something = places_bg[current_place]
And to answer your actual question:
scene statement with image is actually two statements internally:

Code: Select all

scene some_image
# This equals to:
scene # Remove everything from layer
show some_image # actually display stuff
You can use show expression as second statement here:

Code: Select all

scene places_list[bg_code]
# Reolace with
scene
show expression places_list[bg_code]

Re: Problems with displaying variable images

Posted: Mon Oct 24, 2022 4:19 pm
by Team1Player2
Because in some cases, putting things into lists, dicts etc. made my life so much easier and I thoughtlessly did it here too (not just here but I will be able to fix the rest eventually).

I opted for the first solution that simplifies the whole thing. I defaulted current_place and defined places_bg in this format:

Code: Select all

define places_bg = {
    "room_1": bg_room_1.png,
    "room_2": bg_room_2.png,
}
This throws the error saying that:

Code: Select all

While running game code:
  File "game/00_01_definitions.rpy", line 889, in script
    define places_bg = {
  File "game/00_01_definitions.rpy", line 889, in script
    define places_bg = {
  File "game/00_01_definitions.rpy", line 890, in <module>
    "room_1": bg_room_1.png,
NameError: name 'bg_room_1' is not defined
Doing:

Code: Select all

define places_bg = {
    "room_1": "bg_room_1.png",
    "room_2": "bg_room_2.png",
}
Gives different error:

Code: Select all

While loading <renpy.display.im.Image object ('bg_room_1.png') at 0x0000000006cb62b0>:
  File "game/02_01_common_events_places.rpy", line 39, in script
    "XXX"
OSError: Couldn't find file 'bg_room_1.png'.
Did I place in a wrong place?

Also would you plesae clarify the something in:

Code: Select all

# setting pace and getting place image is as simple as:
$ current_place = "room_1"
$ something = places_bg[current_place]
Scratch most of that. I fixed it (had to clarify subfolders).

Displaying by:

Code: Select all

scene
show expression places_bg[current_place]
also works. Need to do some more testing before marking as solved.