Home > Software engineering >  Cant pass variables within an extended View class -Discord.py Cogs
Cant pass variables within an extended View class -Discord.py Cogs

Time:02-04

        embed.add_field(name="Player 1", value=new_game['player_1']['name'], inline=True)
        embed.add_field(name="Player 2", value=new_game['player_2']['name'], inline=True)
        await ctx.send(view=view, embed=embed)
    
        class StartMatch(View):
            def __init__(self, player_1, player_2):
                self.player_1 = player_1
                self.player_2 = player_2
                print(f'This is player 1 {self.player_1}, player 2 {self.player_2}')

View and embed is sending alright until I try passing 2 values through it view = StartMatch(player_1, player_2).

Im not very strong at OOP so I might be missing something.

the values gets printed as intended but await ctx.send(view=view, embed=embed) doesnt seem to work after I do so...

I hope i managed to explain well let me know if you need further details!

edit: I didnt manage to raise any error

fixed code:

def __init__(self, player_1, player_2):
        super().__init__()
        self.player_1 = player_1
        self.player_2 = player_2
        print(f'This is player 1 {self.player_1}, player 2 {self.player_2}')```

CodePudding user response:

Whenever you subclass something & override the __init__, you should call the parent class' __init__ method as well. Otherwise, you're not initializing the parent class.

def __init__(self, ...):
    super().__init__(...)  # Pass any args you want to pass to the super class, if applicable
    # Do whatever you want here

For future reference, "it doesn't seem to work anymore" is not very useful for people to find the problem. Add the actual error message to your post.

  • Related