{Solved} ui.imagebuttons User Issue

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
Mehmed
Newbie
Posts: 23
Joined: Sat Jul 20, 2013 3:57 pm
Contact:

{Solved} ui.imagebuttons User Issue

#1 Post by Mehmed »

I looked around for a while but I couldn't find anything on this, I recently moved over to imagebuttons but I'm having trouble getting acquainted with them in their entirety.
I'm a bit rusty since I've put off work for about a year, but what I'm working on is a fight screen where the player clicks on the button to attack, rather than using what I have now to do so.
Here is an example of what I have now :

Code: Select all

label placeholder:
    while (enemy_hp > 0) and (player_hp > 0):
        menu:
            "Attack":
                $ player_damage = renpy.random.randint(1, 6)
                $ enemy_hp -= player_damage
                r "You attack [enemy] for [player_damage]!"
                "([enemy] has [enemy_hp] HP.)"
            "Rest":
                $ player_hp = min(player_hp+5, player_max_hp)
                r "You recovered 5 HP."
    
        if enemy_hp <= 0:
            "[enemy] has died."
            return 
            
        if enemy_hp >= 0:
            $ enemy_damage = renpy.random.randint(1, 6)
            
            if enemy_damage > 3:
                $ player_hp -= enemy_damage
                w "[enemy] hits you(light). *[enemy_damage]*"
                
            if enemy_damage == 3:
                $ player_hp -= enemy_damage+2
                w "[enemy] hits you 2(medium). *[enemy_damage]*"
            
            if enemy_damage < 3:
                $ player_hp -= enemy_damage+5
                w "[enemy]hits you 3(heavy). *[enemy_damage]*"
   
    if enemy_hp <= 0:
        "[enemy] has died."
        return
           
    else:
        "[enemy] defeats you"
        $ player_hp = 0
        return     
I tried this complete mess, as an example

Code: Select all

 imagebutton auto "Graphics/GUI/Attack_%.png" xpos 1230 ypos 35 focus_mask True action [ SetVariable(player_damage = renpy.random.randint(1, 6))  SetVariable(enemy_hp -= player_damage)] 
But got a syntax error, so I figure because player_damage isn't properly defined in that line so it can't grab it for enemy_hp -=. I knew I was being stupid with it anyway but it was worth a shot before I came here asking.

What I'd like is to have the imagebutton correspond to Attack and another for Rest, but I can't figure out how to get an imagebutton to pull multiple actions in one go like I described above. I thought maybe trying to make it correspond to a variable, like
SetVariable (variable1, 1)
If variable1 = 1
label attackscript : "attack script + $ variable1 = 0"

Or something along those lines which I know I could make work but it seems really clunky and I know there has to be a more straightforward and overall competent way of going about this.

Please forgive my ignorance and help me out with what I did wrong and what I should do, or at least give me an example of the proper way of going about this if you could. I looked around and read for a bit but couldn't find anything to meet my needs.
I thought I was on top of this for a while, but I'm totally out in unknown waters right now.
Thanks in advance for any help.
Last edited by Mehmed on Tue Aug 12, 2014 3:46 pm, edited 1 time in total.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: ui.imagebuttons User Issue

#2 Post by Milkymalk »

Syntax error often means you have a ")" too many, or you forgot a ",":

Code: Select all

 imagebutton auto "Graphics/GUI/Attack_%.png" xpos 1230 ypos 35 focus_mask True action [ SetVariable(player_damage = renpy.random.randint(1, 6)), SetVariable(enemy_hp -= player_damage)] 
Try this; I separated the two SetVariable with a comma. I'm a rookie with button actions myself, but this has a higher chance of working than your original code ;-)
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Mehmed
Newbie
Posts: 23
Joined: Sat Jul 20, 2013 3:57 pm
Contact:

Re: ui.imagebuttons User Issue

#3 Post by Mehmed »

Milkymalk wrote:Syntax error often means you have a ")" too many, or you forgot a ",":

Code: Select all

 imagebutton auto "Graphics/GUI/Attack_%.png" xpos 1230 ypos 35 focus_mask True action [ SetVariable(player_damage = renpy.random.randint(1, 6)), SetVariable(enemy_hp -= player_damage)] 
Try this; I separated the two SetVariable with a comma. I'm a rookie with button actions myself, but this has a higher chance of working than your original code ;-)
Nah, that's what I had before but I forgot to type up the proper separators in the example, sorry.

Image

User avatar
korova
Veteran
Posts: 217
Joined: Sat Jun 27, 2009 5:15 pm
Completed: Ivy, Chocolate, Time, Clair Obscur
Projects: Writing exercises, The House [Nano18]
Tumblr: korova08
itch: korova
Location: Normandie, France
Contact:

Re: ui.imagebuttons User Issue

#4 Post by korova »

I think SetVariable syntax is a bit different, more like

Code: Select all

SetVariable("player_damage", renpy.random.randint(1, 6))
i.e. name of the variable like a string, a coma and the value you want to give to your varirable.

(see here)

User avatar
Mehmed
Newbie
Posts: 23
Joined: Sat Jul 20, 2013 3:57 pm
Contact:

Re: ui.imagebuttons User Issue

#5 Post by Mehmed »

korova wrote:I think SetVariable syntax is a bit different, more like

Code: Select all

SetVariable("player_damage", renpy.random.randint(1, 6))
i.e. name of the variable like a string, a coma and the value you want to give to your variable.

(see here)
I believe because the second "player_damage" isn't being set on its own container it's trying to set both at the same time.
The latter one is a value which hasn't yet been set by the first one, so it's reporting it has not been defined.
So I tried this

Code: Select all

[SetVariable("player_damage",renpy.random.randint(1, 6))]
Image

So I came to the conclusion that it was the first one doing it's business. Trying it with a regular number (6), it seems to have worked on the imagebutton's end, although I needed to tailor it properly into the battle. I'm guessing it didn't like the renpy. statement, how can I get this to randomize between the numbers though?
How can I implement the "enemy_hp =- player_damage" at the end as well? It's absolutely crucial to the rest of the script that enemy_hp detracts from player_damage, otherwise modifiers will cease to work completely.
The cookbook doesn't really touch on this very much, or I've just been reading the wrong parts. The tutorial itself only covers basic connections and transformations.

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

Re: ui.imagebuttons User Issue

#6 Post by Alex »

You get this error 'cause all actions in list are execute simultaneously (the order doesn't matter at all). So in your battle loop you could call a screen instead of showing a menu and make all the neccessary calculations depending of the value that would be returned by this screen.

Code: Select all

screen attack_scr:
    vbox:
        textbutton "Attack!" action Return( "attack" )
        textbutton "Rest" action Return( "rest" )

label placeholder:
    while (enemy_hp > 0) and (player_hp > 0):
        
        call screen attack_scr
        
        if _return == "attack":
            $ player_damage = renpy.random.randint(1, 6)
            $ enemy_hp -= player_damage
            r "You attack [enemy] for [player_damage]!"
            "([enemy] has [enemy_hp] HP.)"
            
        elif _return == "rest":
            $ player_hp = min(player_hp+5, player_max_hp)
            r "You recovered 5 HP."
http://www.renpy.org/doc/html/screens.html#call-screen

User avatar
Mehmed
Newbie
Posts: 23
Joined: Sat Jul 20, 2013 3:57 pm
Contact:

Re: ui.imagebuttons User Issue

#7 Post by Mehmed »

Alex wrote:You get this error 'cause all actions in list are execute simultaneously (the order doesn't matter at all). So in your battle loop you could call a screen instead of showing a menu and make all the neccessary calculations depending of the value that would be returned by this screen.

Code: Select all

screen attack_scr:
    vbox:
        textbutton "Attack!" action Return( "attack" )
        textbutton "Rest" action Return( "rest" )

label placeholder:
    while (enemy_hp > 0) and (player_hp > 0):
        
        call screen attack_scr
        
        if _return == "attack":
            $ player_damage = renpy.random.randint(1, 6)
            $ enemy_hp -= player_damage
            r "You attack [enemy] for [player_damage]!"
            "([enemy] has [enemy_hp] HP.)"
            
        elif _return == "rest":
            $ player_hp = min(player_hp+5, player_max_hp)
            r "You recovered 5 HP."
http://www.renpy.org/doc/html/screens.html#call-screen
Oh wow, didn't even think of using a return.
Thanks so much man.

Post Reply

Who is online

Users browsing this forum: Google [Bot]