#๐Ÿ”’ Objects Within Objects?

24 messages ยท Page 1 of 1 (latest)

supple whale
#

Hello! I was wondering if it's possible to have an object inside of an object. I'm trying to have moves assigned to certain characters, but it doesn't seem to work. The code is in Ren'py, but I put it in an init python block, so it's interpreting it as Python code.

Here's all of the code in the file:


init python:

    class Moves:
        def __init__(self, name, text, effect):
            self.name = name
            self.text = text
            self.effect = effect

    pet = Moves("Pet", "He really liked that!", 1)


    def recruit_value(value):
        recruit += value

    class Attacks:
        def __init__(self, name, text, effect):
            self.name = name
            self.text = text
            self.effect = effect

    class Characters:
        def __init__(self, name, hp, attack, defense, speed, move1, move2):
            self.name = name
            self.hp = hp
            self.attack = attack
            self.defense = defense
            self.speed = speed
            self.move1 = move1
            self.move2 = move2

    marshy = Characters("Marshy", 20, 10, 10, 9, marshmallowspit, strongattack)
    drchocomarsh = Characters("Dr. Chocomarsh", 40, 15, 9, 6, marshmallowspit, icyspell)
    loansharky = Characters("Loan Sharky", 30, 15, 15, 9, bite, headbutt)

    marshmallowspit = Attacks("Marshmallow Spit", "[player.name] used Marshmallow Spit!", 10)
    icyspell = Attacks("Icy Spell", "[player.name] used Icy Spell!", 10)
    headbutt = Attacks("Headbutt", "[player.name] used Headbutt!", 15)
    strongattack = Attacks("Strong Attack", "[player.name] used Strong Attack!", 10)
    bite = Attacks("Bite", "[player.name] used Bite", 10)

    #Damage Formula

    def battleformula(player, enemy, move):
        damage = 2*player.attack+move.effect - enemy.defense
        enemy.hp -= damage
        return```
undone cliffBOT
#

@supple whale

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

meager fulcrum
pine relic
#

can you elaborate on "doesn't seem to work"?

supple whale
#

Oh wait, it's because the moves aren't first.

pine relic
#

yes, you've simply assigned them out of order

#

also this "objects within objects" is known as "composition"

supple whale
#

Thank you for giving me the name!

#

That makes it easier to research it.

#

By the way, if it's not advised, does that mean that composition leads to spaghetti code?

pine relic
#

What you have is fine

#

They're saying don't declare a class within a class (which you haven't done here)

#

but passing instances of a class to another class is perfectly ok

#

I'm not familiar with renpy though or what init python is doing

supple whale
#

Ren'py is a programming language based in Python, so it has its own conventions.

#

The "init python" is basically saying, "read this as Python code instead of Ren'py code."

#

Anyway, the problem has been solved. I'll close this.

#

Thank you so much!

#

.close

plain stormBOT
#
Did you mean:
supple whale
#

!close

undone cliffBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.