'use' statement with a variable

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
Kernel Panic
Newbie
Posts: 10
Joined: Mon Jul 31, 2017 5:42 pm
Contact:

'use' statement with a variable

#1 Post by Kernel Panic »

Hello! I'm having troubles with screens again.
Let's say I want to let player pick a class. And after that I want to show him a button - different one for each class.
So right now working code looks like this:



Code: Select all

init:
    image template_image = "/gui/window_icon.png"


screen warrior_screen:
    add "template_image"
    text "warrior"
screen thief_screen:
    add "template_image"
    text "thief"
screen wizard_screen:
    add "template_image"
    text "wizard"

screen test_thingy:
    if player_class == 'warrior': # button for the warrior character
        button:
            xsize 250
            use warrior_screen
            action NullAction
    elif player_class == 'thief': # button for the thief character
        button:
            xsize 250
            use thief_screen
            action NullAction
    elif player_class == 'wizard': # button for the wizard character
        button:
            xsize 250
            use wizard_screen
            action NullAction



label start:

    menu:
        "Pick a class"
        "Warrior":
            $ player_class = 'warrior'
        "Thief":
            $ player_class = 'thief'
        "Wizard":
            $ player_class = 'wizard'

    call screen test_thingy


    return


The problem is with the screen test_thingy. It looks fine with 3 classes, but what if I need a few dozens of them?
Also only thing that changes in there is the name of the screen they are using, depending on what class (player_class) is chosen by the player:

Code: Select all

screen test_thingy:
    if player_class == 'warrior':
        button:
            xsize 250
            use warrior_screen
            action NullAction
    elif player_class == 'thief':
        button:
            xsize 250
            use thief_screen
            action NullAction
    elif player_class == 'wizard':
        button:
            xsize 250
            use wizard_screen
            action NullAction

What I'm asking is - is that possible to add a variable to the use statement?
Something like (following example doesn't work):

Code: Select all

screen test_thingy:
    button:
        xsize 250
        use "[player_class]_screen"
        action NullAction

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: 'use' statement with a variable

#2 Post by kivik »

Funny enough I was looking into this couple of evenings ago and didn't find anything. However looking at your example, I don't think you need to use different screens for different classes anyway. You should be able to do it with some variables:


Code untested:

Code: Select all

define classes = {"warrior":"warrior.png","thief":"thief.png","wizard":"wizard.png"}
screen test_thingy(player_class):
    button:
        xsize 250
        add classes[player_class]
        text player_class
        action NullAction

label start:
    menu:
        "Pick a class"
        "Warrior":
            $ player_class = 'warrior'
        "Thief":
            $ player_class = 'thief'
        "Wizard":
            $ player_class = 'wizard'

    call screen test_thingy(player_class)
    return
I'm guessing you'll want to link the button's action to the class as well - again, you can call a label with a parameter and pass the player_class variable to it.

Code: Select all

action Call("init_player",player_class=playerclass

...

label init_player(player_class):
    # do stuff here based on the class variable

Post Reply

Who is online

Users browsing this forum: Bing [Bot]