While Loop Causing Game To Freeze

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
Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

While Loop Causing Game To Freeze

#1 Post by Nero »

Im doing battle system inside of python however game freezes after i try to run my code.

When I do exactly same thing while in renpy scrip game seems to run fine. Where is the problem?

Code: Select all

init python:
    class Battle(object):
        def __init__(self,characters):
            self.characters = characters

        @staticmethod
        def character_status(characters): # Method used to check if character has more than 0 hp
            for p in characters:
                if p.hp > 0:
                    return "a"
            return "d"

        @staticmethod
        def mainBattle():
            x = 0 # I defined the numeric order in which every team member has its number from 0 to (number of player in list)
            while x < len(team_members): # Every player in team_members needs to have his turn when it's over we jump to a next code
                cp = team_members[x] # Here we define who is the current player
                if cp.hp > 0: # If current player hp is 0 check if everyone else is alive in party if not jump to label
                    if Battle.character_status(team_members) == "d":
                        renpy.jump("game_over")

            global player_turn
            player_turn = True
            target = ui.interact() # Selected enemy will be memorised and get attacked by current player
            player_turn = False
            cp.attack(enemy_members[target]) 
            x += 1 # After this player attacked loop this code again until all players had their turn

label start:
    "Game Starts"
    "Game freezes in infinite loop causing it to crash!"
    $ mainBattle()

User avatar
Enchant00
Regular
Posts: 136
Joined: Tue Jan 12, 2016 1:17 am
Contact:

Re: While Loop Causing Game To Freeze

#2 Post by Enchant00 »

Hmm did you create an object for your class Battle then call said function from referenced object?

It might also be a problem with how renpy deals with function calls. Try declaring your variable x outside your function and referencing it in your function as global .

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: While Loop Causing Game To Freeze

#3 Post by Nero »

Yes I did however I made typing mistake when creating example. Thats how I called it if that's what you meant:

Code: Select all

$ Battle.mainBattle()
Tried making x = 0 outside the init python declared global x in within the method still same problem :/

DannX
Regular
Posts: 99
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: While Loop Causing Game To Freeze

#4 Post by DannX »

As far as I can see, the while loop keeps going infinitely because you're not doing anything inside the loop to change the length of team_members, nor the value of x.

When Python reaches a while loop, it stops there and executes the code inside over and over until the condition you provide it stops evaluating to True. If you do not change the values inside of the loop itself, it will never stop. If you add some print statements and some testing code like this, run it and open the console (Shift+O) you can see:

Code: Select all

#Some quick testing code
default team_members = ["Player 1", "Player 2", "Player 3"]
        def while_test():
            x = 0 
            while x < len(team_members): 
                print "I'm inside of the loop" 
                x +=1
            
            print "Loop finished"
What you may want is to move the code in the whole function below actually inside the loop.

When I do that, however, the game seems to sit there and nothing happens, probably because I don't have the whole code or because ui.interact() I've never used that function directly, instead call screen, so I'm not sure.

Nero
Veteran
Posts: 248
Joined: Tue Aug 09, 2016 2:59 pm
Contact:

Re: While Loop Causing Game To Freeze

#5 Post by Nero »

Oh damn I realised my mistake now I didn't put x value inside the while loop same as all other of my code... Thanks for correcting me now I understand. Oh and yeah it works even without declaring it in separated function it can be all done in main() function.

To answer your question yes it does nothing if you don't have textbutton or imagebutton connected to it as it waits for clicked result means it will store which player you decided to attack inside the "enemy_members" list.

Here is how it works after you press textbutton:
But if you got advice how to improve it or rework it I would gladly listen to your opinion.

Code: Select all

    vbox:
        xalign 0.99 yalign 0.05
        spacing 5
        if enemy_members != []:
            for target, e in enumerate(enemy_members):
                hbox:
                    if e.hp > 0:
                        if player_turn:
                            textbutton "Attack test" action Return(target)
                        else:
                            textbutton "Attack test"
Anyways thanks again!

Post Reply

Who is online

Users browsing this forum: Andredron, elcharlo, Google [Bot]