Problems With Conditional Actions When Using Screens [Solved

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
DerWille
Regular
Posts: 26
Joined: Sun Jul 10, 2011 7:30 pm
Contact:

Problems With Conditional Actions When Using Screens [Solved

#1 Post by DerWille »

So, I'm still plowing along with a character creation screen and I'm coming to a problem. How can I make the action of a button be conditional? For example, let's imagine Protag-kun is deciding how much strength his life has given him up to this point.

He has 20 attribute points to distribute amongst his stats. No stat can be lower than 5 and no higher than 20. How do I go about programming this in the screen itself, so the buttons disable themselves or stop working once they've hit the right conditions?

Right now, this is my code with all of my experimentation removed:

Code: Select all

screen characterCreation:

    grid 4 1:
        text "Strength" xalign 0.1 yalign 0.10
        text "[Protag.Strength]" xalign 0.15 yalign .10
        
        textbutton "-" xalign 0.17 yalign .10:
            action[ SetField( Protag, "Strength", Protag.Strength-1 ), SetField( Protag, "attributePoints", Protag.attributePoints+1) ]
                
        textbutton "+" xalign 0.19 yalign.10:
            action[ SetField( Protag, "Strength", Protag.Strength+1 ), SetField( Protag, "attributePoints", Protag.attributePoints-1 ) ]
            
    text "Attribute Points" xalign 0.0 yalign 0.30
    text "[Protag.attributePoints]" xalign 0.25 yalign 0.30
One of the experiments I tried was using this: http://www.renpy.org/doc/html/screen_actions.html#If But when I used it, I received the error, "Textbutton has no 'If' child." So I tried the other if, with a lowercase i and the same error occurred.

Another experiment was instead of trying to do all of this on the screen, I'd call a function every time a button was pressed. That however caused the buttons to completely deactivate.

Does anyone have any ideas on how to do this or is the impossible kicking me to the curb?
Last edited by DerWille on Sun Mar 04, 2012 1:16 pm, edited 1 time in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Problems With Conditional Actions When Using Screens

#2 Post by Alex »

Try

Code: Select all

action if (  (Protag.Strength < 20 and Protag.attributePoints >0), true = [SetField( Protag, "Strength", Protag.Strength+1 ), SetField( Protag, "attributePoints", Protag.attributePoints-1 ) ], false = None )
(not tested)

DerWille
Regular
Posts: 26
Joined: Sun Jul 10, 2011 7:30 pm
Contact:

Re: Problems With Conditional Actions When Using Screens

#3 Post by DerWille »

Thanks for the reply. I tried it out, Ren'Py doesn't run and this pops up:

Code: Select all

I'm sorry, but an uncaught exception occurred.

After loading the script.
KeyError: u'SetField( Protag, "Strength", Protag'
I removed one of the actions from the list and ran it. Ren'Py does run, but I got this error:

Code: Select all

expected 'simple_expression' not found.
action -> if( ( Protag.Strength > 5 and Protag.attributePoints < 20 ), true = SetField( Protag, "Strength", Protag.Strength-1, false = None )
Two different errors depending on the number of actions given.


DerWille
Regular
Posts: 26
Joined: Sun Jul 10, 2011 7:30 pm
Contact:

Re: Problems With Conditional Actions When Using Screens

#5 Post by DerWille »

Good eye. Works like a charm. Thanks for helping me.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot], Sugar_and_rice, voluorem