Rollback does not revert class variables

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
Saltome
Veteran
Posts: 237
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Rollback does not revert class variables

#1 Post by Saltome » Mon Nov 03, 2014 8:22 am

Once again, this is normal behavior, if I'm not mistaken, but what do I do about it?
Here is my specific example.
New:

Code: Select all

init python:
    class combatant:
        def __init__(self, name="", hp=1, strength=1, endurance=1, dexterity=1, agility=1):
            self.name=name
            self.hp=hp
            self.strength=strength
            self.endurance=endurance
            self.dexterity=dexterity
            self.agility=agility
define Player = "???"
label start:
    $combatants=[]
    $i=0
    while i<10:
        $combatants+=[combatant()]
        $combatants[i].name=str(i)
        $i+=1
    Player "Ah, the arena... finally. Let's see where this gets me."
    call fight(combatants[0],combatants[1])
    "Returned HP: [_return.hp]"
    return
label fight(a,b):
    $hp=b.hp
    while a.hp>0 and hp>0:
        if a.dexterity*renpy.random.random() > b.agility*renpy.random.random():
            $damage=(a.strength*renpy.random.random()-b.endurance*renpy.random.random())
            $hp-= max([damage,0])
            "Hit! [hp]"
        else:
            "Miss!"
    $b.hp=hp
    return b
Old:

Code: Select all

init python:
    
    class combatant:
        name=""
        hp=1
        strength=1
        endurance=1
        dexterity=1
        agility=1
    
    def fight(a,b):
        while a.hp>0 and b.hp>0:
            if a.dexterity*renpy.random.random() > b.agility*renpy.random.random():
                damage=(a.strength*renpy.random.random()-b.endurance*renpy.random.random())
                b.hp-= max([damage,0]) #if damage > 0 else 0
                renpy.say("", "Hit! (%F)" %b.hp)
            else:
                renpy.say("", "Miss!")

        return
label start:
    $combatants=[]
    $i=0
    while i<10:
        $combatants+=[combatant()]
        $combatants[i].name=str(i)
        $i+=1
    $result=fight(combatants[0],combatants[1])
    return
Last edited by Saltome on Mon Nov 03, 2014 11:48 am, edited 1 time in total.
Deviant Art: Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Rollback does not revert class variables

#2 Post by xela » Mon Nov 03, 2014 10:02 am

Use the __init__ "constructor"? I am not sure that your code is sensible as it is now but I could simply be misunderstanding your engine.

This can also be useful if you want/need to do it your way:

http://lemmasoft.renai.us/forums/viewto ... 51&t=27803
Like what we're doing? Support us at:
Image

User avatar
Saltome
Veteran
Posts: 237
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: Rollback does not revert class variables

#3 Post by Saltome » Mon Nov 03, 2014 11:45 am

I already tried that but it didn't seem to make a difference so I removed it.
After rewriting the function as a label I can tell that it rolls back the right instructions in the right order, so that's a relief. But the hp stays the same, and every time I roll forward(or back, into a "hit" instruction) it applies the damage again.

What seems to work is to extract the value from the class then do the calculations on the new variable instead of directly to the class object. And at the end of the label store the value back in the class variable.
I will edit the first post to show the current script in a moment.

I wonder if there is a more efficient approach tho.
Deviant Art: Image

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Rollback does not revert class variables

#4 Post by xela » Mon Nov 03, 2014 5:59 pm

Your code is still off a bit. Same thing rewritten in better script:

Code: Select all

init python:
    class Combatant(object):
        def __init__(self, name="", hp=1, strength=1, endurance=1, dexterity=1, agility=1):
            self.name = name
            self.hp = hp
            self.strength = strength
            self.endurance = endurance
            self.dexterity = dexterity
            self.agility = agility
            
define Player = "???"

label start:
    $ combatants = [Combatant(name=str(i)) for i in range(10)]
    
    Player "Ah, the arena... finally. Let's see where this gets me."
    
    call fight(combatants[0], combatants[1])
    
    "Returned HP: [_return.hp]"
    return
    
label fight(a, b):
    while a.hp>0 and b.hp>0:
        if a.dexterity*renpy.random.random() > b.agility*renpy.random.random():
            $ damage = (a.strength*renpy.random.random()-b.endurance*renpy.random.random())
            $ b.hp -= max([damage, 0])
            "Hit! [b.hp]"
        else:
            "Miss!"
    return b
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: TioNick