Is there an easier way to have a variable that updates itself if multiple conditions are met?

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
skinnyfitgenes
Newbie
Posts: 12
Joined: Wed May 12, 2021 11:15 am
Contact:

Is there an easier way to have a variable that updates itself if multiple conditions are met?

#1 Post by skinnyfitgenes »

Hi folks - as always I will preface this by saying English is not my first language, so apologies for any mistakes or confusion.

My game has a card collecting mechanic which is working quite well, all of the cards have a unique numerical value from 1-13 and a separate “skill” stat. I am trying to set up a mechanic where, if the player has a run of cards that have consecutive numerical value, they get a boost to their skill stat.

For example, if you have 1&2, they both gain +1 to their skill stat. If you have 1&2&3, they all gain +2.

I’ve figured out a way to do it, but it will need so, so many “if” statements. It will need to check if there are any cards that go together, and then apply a bonus; but if the bonus has already been applied, and the player no longer has those cards that went together, then it will need to deduct the bonus.

For every single possible card combination... so, it’s doable, but will require a lot of if statements.

I’m just wondering if anyone has done something similar, and if there is an easier way of doing this?

Apologies if this hasn’t been clear, please let me know if there’s anything that does not make sense and I will try to clarify.

User avatar
Captain Monocle
Newbie
Posts: 7
Joined: Sat Feb 22, 2020 6:59 pm
Contact:

Re: Is there an easier way to have a variable that updates itself if multiple conditions are met?

#2 Post by Captain Monocle »

I'd make the check a Python function so you can reuse it quickly, but otherwise there not really a faster approach as far as I know. There are alternative ways to organize your conditional statements, but ultimately it might not change much. For example, you could have all your conditions in lists/dictionaries, and have your function go through said conditions using a for loop. You'll have a shorter function, but the tradeoff is having to maintain the list/dictionary.

The good thing is that you function handles the check, so you can just call Check() anytime you want to update stats_boost (say, after every card draw).

Mind you this is just one way I figured to handle such conditions (and I'm in no way an expert in Python, clearly) so this isn't quite that efficient, for example, this can't handle multiple conditions/boosts and other edge cases, but hopefully it's an idea you can play around with, maybe? At least, until someone with more experience can better advise you. :P

Code: Select all

init python:

    class Card:
        def __init__(self, val):
            self.val = val
    
    def Check():
        global stats_boost, cards, conditional_statements

        # Reset stats_boost to 0
        stats_boost = 0

        # Create a list of the values of the current cards
        cards_nums = [card.val for card in cards]

        # For each condition in conditional_statements...
        for condition in conditional_statements:

            # If the cards are in the condition...
            if all(item in cards_nums for item in condition["condition"]):

                # Set stats_boost to the boost associated with the condition
                stats_boost = condition["boost"]

                # Then stop processing the for loop (so that stats_boost isn't overwritten by another inferior condition)
                break

default stats_boost = 0
default cards = [Card(1), Card(2), Card(3)]
default conditional_statements = [
    {
        "condition": [1,2,3,4],
        "boost": 5
    },
    {
        "condition": [1,2,3],
        "boost": 2
    },
    {
        "condition": [1,2],
        "boost": 1
    }
]
"We burn the present for the sake of a brighter future and act surprised when all it holds is ash"
http://www.paranatural.net/comic/chapter-4-page-89

Post Reply

Who is online

Users browsing this forum: No registered users