randomnum.renpy.randomint locks in option, leading to a loop

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
facadepapergirl
Regular
Posts: 46
Joined: Thu Jan 31, 2013 7:27 pm
Projects: Pokemon: Final Evolution; Galactic Lust Crisis
Contact:

randomnum.renpy.randomint locks in option, leading to a loop

#1 Post by facadepapergirl »

I'm using randomint for a battle scene, so that whether or not you hit is random. However, I've found that once a random integer has been chosen, when it goes back to that label, the integer has been set at that and stays that way. This causes a problem in battle, because of the character misses, and the opponent misses, this continues indefinately, since the battle ends only when one of the participants' HP is less than one. There are other similar problems, such as one side always attacking and the other always missing.

In another instance, randomint makes travel difficult (or easy). When you travel somewhere in my game, randomint decides if you have an encounter when you get there. One such encounter is the aforementioned battle. But when I ran the game, it chose the option that gets you to your destination safely. That's fine and all, but when I choose the option to leave (by returning to the home label) and try and come back (going to the location menu and choosing the option that I chose earlier), I got the same encounter (lack of, rather). No matter how often I go through it, even if I save after going through it once, closing the game, opening it again and loading the file, the randomint is the same every time. If I start a whole new game, the randomint runs correctly the first time and sticks to that integer every time after on that playthrough (or if I saved and continued later).

As you can see, I rely on randomint a lot for this game, but I need it to be random every time I get to that label. I will try to add a code of the travelling part in a bit; I have to change devices to copy and paste the code. This is the first time I had coded something (I'm referring to the battle) using what I've learned in ren'ppy, instead of adapting something I read off of a thread for my use, so I'm eager to make it work. Thank you for any help that you may offer, even if it's a suggestion to do something completely different.

jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Re: randomnum.renpy.randomint locks in option, leading to a

#2 Post by jw2pfd »

The random number is determined when the function is called:

Code: Select all

    $ num = renpy.random.randint(1,10)
For example, this stores a random integer between 1 and 10 into the variable 'num'. The value that 'num' holds will not change until you store another value into it. Are you only assigning a value once near the start of the game and then never again? If you want to store a new random value into a variable, then you will have to call the renpy.random.randint function again. If this isn't the problem, then I might be able to help you after seeing some code when you get the chance.

In case what I mentioned is the issue, these examples might help. This example illustrates that the values won't change if you never assign new values.

Code: Select all

    $ num1 = renpy.random.randint(1,10)   #num1 is assigned a value on this statement only
    $ num2 = renpy.random.randint(1,10)   #num2 is assigned a value on this statement only

label again:

    $ answer = num1 + num2  #this will always evaluate to the same answer because num1 and num2 will never hold new values

    "[answer]"  #this will display the same answer over and over again

    jump again
This example shows that by having the randint statement run again, then we potentially get new values for num1 and num2. It is possible to get the same value due to the nature of it being random:

Code: Select all

label again:
    $ num1 = renpy.random.randint(1,10)   #num1 is assigned a value each time it loops
    $ num2 = renpy.random.randint(1,10)   #num2 is assigned a value each time it loops

    $ answer = num1 + num2  #this will evaluate to different answers because num1 and num2 will get assigned a random value each time the loop runs

    "[answer]"  #this will display varying values each time it loops

    jump again

User avatar
facadepapergirl
Regular
Posts: 46
Joined: Thu Jan 31, 2013 7:27 pm
Projects: Pokemon: Final Evolution; Galactic Lust Crisis
Contact:

Re: randomnum.renpy.randomint locks in option, leading to a

#3 Post by facadepapergirl »

Here is the code for the traveling bit.

Code: Select all

label outside:
    "Insert description."
    "Some more description."
    menu:
        "Watch out for them, as you head to..."
        
        "The dock":
            jump dock_m
        "The Market":
            jump market_m
        "The Lab":
            jump lab_m
            
label dock_m:
    $ randomnum = renpy.random.randint(1,3)
    if randomnum==1:
        "You run into a couple of cephaloids, ready to fight."
        call twocephiloidfight
        jump dock
    elif randomnum==2:
        "You make it to the dock without running into anyone."
        jump dock
    elif randomnum==3:
        "You run into a male furry canid."
        jump dock
        
label dock:
    "At the dock, you can see that it is largely empty. The room is lined on one side with ports, to which ships would normally attach themselves. Though there are many ports, only a very few tend to get occupied here."
    menu:
        "As you look around, you see..."
        "Nothing, you're not looking. You don't know why you're here, you decide to go back.":
            jump home2
        "The Boy Toy. Though the name seems silly, it is feared as a famous pirate ship.":
            jump tbtx
        "81662, one of the ferry ship that travels from boonies like Oristina to Aglmeck, the nearest interplanetary space station.":
            jump ferry
I'm still rather new to this, trying to learn by doing. Every time I used the random function, it was like above, through the number of integers differed. From what my noobish mind understands, I should add them? I have the functioned defined after the start of the label, so my use of the function is more similar to example one than example two. If you further explain it, I appreciate it. The battle scene is done in much the same way, using renpy.random.randint in the same fashion. Here, an excerpt from the large code, if their dexterity is high enough to warrant a higher chance of success:

Code: Select all

label hitfast:
    $ randomnum = renpy.random.randint(1,5)
    if randomnum==1:
        "Yes! You hit one!"
        jump damage
    elif randomnum==2:
        "You managed to hit one!"
        jump damage
    elif randomnum==3:
        "Your dexterity enabled you to hit one of them!"
        jump damage
    elif randomnum==4:
        "You're too slow, and they dodged your attack!"
        jump cephy1cntatk
    elif randomnum==5:
        "You missed!"
        jump cephy1cntatk

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: randomnum.renpy.randomint locks in option, leading to a

#4 Post by PyTom »

It should be locking things in on rollback, but not on a simple loop.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Re: randomnum.renpy.randomint locks in option, leading to a

#5 Post by jw2pfd »

A post has been made since I had started typing, but I'll go ahead and post anyway. Your code sections appear to have correct placement of the variable assignment statement. As far as I can tell, your pasted sections of code should not be giving you the same "random" value indefinitely. If you are using rollback to test for new random values, then I can tell you that rolling back will not produce new random values when you click forward again. Although, you did describe having the issue clicking through your menus.

One tool you can use to look at the value that a variable holds is by pressing Shift+D while the game is running and then clicking 'variable viewer'. You can check the value there to see if 'randomnum' is staying the same. This may or may not help determine if something weird is going on.

User avatar
facadepapergirl
Regular
Posts: 46
Joined: Thu Jan 31, 2013 7:27 pm
Projects: Pokemon: Final Evolution; Galactic Lust Crisis
Contact:

Re: randomnum.renpy.randomint locks in option, leading to a

#6 Post by facadepapergirl »

If I chose to go to the dock, then chose to go back (which sends me home, I'd have to chose to go outside again), then chose dock again, I'd get the same result each time. I've tested it again to make sure that coincidence wasn't the problem, but five out of five times, I'd get only one of the choices each time I played through it.

*EDIT* Randomnum is staying the same. Right now, it is 3.
Last edited by facadepapergirl on Mon Aug 19, 2013 4:26 pm, edited 1 time in total.

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: randomnum.renpy.randomint locks in option, leading to a

#7 Post by Dragonstar89 »

I've had the same problem with a battle code I'm testing in a separate project that will (when successful) will be inserted into the game. Only, mine will not generate random battles, I will have battles set at certain locations. Anyways, here's the script to what I'm doing:

Code: Select all

label start:

    $m_exp = 0

    "Let's head into battle!"

    # Create skills (name, hit, power)
    $Slash = Skill("Slash", 70, 20)
    $Fire_Vein = Skill("Fire Vein", 35, 35)
    # Create battle actors (name, max_hp, stats, skills)
    $player = Actor("Hero", 1, 5, 5, 5, 100, [Slash, Fire_Vein])
    $Guard = Actor("Guard",  1, 5, 5, 5, 100, [Slash])
    
    jump battle

label afterbattle:

    if m_exp >=39:
        $ player.levelup()
        "Congratulations! You now have %(m_exp)d EXP points, and have leveld up to level 2."
        return
    else:
        "Let's do another battle, shall we?"
        jump battle_2


label battle:
    show g normal at right with dissolve
    show m normal at left with dissolve

    $ enemy= Guard
    show screen battle_ui
    "[enemy.name] appeared"
    while enemy.hp>0:
        $ player.command(enemy)
        if player.hp <1:
            "gameover"
            $ config.rollback_enabled = True
    $m_exp +=20
    "You win"
    hide screen battle_ui
    jump afterbattle

label battle_2:
    show g normal at left with dissolve
    show m normal at right with dissolve

    $ enemy= Guard
    show screen battle_ui
    "[enemy.name] appeared"
    while enemy.hp>0:
        $ player.command(enemy)
        if player.hp <1:
            "gameover"
            $ config.rollback_enabled = True
    $m_exp +=20
    "You win"
    hide screen battle_ui
    jump afterbattle

# Using Python to define the Skill variables and instances

init -1 python: 
    # Class used for battle skills
    class Skill():
        def __init__(self, name, hit, power):
            self.name = name
            self.hit = hit
            self.power = power            
            
    # Class used for battle characters. Inherit renpy.store.object for the rollback function.
    class Actor(renpy.store.object):
        def __init__(self, name, lvl, str, dex, int, max_hp, skills=[]):
            self.name=name
            self.str = str
            self.dex = dex
            self.int = int
            self.lvl = lvl
            self.max_hp = 100
            self.hp = max_hp
            self.skills = skills

        def levelup(self):
            self.lvl += 1
            self.str += 1
            self.dex += 1
            self.int += 1

        def command(self, target):
            self.skill = renpy.call_screen("command")
            target.skill = renpy.random.choice(target.skills)
            self.attack(self.skill, target)
            if target.hp < 1:
                return
            target.attack(target.skill, self)
            
        def attack(self,skill,target):
            if self.skill.hit<renpy.random.randint (0,100):
                narrator ("{} dodged {}'s attack".format(target.name,self.name))
            else:
                target.hp -= self.skill.power
                narrator ("{} got {} damage".format(target.name, self.skill.power))

# Using screen lang of Renpy to create the buttons for attacks

init:
    # Screen used for selecting skills
    screen command:    
        vbox align (.5,.5):
            for i in player.skills:
                textbutton "[i.name]" action Return (value=i)

    # Screen which shows battle status
    screen battle_ui:    
        use battle_frame(char=player, position=(.05,.05))
        use battle_frame(char=enemy, position=(.95,.05))
        
    screen battle_frame:
        frame area (0, 0, 180, 80) align position:
            vbox yfill True:
                text "[char.name]"
                hbox xfill True:
                    text "HP"
                    text "[char.hp]/[char.max_hp]" xalign 1.0
First off, when I loose, I stay on the first battle label, but that's because I have the rollback configured and the battle label is the last one my character interacted with before loosing all HP. Now, here's my problem. This script is to test leveling up, after you battle and you do not have enough EXP yet, you will battle again then get the correct EXP to level up. The problem is, in 'label battle' if I win, it will say normally 'Shall we have another battle?' and then it jumps to label battle_2 like instructed.

Yet, in battle two, the character I play as (Hero) and the enemy still have the same exact HP from last time, and since I won the first battle, I win the second one instantly because the 'Guard's' HP was still set as zero, though it was in a completely different label. Is this a problem with the renpy.store.object? If so, how can I make it so it resets, that way the next time my party battles the Guard actor, they have 100 HP instead of 0?
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]