{Solved} Ren'py is using both the if and else statement.

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

{Solved} Ren'py is using both the if and else statement.

#1 Post by Mehmed »

It has been executing both the if statement and afterward the else. I can't find the exact reason for this and any help would be appreciated.

Ex.

Code: Select all

        if charclass == "charclassplaceholder":
            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                         
                "Rest":
                    $ player_hp = min(player_hp+15, player_max_hp)
                    r "That's a little better"

                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away." 
                        jump checkup
                    if fleechance <= 4: 
                        "You failed to run."

                    
                "Spell Placeholder" if player_mana > 5:
                    $ player_damage = renpy.random.randint(magicdamagelow, magicdamagehigh)
                    $ player_mana -= 5
                    r "Wow such magics. *[player_damage]*" 
                    "([enemy] has [enemy_hp] HP.)"
                    
        else:
            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                    
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    $ player_mana = min(player_mana+5, player_max_mana)
                    r "That's a little better"
                    
                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away." 
                        jump checkup
                    if fleechance <= 4: 
                        "You failed to run."
It was an indentation issue.
Last edited by Mehmed on Mon Sep 09, 2013 3:37 pm, edited 1 time in total.

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#2 Post by Elmiwisa »

I am guessing that this code is part of a loop. Perhaps you should post the code for the whole loop here?
My best guess is that there is nothing that went wrong, you are just mistaken because of the loop.

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

Re: Ren'py is using both the if and else statement. Help?

#3 Post by Mehmed »

Elmiwisa wrote:I am guessing that this code is part of a loop. Perhaps you should post the code for the whole loop here?
My best guess is that there is nothing that went wrong, you are just mistaken because of the loop.

Here's the entirety of it.

Code: Select all

label battle:

    while (enemy_hp > 0) and (player_hp > 0):
            
        if charclass == "magicuser":
            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                    
                   
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    r "That's a little better."


                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away." 
                        jump checkup
                    if fleechance <= 4: 
                        "You failed to run."

                    
                "Spell - Magic" if player_mana > 5:
                    $ player_damage = renpy.random.randint(magicdamagelow, magicdamagehigh)
                    $ player_mana -= 5
                    r "Description of magic being used. *[player_damage]*" 
                    "([enemy] has [enemy_hp] HP.)"
                    
        else:
            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                    
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    $ player_mana = min(player_mana+5, player_max_mana)
                    r "That's a little better."
                    
                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away." 
                        jump checkup
                    if fleechance <= 4: 
                        "You failed to run."
                                
        if enemy_hp <= 0:
            "[enemy] has died."
            $ experiencecurrent = experiencecurrent+5 
            
   
    if enemy_hp <=0:
        jump otherscene

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#4 Post by Elmiwisa »

And why do you think that both the if and the else part get run?

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

Re: Ren'py is using both the if and else statement. Help?

#5 Post by Mehmed »

Elmiwisa wrote:And why do you think that both the if and the else part get run?
If I had the vaguest idea I would of fixed it four hours ago while reviewing the cookbook a few times waiting for the answer to this.

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#6 Post by Elmiwisa »

Mehmed wrote:
Elmiwisa wrote:And why do you think that both the if and the else part get run?
If I had the vaguest idea I would of fixed it four hours ago while reviewing the cookbook a few times waiting for the answer to this.
You misunderstood the question. The question is about whether there is a problem at all. What do you expect to see on the screen? And what did you actually see?

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

Re: Ren'py is using both the if and else statement. Help?

#7 Post by Mehmed »

Elmiwisa wrote:
Mehmed wrote:
Elmiwisa wrote:And why do you think that both the if and the else part get run?
If I had the vaguest idea I would of fixed it four hours ago while reviewing the cookbook a few times waiting for the answer to this.
You misunderstood the question. The question is about whether there is a problem at all. What do you expect to see on the screen? And what did you actually see?
Sorry, sorry. I've been spending the past, week doing this and moving gravel. There is no greater hell than spending hours upon hours feeding work into this kind of stuff and having the only solution to the one problem plaguing you for the past few days being "why don't you google it?". I took out some of it on you, sorry.

What this is that I have here is a battle system I've rigged up off some other code which works perfectly fine, at least until I added the if statements for this.
What this use to do is display a menu to attack, rest, whatever actions for the current battle. After selecting your action it would execute it and then allow the enemy's action to continue which is the bottom few if statements I believe.

What it does now that I have the class based if statements, which based on your class will add a special attack at the cost of the mana variable, is allow you to go through this process twice.
Example, you click attack, and then it attacks, and then the menu to attack is up again. This is only if you're currently using a class that sets one of the if statements to true. If you're using a class that does not interact with the if statements it goes directly to the else and everything works fine, but if you're using a class that does trigger the if statement then you have

Code: Select all

                            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                   
                   
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    r "That's a little better."


                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away."
                        jump checkup
                    if fleechance <= 4:
                        "You failed to run."

                   
                "Spell - Magic" if player_mana > 5:
                    $ player_damage = renpy.random.randint(magicdamagelow, magicdamagehigh)
                    $ player_mana -= 5
                    r "Description of magic being used. *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
run and work, and then

Code: Select all

            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                   
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    $ player_mana = min(player_mana+5, player_max_mana)
                    r "That's a little better."
                   
                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away."
                        jump checkup
                    if fleechance <= 4:
                        "You failed to run."
run afterward, and then the enemy attacks. I was under the assumption that if an if statement was triggered it would ignore the else statement, but it's actually doing it the other way around and if the if statement isn't trigged it goes right to the else statement. That's not how it works for my other stuff so why is it different here? It's as if it is ignoring the else clause entirely and immediately jumping to the else statement's function regardless of whether or not an if statement before it has been triggered.

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#8 Post by Elmiwisa »

Gahh....you still misunderstood the question. I asked for what you ACTUALLY SEE on the SCREEN. I did NOT ask for what you THINK to have happened. That is, tell me what button get displayed on the screen, what dialogue did you see after you click one of the button, then what button do you see on the screen again.
You think that the else statement is run after the if statement. I think that it is more likely that it does not happened, you just made the wrong assumption and what that looks like the else statement being run is actually something else (most likely the if statement being looped back again).
So here is a few points you would want to check out:
1. What is player_mana before the battle start? Make sure that it is at least 6. If it is 5 or less, then the else block is indistinguishable from the if block as far as what is on the screen is concerned. Once the battle start, before clicking on any menu button, press Shift+D and select Variable Viewer to see what the value of player_mana is.
2. You have NO code for enemy attack here, yet you seems to claim to have one. Add in script for enemy attack at the end of the loop and see what happen. Without it (or something random as long as it mark the end of the loop iteration), you literally cannot tell whether a new iteration of the loop have started or not.

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

Re: Ren'py is using both the if and else statement. Help?

#9 Post by Mehmed »

Elmiwisa wrote:Gahh....you still misunderstood the question. I asked for what you ACTUALLY SEE on the SCREEN. I did NOT ask for what you THINK to have happened. That is, tell me what button get displayed on the screen, what dialogue did you see after you click one of the button, then what button do you see on the screen again.
You think that the else statement is run after the if statement. I think that it is more likely that it does not happened, you just made the wrong assumption and what that looks like the else statement being run is actually something else (most likely the if statement being looped back again).
So here is a few points you would want to check out:
1. What is player_mana before the battle start? Make sure that it is at least 6. If it is 5 or less, then the else block is indistinguishable from the if block as far as what is on the screen is concerned. Once the battle start, before clicking on any menu button, press Shift+D and select Variable Viewer to see what the value of player_mana is.
2. You have NO code for enemy attack here, yet you seems to claim to have one. Add in script for enemy attack at the end of the loop and see what happen. Without it (or something random as long as it mark the end of the loop iteration), you literally cannot tell whether a new iteration of the loop have started or not.
It seems forgot the enemy code in the trim, which is weird because I'm pretty sure it was there.
Player_mana is defined beforehand on another script and is displayed using screens and even totally removing it does nothing to change the issue. Having both menus identical does the same thing, if followed by else followed by attack.

Code: Select all

define r = Character('[charname]', color="#CD0000")
define w = Character('Enemy', color="#B5B5B5")

label areafight1:
    show screen charnameshow   
    show screen charstatsshow
    show screen charhealthandmana
    
    $ enemy_max_hp = 60
    $ player_max_hp = vitality
    $ enemy_hp = enemy_max_hp
    $ player_hp = player_max_hp
    $ enemy = "Enemy"   
    "An Enemy attacks you!"



label coastbattle1:
    
    while (enemy_hp > 0) and (player_hp > 0):
            
        if charclass == "Magicuser":
            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                    
                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away." 
                        jump checkup
                    if fleechance <= 4: 
                        "You failed to run."
                   
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    r "That's a little better"

                    
                "Spell - Magic" if player_mana > 5:
                    $ player_damage = renpy.random.randint(magicdamagelow, magicdamagehigh)
                    $ player_mana -= 5
                    r "Describe magic effect here *[player_damage]*" 
                    "([enemy] has [enemy_hp] HP.)"
                    
        else:
            menu:
                "Attack":
                    $ player_damage = renpy.random.randint(damagelow, damagehigh)
                    $ enemy_hp -= player_damage
                    r "You attack [enemy]! *[player_damage]*"
                    "([enemy] has [enemy_hp] HP.)"
                    
                "Rest":
                    $ player_hp = min(player_hp+5, player_max_hp)
                    $ player_mana = min(player_mana+5, player_max_mana)
                    r "That's a little better"
                    
                "Run":
                    $ fleechance = renpy.random.randint(1, 10)
                    if fleechance >= 5:
                        "You successfully ran away." 
                        jump checkup
                    if fleechance <= 4: 
                        "You failed to run."
                                
        if enemy_hp <= 0:
            "[enemy] has died."
            $ experiencecurrent = experiencecurrent+50 
                
        if enemy_hp >= 0:
            $ enemy_damage = renpy.random.randint(1, 6)
            
            if enemy_damage > 3:
                $ player_hp -= enemy_damage
                w "[enemy] hits for high damage. *[enemy_damage]*"
                
            if enemy_damage == 3:
                $ player_hp -= enemy_damage
                w "[enemy] hits you. *[enemy_damage]*"
            
            if enemy_damage < 3:
                $ player_hp -= enemy_damage
                w "[enemy] hits for low damage. *[enemy_damage]*"
            
       
       
       
    hide screen simple_stats_screen
           
    if enemy_hp <=0:
        jump checkup
        
   
    else:
        "[enemy] defeats you"
        $ player_hp = 0
        
jump checkup

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#10 Post by Elmiwisa »

Sigh...I guess I would just ask you for the log...
Add this to the end of options.rpy:

Code: Select all

init python:
    config.log="battlelog.txt"
Save and run the game. Once finished, look into your Ren'Py folder (NOT your project folder, but Ren'Py folder which is the place with renpy.exe) and search for battlelog.txt. Attach that file here.

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

Re: Ren'py is using both the if and else statement. Help?

#11 Post by Mehmed »

--- Mon Sep 09 11:39:09 2013

YHNIWYAD

A pile of living grass attacks you!

Choice: Attack
Choice: Rest
Choice: Run
Choice: Spell - Nerve Crush

User chose: Spell - Nerve Crush

YHNIWYAD
Grass Hermit spasms in pain for a moment. *36*

(Grass Hermit has 15 HP.)

Choice: Attack
Choice: Rest
Choice: Run

User chose: Attack

YHNIWYAD
You attack Grass Hermit! *5*

(Grass Hermit has 10 HP.)

Enemy
Grass Hermithits you. *1*

Choice: Attack
Choice: Rest
Choice: Run
Choice: Spell - Nerve Crush

User chose: Spell - Nerve Crush

YHNIWYAD
Grass Hermit spasms in pain for a moment. *46*

(Grass Hermit has 10 HP.)

Choice: Attack
Choice: Rest
Choice: Run

User chose: Attack

YHNIWYAD
You attack Grass Hermit! *6*

(Grass Hermit has 4 HP.)

Enemy
Grass Hermit hits you. *2*

Choice: Attack
Choice: Rest
Choice: Run
Choice: Spell - Nerve Crush

User chose: Spell - Nerve Crush

YHNIWYAD
Grass Hermit spasms in pain for a moment. *22*

(Grass Hermit has 4 HP.)

Choice: Attack
Choice: Rest
Choice: Run

User chose: Attack

YHNIWYAD
You attack Grass Hermit! *7*

(Grass Hermit has -3 HP.)

Grass Hermit has died.

Level Up!

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#12 Post by Elmiwisa »

Hmmm...how about you send me your whole project, if you don't mind? Because I already run the code you posted here, and there is nothing wrong with it (of course, I had to make a few minor modification to make it work, such as removing all the screen code and declare certain variables).

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

Re: Ren'py is using both the if and else statement. Help?

#13 Post by Mehmed »

Elmiwisa wrote:Hmmm...how about you send me your whole project, if you don't mind? Because I already run the code you posted here, and there is nothing wrong with it (of course, I had to make a few minor modification to make it work, such as removing all the screen code and declare certain variables).
I mind, sorry. It's just under 1GB and there's no way I'm uploading that just to fix this. It took long enough to upload back when it was at 400MB. I'll continue to fiddle with it and see if I can find exactly what is causing it, since it was able to work for you while using the solitary code that means I have a good lead on what to do. I also just now noticed the if statements don't actually do any damage. They just display the text and the else statement is what leads to an actual action other than losing mana, which has given me a pretty solid idea on what might be the matter. I don't know exactly how to fix it now but I have a good idea of how to tweak it until it maybe falls into working order.
If I find out exactly what was wrong and how to fix it I'll edit the OP for others who had similar issues.

Words would do no justice to how grateful I truly am for you trying to help. This has been a thorn in my side for the past few days and this is the furthest I've gotten with fixing it.

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Ren'py is using both the if and else statement. Help?

#14 Post by Elmiwisa »

How about you just send me the script files (.rpy file) then? I can just remove the part that require image/sound/movie to run and test the rest. The code is only a few KB at most.
And no the reason why the "if" part does not deal damage is because you forgot to add the code to subtract enemy HP in your Spell menu, so I don't think it is related to the problem.

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

Re: Ren'py is using both the if and else statement. Help?

#15 Post by Mehmed »

Elmiwisa wrote:How about you just send me the script files (.rpy file) then? I can just remove the part that require image/sound/movie to run and test the rest. The code is only a few KB at most.
And no the reason why the "if" part does not deal damage is because you forgot to add the code to subtract enemy HP in your Spell menu, so I don't think it is related to the problem.
No need I found the exact problem to be the while statement and the indentation between the enemy damage and the if/else statements.

I indented those lines further and it works like a charm for me now. Fixed the enemy hp too I do not know how I've missed that for so long.

Post Reply

Who is online

Users browsing this forum: No registered users