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.
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)
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
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)
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).
# 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."
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:
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.