Coding Help for a Sim with Multiple Storylines

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
WizzyGameMaster
Regular
Posts: 40
Joined: Tue Mar 03, 2015 11:03 pm
Projects: Misfits. Summon!
Deviantart: WizzyGameMaster
Contact:

Coding Help for a Sim with Multiple Storylines

#1 Post by WizzyGameMaster »

Alright, so I'm working on a dating sim that currently has 10 romanceable options. The game uses a points system and I want to code it so that at the end of the prologue, it'll jump to the guy with the most points. Sort of like how does in Katawa Shoujo: you get to the festival and if the character's points are high enough, you jump to that storyline.

But there's also certain conditions that limit the routes. Let's just call them 1, 2, and 3 to keep it simple. We'll just call the guys A, B, C, and so on
Condition 1 - A, B, C
Condition 2 - D, E, F
Condition 3 - G, H, J, K

What I want to know is, how would I write the code for that?

Also for another project that is closely related to this, how would I code the same thing without the Conditions limiting it?
Like I have A, B, C, D, E, and F and I want it to automatically jump to the route with the highest points within that group.

I've tried looking around, but because of my number of routes, nothing that I've been able to find has been usable. They all only show two or three routes in a way where adding even one more would be too messy to be functional or have the player just click the character they want which sort of defeats the whole purpose of the point system.

Can anyone help me out here? Even just an example code would be helpful

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#2 Post by xela »

I don't understand why this is different from having just the two choices:

Code: Select all

menu:
    "A" if condition_1:
    "B" if condition_1:
    ...
    "K" if condition_3:
For the second part, you need a binding, like a dictionary:

Code: Select all

d = {"A": 0, "B": 0, ...}
when you want to add points to one of the stories, you do:

Code: Select all

d["A"] += 1
When you want to pick the story with the highest score, you get the highest value and jump to whatever key is bound to it (or resolve the jump based of the story letter). You can read about dicts in Tutorials Point, menus in Ren'Py documentation and getting the key off the highest value from a dict on stack overflow.
Like what we're doing? Support us at:
Image

User avatar
WizzyGameMaster
Regular
Posts: 40
Joined: Tue Mar 03, 2015 11:03 pm
Projects: Misfits. Summon!
Deviantart: WizzyGameMaster
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#3 Post by WizzyGameMaster »

xela wrote:I don't understand why this is different from having just the two choices:

Code: Select all

menu:
    "A" if condition_1:
    "B" if condition_1:
    ...
    "K" if condition_3:
For the second part, you need a binding, like a dictionary:

Code: Select all

d = {"A": 0, "B": 0, ...}
when you want to add points to one of the stories, you do:

Code: Select all

d["A"] += 1
When you want to pick the story with the highest score, you get the highest value and jump to whatever key is bound to it (or resolve the jump based of the story letter). You can read about dicts in Tutorials Point, menus in Ren'Py documentation and getting the key off the highest value from a dict on stack overflow.
I'm not quite understanding this. I'm still sort of a beginner with some stuff. Do you think maybe you could explain it a little clearer? (or dumb it down a bit...)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#4 Post by xela »

Dictionaries you can use to track points. "Guys" should be keys and amount of points should be values:

http://www.tutorialspoint.com/python/py ... ionary.htm


Getting the "guy" with the highest points value is explained here:

http://stackoverflow.com/questions/6131 ... y-by-value


Jumping to a new label (starting a path/option) is explained here:

https://www.renpy.org/doc/html/label.html


If statement is explained here a little bit:

https://www.renpy.org/doc/html/quicksta ... statements

or look for the same at Tutorials Point.

===>
Forget the menu bit, I misread your post thinking that you wanted to offer the player choices but now see that you want it to be resolved automatically.
Like what we're doing? Support us at:
Image

User avatar
WizzyGameMaster
Regular
Posts: 40
Joined: Tue Mar 03, 2015 11:03 pm
Projects: Misfits. Summon!
Deviantart: WizzyGameMaster
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#5 Post by WizzyGameMaster »

xela wrote:Dictionaries you can use to track points. "Guys" should be keys and amount of points should be values:

http://www.tutorialspoint.com/python/py ... ionary.htm


Getting the "guy" with the highest points value is explained here:

http://stackoverflow.com/questions/6131 ... y-by-value


Jumping to a new label (starting a path/option) is explained here:

https://www.renpy.org/doc/html/label.html


If statement is explained here a little bit:

https://www.renpy.org/doc/html/quicksta ... statements

or look for the same at Tutorials Point.

===>
Forget the menu bit, I misread your post thinking that you wanted to offer the player choices but now see that you want it to be resolved automatically.
Alright, I get the part about the dictionary and adding points. But once I get past that, I'm a bit lost. (I get the basics of jumps, labels and if statements though.)
I'm not quite getting what it is I need to do. The second link explains about sorting the values, but it seems like it matters how you do it.

Let's say I have A, B, and C. C has 12 points, A has 9 and B has 3. They are only accessible if you have Condition 1,

Code: Select all

$ condition1
, and not the others. (They're set so that you can only have one of the three conditions.)
What would be the best way to code that?
If I can at least get that figured out, I think I could figure out how to code a similar thing without the conditions on my own.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#6 Post by xela »

One way would be to rebuild the dict so it only has the choices that belong to condition 1:

Code: Select all

python:
    if condition1:
        temp_dict = {k: d[k] for k in d if k in ["A", "B"]}
then you can get the highest value from the temp dict and you're done. Change the values in ["A", "B"] accordingly, rest of the code can remain the exact same for all of your choices. If you wish to understand what's going on, google: python dict comprehensions.
Like what we're doing? Support us at:
Image

User avatar
WizzyGameMaster
Regular
Posts: 40
Joined: Tue Mar 03, 2015 11:03 pm
Projects: Misfits. Summon!
Deviantart: WizzyGameMaster
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#7 Post by WizzyGameMaster »

xela wrote:One way would be to rebuild the dict so it only has the choices that belong to condition 1:

Code: Select all

python:
    if condition1:
        temp_dict = {k: d[k] for k in d if k in ["A", "B"]}
then you can get the highest value from the temp dict and you're done. Change the values in ["A", "B"] accordingly, rest of the code can remain the exact same for all of your choices. If you wish to understand what's going on, google: python dict comprehensions.
Alright, though how do I get the highest value from the temp dict and make that jump to the coordinating storyline?

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#8 Post by xela »

Everything is spelled out for you in the guides, if you don't want to figure it out for yourself, you are not likely to ever complete a game with logic of any high degree complexity in it...

Code: Select all

init python:
    import operator
    
default story_points = {"A": 1, "B": 23, "C": 11, "D": 3}
default condition_1 = False
default condition_2 = True

label start:
    python:
        if condition_1:
            temp_dict = {k: story_points[k] for k in story_points if k in ["A", "B"]}
        elif condition_2:
            temp_dict = {k: story_points[k] for k in story_points if k in ["C", "D"]}
            
        highest_score = sorted(temp_dict.items(), key=operator.itemgetter(1))[-1][0]
        
    jump expression highest_score
        
label A:
    "Story A"
    return
    
label B:
    "Story B"
    return
    
label C:
    "Story C"
    return
    
label D:
    "Story D"
    return
Like what we're doing? Support us at:
Image

User avatar
WizzyGameMaster
Regular
Posts: 40
Joined: Tue Mar 03, 2015 11:03 pm
Projects: Misfits. Summon!
Deviantart: WizzyGameMaster
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#9 Post by WizzyGameMaster »

xela wrote:Everything is spelled out for you in the guides, if you don't want to figure it out for yourself, you are not likely to ever complete a game with logic of any high degree complexity in it...

Code: Select all

init python:
    import operator
    
default story_points = {"A": 1, "B": 23, "C": 11, "D": 3}
default condition_1 = False
default condition_2 = True

label start:
    python:
        if condition_1:
            temp_dict = {k: story_points[k] for k in story_points if k in ["A", "B"]}
        elif condition_2:t
            temp_dict = {k: story_points[k] for k in story_points if k in ["C", "D"]}
            
        highest_score = sorted(temp_dict.items(), key=operator.itemgetter(1))[-1][0]
        
    jump expression highest_score
        
label A:
    "Story A"
    return
    
label B:
    "Story B"
    return
    
label C:
    "Story C"
    return
    
label D:
    "Story D"
    return
I've got it now. Thank you for your help. My apologies if I seemed difficult. Reading about things doesn't always help me learn as easily as being given an example and trying it myself.

User avatar
WizzyGameMaster
Regular
Posts: 40
Joined: Tue Mar 03, 2015 11:03 pm
Projects: Misfits. Summon!
Deviantart: WizzyGameMaster
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#10 Post by WizzyGameMaster »

So I've got this figured out, though I had a question about it.

I know that this will add to the total

Code: Select all

d["A"] += 1
and

Code: Select all

d["A"] -= 1
will subtract a point, but how can I reset the value to "0"?

Like after jumping to Route "A," I want to reset only that set of points to "0" so that I can then use them as a way determining which ending the player will get. Like if you have under 5 points by a certain event, then you get the bad ending.

I've tried messing around with it, but it always gives me an error no matter what I try.

What would be the proper code to rest it to "0"? Or would it be best to use something like

Code: Select all

$ a2 += 0

Code: Select all

$ a2 = 0
instead? (I forget the exact way that kind of code goes, as I'm not looking at it right this second.)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coding Help for a Sim with Multiple Storylines

#11 Post by xela »

Code: Select all

d["A"] = 0
should do it.
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: Alex