[Solved]Can you change attributes in a duplicated class object independently?

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
Cazad0ra
Newbie
Posts: 22
Joined: Wed May 19, 2021 3:53 pm
Contact:

[Solved]Can you change attributes in a duplicated class object independently?

#1 Post by Cazad0ra »

Hello! I'm not sure if I'm going to far with this but it's always worth to ask!
I'm working on a combat system and the last thing I need to polish the structure is the buff and debuffing system, for example, poison effects.
So far it works neatly, except for the fact that if two characters got the same debuff at different turns, the duration will be shared, to explain better:
poison duration = 3 turns
character 1 got poisoned in turn 1
character 2 got poisoned in turn 2
in turn 2, both of them have a shared duration of 2 turns of poison
I figure it's because I'm changing the "remaining turn attribute" of the debuff, and it's not an independ, so my question is if there's a way to have duplicates that can be changed separately, or if there's a better way to manage these. :D

To avoid walls of code I'll summarize how the basics work. Characters have an attribute, dot, which contains a list with the buffs and debuffs applied to them, and the code below is aiming to them.

Here are the pieces of code regarding this part:

Debuff class

Code: Select all

class DoT(object):
        def __init__(self, name, img, desc, turns, damage, heal, prot, type, damagetotal=0, healtotal=0, prottotal=0):
            self.name = name
            self.img = img
            self.desc = desc
            self.turns = turns
            self.damage = damage
            self.heal = heal
            self.prot = prot
            self.type = type
            self.damagetotal = damagetotal
Code to change the debuff after each turn:

Code: Select all

def DotEffect(self):
            global party                             #   Your team
            global enemy_list				#  Enemy team
            
            for member in party:
                if member.dot != []:
                    for item in member.dot:
                        if item.turns < 0 and item.prot == 0:
                            member.dot.remove(item)

                        if item.turns > 0 and item.prot == 0:                      ##    Protection buffs last til the character is hit
                            item.turns -= 1                                                          ##<------- Here's where I modify the turns
                            self.healingraw = member.max_hp*item.heal
                            self.healingdone = int(self.healingraw)
                            member.addHP(self.healingdone)
                            self.damageraw = member.max_hp*item.damage
                            self.damagedone = int(self.damageraw)
                            member.reduceHP(self.damagedone)
                           

                        if item.turns == 0 and item.prot == 0:
                            item.turns -= 1
                            self.healingraw = member.max_hp*item.heal
                            self.healingdone = int(self.healingraw)
                            member.addHP(self.healingdone)
                            self.damageraw = member.max_hp*item.damage
                            self.damagedone = int(self.damageraw)
                            member.reduceHP(self.damagedone)
                            member.dot.remove(item)
                            
Last edited by Cazad0ra on Mon Jun 28, 2021 8:30 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Can you change attributes in a duplicated class object independently?

#2 Post by philat »

You need to either 1. create a new instance each time the debuff is applied (i.e., member.dot.append(DoT(args)) ) or 2. if you have something like poison = DoT(args) and you're doing member.dot.append(poison), you need to deep copy that predefined instance. Googling deep copy python will turn up a bunch of tutorials on the difference between shallow and deep copies, which will do a better job explaining the concept than I would.

Cazad0ra
Newbie
Posts: 22
Joined: Wed May 19, 2021 3:53 pm
Contact:

Re: Can you change attributes in a duplicated class object independently?

#3 Post by Cazad0ra »

Doing a deep copy worked indeed!!! Thanks so much!!
I found this very clear and simple explanation on how it works and how to use it, if anyone else is interested!
https://www.educative.io/edpresso/how-t ... -in-python

Post Reply

Who is online

Users browsing this forum: No registered users