Deleting from string in which data was written though the class

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
Ultralisk
Newbie
Posts: 4
Joined: Sat Apr 03, 2021 11:55 am
Contact:

Deleting from string in which data was written though the class

#1 Post by Ultralisk »

Hello everyone!
I have a project, there is Class, which has a function new hero, which adds a new hero to the characters screen. I can call new_hero and add a new hero to heroes_full_list and it works. The problem is that I can't delete the hero. I tried using the command "$ heroes_full_list.remove(0)" but I get an error. I think it's because I'm using a class. Please tell me how I can remove a previously added hero from the list heroes_full_list? (it was added via def new_hero which written in a class)

Code: Select all

heroes = []
heroes_full_list = []
avatars_list = []

class Hero():
def new_hero(name, avatar):
 	_hero = Hero(name=name, avatar=avatar)
        store.heroes_full_list.append(_hero)
        store.heroes.append(_hero)
        renpy.restart_interaction()



# adding new hero:
$ new_hero('Jack','jack_base')


And here is the error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/mylabels.rpy", line 1587, in script call
    call fvadminevent from _call_fvadminevent
  File "game/mylabels.rpy", line 1767, in script call
    call svpirateevent from _call_svpirateevent
  File "game/mylabels.rpy", line 115, in script call
    call mushroomsfinishnextdayevent3 from _call_mushroomsfinishnextdayevent3
  File "game/mylabels.rpy", line 125, in script call
    call finaldayevent2 from _call_finaldayevent2
  File "game/events.rpy", line 1957, in script
    $ heroes_full_list.remove(0)
  File "game/events.rpy", line 1957, in <module>
    $ heroes_full_list.remove(0)
ValueError: list.remove(x): x not in list

Windows-10-10.0.19041
Ren'Py 7.4.11.2266

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1118
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Deleting from string in which data was written though the class

#2 Post by m_from_space »

Ultralisk wrote: Sun Nov 19, 2023 12:14 pm Hello everyone!
I have a project, there is Class, which has a function new hero, which adds a new hero to the characters screen. I can call new_hero and add a new hero to heroes_full_list and it works. The problem is that I can't delete the hero. I tried using the command "$ heroes_full_list.remove(0)" but I get an error. I think it's because I'm using a class. Please tell me how I can remove a previously added hero from the list heroes_full_list? (it was added via def new_hero which written in a class)

Code: Select all

heroes = []
heroes_full_list = []
avatars_list = []

class Hero():
def new_hero(name, avatar):
 	_hero = Hero(name=name, avatar=avatar)
        store.heroes_full_list.append(_hero)
        store.heroes.append(_hero)
        renpy.restart_interaction()



# adding new hero:
$ new_hero('Jack','jack_base')


To be honest, your code doesn't make a lot of sense and is just wrong. You should read about how classes are defined and used in the first place. You usually don't define a new class object by calling a function (at least not if that function is part of the class). You said that "new_hero" is part of the class, but I don't think it really is, otherwise you couldn't call it like you showed in your code. It's also missing a reference to itself. Or did you just leave out entire code blocks that are relevant? Because I don't see any class constructor etc.; You really don't need a "new_hero" method, it also leads to you not having a reference to the heroes. Then again list.remove(x) needs x to be the thing you want to remove, not some index like 0. Maybe you want to use list.pop(x) instead, then x is the index. But I hope you always know what hero is at what position (I don't think you will).

So here is a primitive suggestion:

Code: Select all

# use Renpy's default keywords to create variables!
default heroes = []
default heroes_full_list = []
default avatar_list = []

init python:
    class Hero:
        # the class constructor
        def __init__(self, name, avatar):
            self.name = name
            self.avatar = avatar
            self.add_to_list()
            return
        def add_to_list(self):
            store.heroes.append(self)
            store.heroes_full_list.append(self)
            # i don't know why you do that, I guess you want to redraw screens?
            renpy.restart_interaction()
            return
        def remove_from_list(self):
            if self in store.heroes_full_list:
                store.heroes_full_list.remove(self)
            return

label start:
    # create a new hero, this will call the constructor and also add it to the list
    $ some_hero = Hero("Jack", "jack_base")
    "So we just created a hero named [some_hero.name], now let's remove it from the list again."
    $ some_hero.remove_from_list()
    "We removed the hero from the list, but the object <some_hero> is still available if we ever want to add it again."
    $ some_hero.add_to_list()
    "Fin."

Ultralisk
Newbie
Posts: 4
Joined: Sat Apr 03, 2021 11:55 am
Contact:

Re: Deleting from string in which data was written though the class

#3 Post by Ultralisk »


To be honest, your code doesn't make a lot of sense and is just wrong. You should read about how classes are defined and used in the first place. You usually don't define a new class object by calling a function (at least not if that function is part of the class). You said that "new_hero" is part of the class, but I don't think it really is, otherwise you couldn't call it like you showed in your code. It's also missing a reference to itself. Or did you just leave out entire code blocks that are relevant? Because I don't see any class constructor etc.; You really don't need a "new_hero" method, it also leads to you not having a reference to the heroes. Then again list.remove(x) needs x to be the thing you want to remove, not some index like 0. Maybe you want to use list.pop(x) instead, then x is the index. But I hope you always know what hero is at what position (I don't think you will).
Thank you! The idea with .pop is working. Now I'm adding a hero to special list before remove it with this command (to save him there with all his experience), if I need him again I can move him to heroes_full_list again:

Code: Select all


jack_vacation = []

def jackaddvacation(name):
    for obj in heroes_full_list:
        if obj.name == "Jack":
            jack_vacation.append(obj)


def remove_hero(name):
    _hero = Hero(name=name, avatar=avatar)
    store.heroes_full_list.pop()
    store.heroes.pop()
    renpy.restart_interaction()
    
I can call this defs like this:

Code: Select all

$ jackaddvacation('Jack')
$ heroes_full_list.pop(0)
and later I can add Jack again!
Just

Code: Select all

$ heroes_full_list.append(jack_vacation[0])
Thank you again!

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1118
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Deleting from string in which data was written though the class

#4 Post by m_from_space »

Ultralisk wrote: Mon Nov 20, 2023 6:58 pm

Code: Select all

def remove_hero(name):
    _hero = Hero(name=name, avatar=avatar)
    store.heroes_full_list.pop()
    store.heroes.pop()
    renpy.restart_interaction()
    
No way this is going to work as intended. You're creating a new Hero object with some name and then just pop it again, since it was appended to the list. What's the point? If you want to remove objects from lists, you have to have a reference to the object. Creating a new object that just uses the same internal variable "name", doesn't work. In your code you also pass a variable called "avatar" which isn't even known. This all will lead to confusion and error I can guarantee that.

Please try to understand the code I provided.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot]