Page 1 of 1

Variable to access file

Posted: Thu Sep 23, 2021 10:50 am
by Feleven
I'm making a glossary and want to make textbuttons that change the character's picture. Atm I have a different set of buttons for each character but I'd like to make only 1 set for all of them. Just to tidy up.
It looks like this atm:

Code: Select all

textbutton "1"  action SetVariable("boy.imageName", "gui/custom/glossary/boy_glossary1.webp") 
textbutton "2"  action SetVariable("boy.imageName", "gui/custom/glossary/boy_glossary2.webp") 
Boy is the character's name.

I made a class with the selectedCharacter variable and this is how I tried to 'automate' the whole thing. The first part works fine. What bricks it is finding the file.

Code: Select all

textbutton "1" action SetVariable("selectedCharacter.imageName", "gui/custom/glossary/" + "selectedCharacter" + "_glossary1.webp") 
""gui/custom/glossary/" + "selectedCharacter" + "_glossary1.webp"" just doesn't work. I also tried:

Code: Select all

"gui/custom/glossary/" + "[selectedCharacter]" + "_glossary1.webp"
"gui/custom/glossary/" + selectedCharacter + "_glossary1.webp"
This is the error I get:

Code: Select all

  File "renpy/display/image.py", line 577, in _scope
    return self.find_target(scope, update)
  File "renpy/display/image.py", line 652, in find_target
    raise Exception(error)
Exception: DynamicImage u'[selectedCharacter.imageName]': could not find image. (u'gui/custom/glossary/selectedCharacter_glossary1.webp')
Would appreciate any clues or help.

Re: Variable to access file

Posted: Thu Sep 23, 2021 2:02 pm
by jeffster
I think that in the right part of the assignment you should not quote out the variables names. I.e. the right way should be:

Code: Select all

"gui/custom/glossary/" + selectedCharacter + "_glossary1.webp")
However selectedCharacter should be a string if you want to use it as a part of a string.

But look here:

Code: Select all

textbutton "1" action SetVariable("selectedCharacter.imageName",
You use selectedCharacter.imageName which means it's a complex object, not a string.

That object probably has a field like "name" or something, which would be the string you need.

Then the solution is:

Code: Select all

textbutton "1" action SetVariable("selectedCharacter.imageName",
    "gui/custom/glossary/" + selectedCharacter.name + "_glossary1.webp")

Re: Variable to access file

Posted: Fri Sep 24, 2021 11:54 am
by Feleven
jeffster wrote: Thu Sep 23, 2021 2:02 pm You use selectedCharacter.imageName which means it's a complex object, not a string.

That object probably has a field like "name" or something, which would be the string you need.

Then the solution is:

Code: Select all

textbutton "1" action SetVariable("selectedCharacter.imageName",
    "gui/custom/glossary/" + selectedCharacter.name + "_glossary1.webp")
You're spot on right. Thank you.
I went from having repetitive screens and entries for each character to having the whole thing in 60 lines of code.