[Solved] imagebutton to pick different screens depending on 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
daniople
Newbie
Posts: 2
Joined: Fri Oct 07, 2022 12:53 am
Completed: Mostly Harmless
Projects: tastes like coffee, Tomodachi Dan
Tumblr: daniople
itch: daniople
Contact:

[Solved] imagebutton to pick different screens depending on variable

#1 Post by daniople » Fri Oct 07, 2022 3:34 am

Hello! I'm currently stuck trying to figure out the correct syntax for what im trying to do. I have a screen with 4 imagebuttons, and I'm trying to get each imagebutton press to do the following:
  • 1. to add + 1 to a variable: drink1_ingredients (this has been accomplished with SetVariable)
  • 2. depending on the current value of the previous variable (drink1_ingredients), to display a screen with the corresponding number (either "milk1", "milk2", or "milk3".)
At the moment, clicking on the imagebutton once (the milk one for example) once has all 3 milks show up, regardless of the value of drink1_ingredients. :( i want to be able to add the 3 'milks' one click at a time.
Image

here's what i have so far for the code of the imagebutton:

Code: Select all

    imagebutton:
        xpos 220
        ypos 250
        auto "images/UI/milk_%s.png"
        action [SetVariable("drink1_ingredients", drink1_ingredients + 1), If("drink1_ingredients = 1", Show("milk1")), If("drink1_ingredients = 2", Show("milk2")), If("drink1_ingredients = 3", Show("milk3"))]
and here is the traceback:

Code: Select all

I'm sorry, but an uncaught exception occurred.

After initialization, but before game start.
  File "game/script.rpy", line 119, in prepare_screen
    screen drinktime:
  File "game/script.rpy", line 119, in prepare
    screen drinktime:
  File "game/script.rpy", line 123, in prepare
    imagebutton:
  File "game/script.rpy", line 123, in prepare
    imagebutton:
SyntaxError: invalid syntax (game/script.rpy, line 127)

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

Full traceback:
  File "D:\renpy-8.0.3-sdk\renpy\bootstrap.py", line 277, in bootstrap
    renpy.main.main()
  File "D:\renpy-8.0.3-sdk\renpy\main.py", line 660, in main
    run(restart)
  File "D:\renpy-8.0.3-sdk\renpy\main.py", line 95, in run
    renpy.display.screen.prepare_screens()
  File "D:\renpy-8.0.3-sdk\renpy\display\screen.py", line 976, in prepare_screens
    s.ast.prepare_screen()
  File "game/script.rpy", line 119, in prepare_screen
    screen drinktime:
  File "game/script.rpy", line 119, in prepare
    screen drinktime:
  File "game/script.rpy", line 123, in prepare
    imagebutton:
  File "game/script.rpy", line 123, in prepare
    imagebutton:
  File "D:\renpy-8.0.3-sdk\renpy\py3analysis.py", line 790, in ast_eval
    return self.ast_eval_literal(expr)[0]
  File "D:\renpy-8.0.3-sdk\renpy\py3analysis.py", line 772, in ast_eval_literal
    expr = py_compile(expr, 'eval', ast_node=True)
  File "D:\renpy-8.0.3-sdk\renpy\python.py", line 1027, in py_compile
    raise e
  File "D:\renpy-8.0.3-sdk\renpy\python.py", line 979, in py_compile
    raise orig_e
  File "D:\renpy-8.0.3-sdk\renpy\python.py", line 972, in py_compile
    tree = compile(source, filename, py_mode, ast.PyCF_ONLY_AST | flags, 1)
SyntaxError: invalid syntax (game/script.rpy, line 127)
Last edited by daniople on Fri Oct 07, 2022 2:33 pm, edited 1 time in total.

User avatar
laure44
Regular
Posts: 60
Joined: Mon Mar 08, 2021 10:55 pm
Projects: Arkan'sTower, Gemshine Lorelei!
Location: France
Contact:

Re: imagebutton to pick different screens depending on variable

#2 Post by laure44 » Fri Oct 07, 2022 9:02 am

Use == for comparison, without the quotes.

Code: Select all

action SetVariable("drink1_ingredients", drink1_ingredients + 1), If(drink1_ingredients == 1, Show("milk1")), If(drink1_ingredients == 2, Show("milk2")), If(drink1_ingredients == 3, Show("milk3"))

User avatar
daniople
Newbie
Posts: 2
Joined: Fri Oct 07, 2022 12:53 am
Completed: Mostly Harmless
Projects: tastes like coffee, Tomodachi Dan
Tumblr: daniople
itch: daniople
Contact:

Re: imagebutton to pick different screens depending on variable

#3 Post by daniople » Fri Oct 07, 2022 2:32 pm

This worked perfectly, thank you so much!!! ^^

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]