[Solved] A bit of an 'if'fy problem, a good way to change screen actions for the same button when condition(s) are met?

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
zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

[Solved] A bit of an 'if'fy problem, a good way to change screen actions for the same button when condition(s) are met?

#1 Post by zeroTheHero »

I'm trying to write some if statements and this one keeps giving an error-
My code:

Code: Select all

default turn = "one"


screen turnOne:
    frame:
        imagebutton auto "pika.jpg" action (SetScreenVariable(turn, "two"))

screen turnTwo:
    frame:
        imagebutton auto "charmander.png" action (SetScreenVariable(turn, "three"))

screen turnThree:
    frame:
        imagebutton auto "cafe.jpg" action (SetScreenVariable(turn, "one"))

label start:
    if turn == "one":
        show screen turnOne
    elif turn == "two":
        show screen turnTwo 
    else:
        show screen turnThree
    e "..."
TypeError: Not all arguments converted during string formatting.

Help?

PS: If you know any way to dynamically change screen actions when some conditions are met in-game please let me know. For example, I have a textbutton say "Attack" and I want it to output different text based on certain conditions, like "HP = 100%", "<50%", "if evolution = True", etc.
I tried action If() and it would be suitable if I could chain it in some way, or have more than one If().

Thanks!
Last edited by zeroTheHero on Tue Nov 06, 2018 8:12 am, edited 2 times in total.

User avatar
Andredron
Miko-Class Veteran
Posts: 714
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: A bit of an 'if'fy problem, a good way to change screen actions when condition(s) are met?

#2 Post by Andredron »

Code: Select all

screen battlefield:
     if turn == "one":
        ....
    elif turn == "two":
        ....
    else:
        ....
        
label start:
     " text"
     $ turn = "one"
     "text"
     
Imagebutton

Code: Select all

screen imagbutton:
    frame:
        background None
        imagebutton xalign 0.45 yalign 0.45:
            idle ("images/button_idle.png")
            hover ("images/button_hover.png")
            action [Jump("example_label"), SetScreenVariable(turn, "one"), HideScreen("rex")]

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: A bit of an 'if'fy problem, a good way to change screen actions when condition(s) are met?

#3 Post by zeroTheHero »

Andredron wrote: Sat Oct 27, 2018 2:15 pm

Code: Select all

screen battlefield:
     if turn == "one":
        ....
    elif turn == "two":
        ....
    else:
        ....
        
label start:
     " text"
     $ turn = "one"
     "text"
     
Imagebutton

Code: Select all

screen imagbutton:
    frame:
        background None
        imagebutton xalign 0.45 yalign 0.45:
            idle ("images/button_idle.png")
            hover ("images/button_hover.png")
            action [Jump("example_label"), SetScreenVariable(turn, "one"), HideScreen("rex")]
Hey Andredon! Thanks for answering! The thing is, I want the actions of the imagebutton to change depending on conditions (like if turn == "one" do 'this', elif turn == "two" do 'this', else do 'this' etc).

From my understanding of your code, screen imagebutton has an imagebutton that sets variable 'turn' to "one". But if after turn =="one" condition has been met, how would I change the action of the imagebutton to SetScreenVariable(turn, "two")?

The way I did this was if turn=="one", show screen containing imagebutton that SetScreenVariable(turn, "two") , but if turn =="two" show screen containing imagebutton that SetScreenVariable(turn, "three") etc.

...This didn't work, as seen in my previous post. Some pointers would be gratefully acknowledged. Thanks!

martingerdes
Regular
Posts: 26
Joined: Sun Oct 07, 2018 5:14 am
Contact:

Re: A bit of an 'if'fy problem, a good way to change screen actions for the same button when condition(s) are met?

#4 Post by martingerdes »

How about something like this, keeping closely to your starting code:

Code: Select all

default turn = "one"

screen turn():
    frame:
        if turn=="one":
            textbutton "pica" action ([SetVariable("turn", "two"),Return(True)])
        elif turn=="two":
            textbutton "charmander" action ([SetVariable("turn", "three"),Return(True)])
        else:
            textbutton "cafe" action ([SetVariable("turn", "one"),Return(True)])

label start:
    e "now coming: turn [turn]"
    call screen turn
    jump start
->you don't need turn just in the screen, so I would make a normal variable out of it.
You need to give SetVariable a string for the variable name, this is probably your original error.
Using "call screen" instead of "show screen" leaves Eileen behind.
Using a second action Return(True) returns from the screen back to Eileen.

To actually get the same button to do different things, you need a data structure telling you what to do when.
So lets rewrite this more elegantly:

Code: Select all

default turn = "one"

define turns={
    "one":{"title":"pica","next":"two"},
    "two":{"title":"charmander","next":"three"},
    "three":{"title":"cafe","next":"one"},
}

screen turn():
    frame:
        textbutton turns[turn]["title"] action ([SetVariable("turn", turns[turn]["next"]),Return(True)])

label start:
    e "now coming: turn [turn]"
    call screen turn
    jump start
Does this help? :)

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: A bit of an 'if'fy problem, a good way to change screen actions for the same button when condition(s) are met?

#5 Post by zeroTheHero »

martingerdes wrote: Sun Oct 28, 2018 10:06 am How about something like this, keeping closely to your starting code:

Code: Select all

default turn = "one"

screen turn():
    frame:
        if turn=="one":
            textbutton "pica" action ([SetVariable("turn", "two"),Return(True)])
        elif turn=="two":
            textbutton "charmander" action ([SetVariable("turn", "three"),Return(True)])
        else:
            textbutton "cafe" action ([SetVariable("turn", "one"),Return(True)])

label start:
    e "now coming: turn [turn]"
    call screen turn
    jump start
->you don't need turn just in the screen, so I would make a normal variable out of it.
You need to give SetVariable a string for the variable name, this is probably your original error.
Using "call screen" instead of "show screen" leaves Eileen behind.
Using a second action Return(True) returns from the screen back to Eileen.

To actually get the same button to do different things, you need a data structure telling you what to do when.
So lets rewrite this more elegantly:

Code: Select all

default turn = "one"

define turns={
    "one":{"title":"pica","next":"two"},
    "two":{"title":"charmander","next":"three"},
    "three":{"title":"cafe","next":"one"},
}

screen turn():
    frame:
        textbutton turns[turn]["title"] action ([SetVariable("turn", turns[turn]["next"]),Return(True)])

label start:
    e "now coming: turn [turn]"
    call screen turn
    jump start
Does this help? :)
Holy moly I'm so sorry for not having seen this; for whatever reason I stopped receiving email updates from lemma. This is so amazingly helpful, I never really knew data sets could be used like that! For the past few days I've been chugging coffee and hard - coding all my buttons and screens, I would've loved to try your approach but I feel bad for all the coffee I would drink if I did XO Seriously though, thanks a ton for taking the time to write such a detailed answer, it really means a ton to a newb like me :)

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: A bit of an 'if'fy problem, a good way to change screen actions for the same button when condition(s) are met?

#6 Post by zeroTheHero »

zeroTheHero wrote: Sat Oct 27, 2018 9:47 am I'm trying to write some if statements and this one keeps giving an error-
My code:

Code: Select all

default turn = "one"


screen turnOne:
    frame:
        imagebutton auto "pika.jpg" action (SetScreenVariable(turn, "two"))

screen turnTwo:
    frame:
        imagebutton auto "charmander.png" action (SetScreenVariable(turn, "three"))

screen turnThree:
    frame:
        imagebutton auto "cafe.jpg" action (SetScreenVariable(turn, "one"))

label start:
    if turn == "one":
        show screen turnOne
    elif turn == "two":
        show screen turnTwo 
    else:
        show screen turnThree
    e "..."
TypeError: Not all arguments converted during string formatting.

Help?

PS: If you know any way to dynamically change screen actions when some conditions are met in-game please let me know. For example, I have a textbutton say "Attack" and I want it to output different text based on certain conditions, like "HP = 100%", "<50%", "if evolution = True", etc.
I tried action If() and it would be suitable if I could chain it in some way, or have more than one If().

Thanks!
I found the problem - it's 'auto'. Apparently, imagebutton auto does not work like this, instead writing imagebutton idle " " hover " " (even if they are the same pic) should solve the issue.

Post Reply

Who is online

Users browsing this forum: bonnie_641