Assigning image dependent on player gender choice

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
User avatar
Bum_McFluff
Regular
Posts: 45
Joined: Sat Aug 18, 2018 8:15 pm
Contact:

Assigning image dependent on player gender choice

#1 Post by Bum_McFluff »

For the side image, I'm trying to assign either a female head or a male head, depending on what selection the player made for the character. At this stage I'm only using a single design for each, but I'm getting an error (shown below)

My code for gender selection, which is working.

Code: Select all

menu:
    'Male':
        $ gender = "Male"
    'Female':
        $ gender = "Female"
        jump test

label test:        
scene gendertest
	if gender == "Female":
	o "I'm a woman"
	else:
	o "I'm a man"
My code for assigning image to selection

Code: Select all

image x = if gender == "Female": 'characters/You_F_Speak_Left.png'
else: 'characters/You_M_Speak_Left.png'

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.

File "game/images.rpy", line 28: expected statement.
    else: 'characters/You_M_Speak_Left.png'
        ^

File "game/images.rpy", line 27: invalid syntax
    if gender = "Female": 'characters/You_F_Speak_Left.png'
      ^

Ren'Py Version: Ren'Py 7.3.5.606
Sat Feb 22 13:48:13 2020
Once I get this sorted, then I might try expanding it to take on other potential choices (hair colour, skin colour etc), but baby steps for me.
What, spy on our spy as he searches for their spy? Why not, sounds rather like fun.

User avatar
Bum_McFluff
Regular
Posts: 45
Joined: Sat Aug 18, 2018 8:15 pm
Contact:

Re: Assigning image dependent on player gender choice

#2 Post by Bum_McFluff »

I also tried this:

Code: Select all

image x:
    choice( gender == "Female"):
        'characters/You_F_Speak_Left.png'
    choice( gender == "Male"):
        'characters/You_M_Speak_Left.png'
but it throws up this

Code: Select all

I'm sorry, but an uncaught exception occurred.

Compiling ATL code at game/images.rpy:28
NameError: name 'gender' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\bootstrap.py", line 316, in bootstrap
    renpy.main.main()
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\main.py", line 519, in main
    renpy.atl.compile_all()
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\atl.py", line 231, in compile_all
    i.compile()
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\atl.py", line 489, in compile
    block = self.atl.compile(self.context)
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\atl.py", line 684, in compile
    statements = [ i.compile(ctx) for i in self.statements ]
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\atl.py", line 1384, in compile
    return Choice(self.loc, [ (ctx.eval(chance), block.compile(ctx)) for chance, block in self.choices])
  File "C:\Users\Admin\Documents\Work in Progress\0 Robin Projects\renpy-7.0.0-sdk\renpy\atl.py", line 246, in eval
    return eval(expr, renpy.store.__dict__, self.context)  # @UndefinedVariable
  File "<string>", line 1, in <module>
NameError: name 'gender' is not defined

Windows-7-6.1.7601-SP1
Ren'Py 7.3.5.606
Slave Maker Revenge 1.1
Sat Feb 22 19:45:15 2020
I have my .rpy files in sections to make it easier for me to see where I am. This was mentioned as being okay to do in another post somewhere.
My .rpy files in the order they run as far as this bit is concerned is:
script.rpy which takes me to
char_selection.rpy where you choose your gender and name them, which leads to
story_intro.rpy where the background story is explained to the new player which leads to
office.rpy where the character is briefed by their boss as to their mission, which is the point of the game.
The images are stored in images.rpy which is where the very first code above is stored.

Additionally, characters are laid out in character.rpy, and every scene has its own .rpy file, for example hotel.rpy

So it's telling me that NameError: name 'gender' is not defined. I thought that it was defined when I first added the menu as seen in the original post.

Code: Select all

menu:
    'Male':
        $ gender = "Male"
    'Female':
        $ gender = "Female"
        jump test
Because I was able to then add

Code: Select all

label test:        
scene gendertest
	if gender == "Female":
	o "I'm a woman"
	else:
	o "I'm a man"
	
and it worked. So where have I erred?

I apologize if I have bent the rules, but I believe this is pertinent to the original question.
What, spy on our spy as he searches for their spy? Why not, sounds rather like fun.


Post Reply

Who is online

Users browsing this forum: Google [Bot]