Randomizer don't work properly.

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
Pankrazius
Newbie
Posts: 5
Joined: Wed Oct 08, 2014 3:35 pm
Projects: Torwelten - Karil (Promo-Game)
Deviantart: Pankrazius
Location: Bavaria
Contact:

Randomizer don't work properly.

#1 Post by Pankrazius »

Tried to use the randomizer (renpy.random.randint).

Inside a label it just works fine, returns randomized results and so on.

But turning the label into a defined function (putting it in an python-block, using def instead of label) seams to kill the randomizer, returning the same result over and over again.

Here's the code of the not working function.

Code: Select all

    def GetIni(angreifer, ziel):
        random.seed()
        rndbase = (angreifer.bew + ziel.bew)
        rnd = random.randint(1,rndbase)
       
        
        if rnd < angreifer.bew:
            return(True)
        elif rnd > angreifer.bew:
            return(False)
        else:
            return(True)

here' s the label i used to develop the function.

Code: Select all

label GetIni(angreifer, ziel):
        
        $ rndbase = (angreifer.bew + ziel.bew)
        $ rnd = random.randint(1,rndbase)
       
        
        if rnd < angreifer.bew:
            return(True)
        elif rnd > angreifer.bew:
            return(False)
        else:
            return(True)
maybe i wrote something wrong so the randomizer got stuck or something.
For further tests here the classes and used objects and the code used complete.

Code: Select all

init python:
    import random
    
       
    class Actor(object):
        
        """
        Basisdaten für Charaktere. XP, Geld.
        
        self.xp             = Erfahrungspunkte (aktuell verfügbar)
        self.sxp            = Ausgegebene EP
        self.money          = Geld
        """
        
        def __init__(self=0, name="", xp=0, sxp=0, money=0, hp=3, hpmax=3, bew=10, atat=3, atskill=0, dmg=1, ):
            self.name = name
            self.xp = xp
            self.sxp = sxp
            self.money = money
            self.hp = hp
            self.hpmax = hpmax
            self.bew = bew
            self.atat = atat
            self.atskill = atskill
            self.dmg = dmg

define player = Actor("Spieler", 300, 0, 1000, 5, 5, 10, 5, 2, 2)
define soeldner = Actor ("Söldner", 0, 0 ,1000, 5, 5, 10, 5, 2, 2)


label start:
    
    call Kampf(soeldner)
    
    if _return == True:
        "Du hast gewonnen."
    else:
        "Du bist tot."
        
    menu:
        "Nochmal":
            jump start
        "Ende":
            return
    
    return
    
    
label Kampf(enemy):
    
    "EnemyAtackAttribut [enemy.atat], Enemy AtackSkill [enemy.atskill]"
    
    $ GetIni(player, enemy)
    
    if _return == True:
        "Du bist schneller"
        $ eins = player
        $ zwei = enemy
    else:
        "Der Söldner ist schneller."
        $ eins = enemy
        $ zwei = player

    call GetHit(eins, zwei)
    
    if _return == True:
        "[eins.name] hat getroffen!"

"Kampf abgehandelt"
return (True)
    
label GetHit(angreifer, ziel):
    
    "1. AtackAttribut [angreifer.atat]; 1. AtackAttribut [ziel.atat]"
    
    return (True)

This code should allow to repeatly use the GetIni-Function. To use the GetIni-label jus comment out the function and set call instead of the $ in the first codeline within the Kampf-Label.

apricotorange
Veteran
Posts: 479
Joined: Tue Jun 05, 2012 2:01 am
Contact:

Re: Randomizer don't work properly.

#2 Post by apricotorange »

The Ren'Py "return" statement sets _return. The Python "return" statement does not; if you don't store the result of a function call into variable, it just disappears.

Code: Select all

# In Ren'Py code, check whether the attack hit.
$ hit = GetIni(player, enemy)
if hit:
    "Du bist schneller"


# get a random number
$ rnd = random.randint(1,rndbase)
On a side note, you almost never want to call random.seed(); just delete that line.

Post Reply

Who is online

Users browsing this forum: Google [Bot]