[SOLVED]How to check if variable is in self.list

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:

[SOLVED]How to check if variable is in self.list

#1 Post by Nero »

I was wondering if there is a way I could check if self.list (self.status) holds variable poison. I tried doing it this way but it doesn't work... Any ideas how to fix this problem?

Code: Select all

init python:
    class Player(object):
        def __init__(self,name):
            self.name = name
            self.status = []

        def add_spell(self, spell):
            from copy import deepcopy 
            self.status.append(deepcopy(spell))
            if spell in self.status: # Problematic line
                renpy.say(None, "Works")
            else:
                renpy.say(None, "Error Doesn't work")


init python:
    class Debuff(object):
        def __init__(self, name="", damage=0,duration=0, picture="Picture",active=False):
            self.name = name

default poison = Debuff(name="Poison")
default player = Player(name="Player")





label start:
    "Test"
    $ player.add_spell(poison)
    "Test"
    "Test"
    "Test"
Last edited by Nero on Sun Dec 16, 2018 10:15 am, edited 1 time in total.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: How to check if variable is in self.list

#2 Post by Per K Grok »

Nero wrote: Sun Dec 16, 2018 12:26 am I was wondering if there is a way I could check if self.list (self.status) holds variable poison. I tried doing it this way but it doesn't work... Any ideas how to fix this problem?

Code: Select all

init python:
    class Player(object):
        def __init__(self,name):
            self.name = name
            self.status = []

        def add_spell(self, spell):
            from copy import deepcopy 
            self.status.append(deepcopy(spell))
            if spell in self.status: # Problematic line
                renpy.say(None, "Works")
            else:
                renpy.say(None, "Error Doesn't work")


init python:
    class Debuff(object):
        def __init__(self, name="", damage=0,duration=0, picture="Picture",active=False):
            self.name = name

default poison = Debuff(name="Poison")
default player = Player(name="Player")





label start:
    "Test"
    $ player.add_spell(poison)
    "Test"
    "Test"
    "Test"

If you replace

Code: Select all

            from copy import deepcopy 
            self.status.append(deepcopy(spell))
with

Code: Select all

            self.status.append(spell)
the script seem to work.

The problem is not checking the list, but with appending the variable.

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

Re: How to check if variable is in self.list

#3 Post by Nero »

Yeah I know tried it myself but you have idea how to work around it with deepcopy?

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: How to check if variable is in self.list

#4 Post by Per K Grok »

Nero wrote: Sun Dec 16, 2018 7:08 am Yeah I know tried it myself but you have idea how to work around it with deepcopy?
I don't really know what deepcopy does, but apparently deepcopy(spell) is not the same as spell, so if you check for spell in the list you will get a miss.

I tried to add a new variable to hold deepcopy(spell) and then check for that variable. That seemed to work.

abra=deepcopy(spell)
self.status.append(abra)
if abra in self.status:

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

Re: How to check if variable is in self.list

#5 Post by Nero »

In my case it does that for example if I have list with 5 (spells) in it and I declared each spell have duration time in class Debuff .. so if I create function that will run every turn (turn based game) and function will do duration -= 1 every time it loops ... if I dont use deepcopy the cooldown duration will be decreased by 5(because 5 spells are in list) instead of 1 so that's not what I want and deepcopy corrects this bug.

And mysteriously this actually works very well what you did, sadly I don't see much logic behind it I guess deepcopy creates something in variable name if appended through it. Anyways thanks for your help!

Code: Select all

        def add_status(self, spell):
            from copy import deepcopy #import deepcopy so we can append a new object with the same properties
            spell=deepcopy(spell)
            self.status.append(spell)
            if spell in self.status:
                renpy.say(None, "Works")

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]