[SOLVED] way to change variables with computations/arithmetic (like add +1, subtract -1) via a textbutton on a screen?

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
lorenzopolo
Newbie
Posts: 6
Joined: Tue Jul 14, 2020 10:12 am
Contact:

[SOLVED] way to change variables with computations/arithmetic (like add +1, subtract -1) via a textbutton on a screen?

#1 Post by lorenzopolo »

I'm trying to create an inventory system where there is a list of items in text. When clicked, the items would either add health, mana, damage enemies, etc. but I keep running into problems like the viewport not having a scrollbar, the menu exiting out when using a single item, the return button repeating the previously used item and more.

This is kinda a symptom of me trying to call and jump to labels for changing the variables in my game, and a lot of my problems would be solved if i can change variables with button clicks.

Here is the current messy code:

Code: Select all

screen inventorybutton:
    vbox:
        xalign 0.0
        yalign 0.5
        spacing 4
        textbutton "Inventory":
            action ShowMenu("inventory_screen")
            #action [Call ("inventoryactivation")]
label inventoryactivation:
    show screen inventory_screen
    hide screen inventorybutton
    window hide### disables the dialogue
    #pause###
style inventory_label:
    xalign 0.2
label healthpotionuse:
    $itemamounthealthpotion -= 1
    $playerhealth += 30
    window hide
    pause
    jump nothinguse
    return
label manapotionuse:
    $itemamountmanapotion -= 1
    $playermana += 30
    window hide
    pause
    jump nothinguse
    return
label nothinguse:
    return
screen inventory_screen:
    default tooltiptext = Tooltip("")
    default tooltipx = Tooltip(0)
    default tooltipy = Tooltip(0)
    vbox:
        xmaximum 300
        ymaximum 300
        spacing 10
        xalign 0.5
        yalign 0.1
        label "Items"
    viewport id "vp":
        child_size (500,300)
        scrollbars True
        draggable True
        xpos 950
        ypos 150
        add "white"
        vbox:
            xmaximum 300
            ymaximum 300
            spacing 10
            xalign 0.57
            yalign 0.15
            if itemamounthealthpotion > 0:
                textbutton "Health Potions [itemamounthealthpotion]x":
                    action [Call ("healthpotionuse"),ShowMenu("inventory_screen")]
                    #action [SetVariable(itemamounthealthpotion, itemamounthealthpotion-1)]
                    hovered tooltiptext.Action("Health Potion: Restores 30 Health")
            if itemamountmanapotion > 0:
                textbutton "Mana Potions [itemamountmanapotion]x":
                    action [Call ("manapotionuse"),ShowMenu("inventory_screen")]
                    #action [SetVariable(itemamountmanapotion, itemamountmanapotion-1)]
                    hovered tooltiptext.Action("Mana Potion: Restores 30 Mana")
            label "extra"
            label "extra1"
            label "extra2"
            label "extra3"
            label "extra4"
            label "extra5"
    textbutton "Return":
        action [hide_inventory]
        #action Return()
        xalign 0.5
        yalign 0.95
        hovered tooltiptext.Action("Exit")
        
Last edited by lorenzopolo on Tue Jul 14, 2020 11:03 am, edited 1 time in total.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Is there a way to change variables with computations/arithmetic (like add +1, subtract -1) via a textbutton on a scr

#2 Post by RicharDann »

You can do it with the SetVariable action, I'm going to guess you tried to do so since it's there in your code but commented out. My guess is it didn't work, but that might because you have to pass the variable name as a string, then the expression you want to set the variable to.

Code: Select all

action [SetVariable("potion_amount", potion_amount-1), SetVariable("player_health", player_health+30)]
You don't have to call any labels this way. You can also use If or SensitiveIf action to check how many items are left so it doesn't substract below 0 and you end up having -1 potions.
The most important step is always the next one.


Post Reply

Who is online

Users browsing this forum: Bing [Bot]