[Solved] Increase Stats based on number of Occurrences

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
User avatar
ihaveSEN
Newbie
Posts: 3
Joined: Fri Nov 17, 2023 8:10 pm
Discord: ihavesen
Contact:

[Solved] Increase Stats based on number of Occurrences

#1 Post by ihaveSEN »

Hello. Hopefully this makes sense.

Currently, I have made a system where player can choose from a list of classes, and their stat increase is based on which type of classes they chose as well as the number of classes chosen.

This is how it is coded currently:

(1) Slots 1~5 defined at the beginning of the script.

Code: Select all

default slot1 = "undefined"
default slot2 = "undefined"
default slot3 = "undefined"
default slot4 = "undefined"
default slot5 = "undefined"
(2) Choice menu for actually choosing the classes.

Code: Select all

    menu:
        "Select a course for Day 1."
        "Class A":
            $ slot1 = "Class A"
        "Class B":
            $ slot1 = "Class B"
        "Class C":
            $ slot1 = "Class C"
            
    menu:
        "Select a course for Day 2."
        "Class A":
            $ slot2 = "Class A"
        "Class B":
            $ slot2 = "Class B"
        "Class C":
            $ slot2 = "Class C" 
^Repeat this 3 more times for slots 3~5

(3) Actually calculating the stat increases.

Code: Select all

	if slot 1 == "Class A":
		$ skillA =+ 5
	if slot 1 == "Class B":
		$ skillB =+ 5
	if slot 1 == "Class C":
		$ skillC =+ 5
	
^Repeat for slots 2~5

Currently, this is fine with only 3 or so classes, but in the future, this is probably a problem if I add more classes, like 20~30, because there will be too many if statements.

Main Question:
Is there a way to streamline this process, such as checking slots 1~5 for the number of instance which a class appears, and assigning stat increase based on the name and the number of times each class has appeared.

Hopefully this made sense. Thank you in advance for reading so far.
Last edited by ihaveSEN on Sat Nov 18, 2023 9:09 pm, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2467
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Increase Stats based on number of Occurrences

#2 Post by Ocelot »

First of all, if you ever feel the need to make multiple varables whcih differ only in number at their end, you need a list instead:

Code: Select all

default slots = ["undefined"]*5
Now we can streamline.

First of all, even though you didn't ask, you can streamline your menus:

Code: Select all

$ current_day = 1
while current_day < 6:
    menu:
        "Select a course for Day [i]."
        "Class A":
            $ slots[i-1] = "Class A"
        "Class B":
            $ slots[i-1] = "Class B"
        "Class C":
            $ slots[i-1] = "Class C"
    $ current_day += 1
Second: now when all courses are packed neatly in list, you can check count of each and every class and modify stat values as nessesary:

Code: Select all

python:
    skillA += 5 * slots.count("Class A")
    skillB += 5 * slots.count("Class B")
    skillC += 5 * slots.count("Class C")
There arre ways to automate it further, but they require non-trivial modifications of your code, and I am not sure if you event need that.
< < insert Rick Cook quote here > >

User avatar
ihaveSEN
Newbie
Posts: 3
Joined: Fri Nov 17, 2023 8:10 pm
Discord: ihavesen
Contact:

Re: Increase Stats based on number of Occurrences

#3 Post by ihaveSEN »

Thank you for your reply.

I have input this part of the script:

Code: Select all

##define slot 0~4 as string before start##
default slot = ["undefined"]*5

##Actual code to assign class to slots 0~4##
label start:
    $ day = 1
    while day < 6:
        menu:
            "Select course for day [day]."
            "Class A":
                $ slot[i-1] = "Class A"
            "Class B":
                $ slot[i-1] = "Class B"
            "Class C":
                $ slot[i-1] = "Class C"
        $ day += 1
    "Finish"
I just have a follow-up question. How and where can I define the "i" in [i-1] so the program knows the integer in "i" should share the same value as "day"?

Error message from renpy:
NameErorr: name 'i' is not defined
Thank you in advance for your time.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2467
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Increase Stats based on number of Occurrences

#4 Post by Ocelot »

That's my bad, I have edited text to change i to current_day and missed the most obvious case.

Use day-1 instead of i-1 on your code.
< < insert Rick Cook quote here > >

User avatar
ihaveSEN
Newbie
Posts: 3
Joined: Fri Nov 17, 2023 8:10 pm
Discord: ihavesen
Contact:

Re: Increase Stats based on number of Occurrences

#5 Post by ihaveSEN »

Alright, thank you.

I will take some time to sit with the code but I think my problem has been solved for now.

Thank you very much.

Post Reply

Who is online

Users browsing this forum: Google [Bot]