Only allowing a variable to add to a value the first time an option is clicked?

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
NotePaper
Newbie
Posts: 1
Joined: Tue Jul 17, 2018 10:35 pm
Contact:

Only allowing a variable to add to a value the first time an option is clicked?

#1 Post by NotePaper »

The title probably sounds confusing, but i'm not sure how to explain my issue.
I'm creating a dating sim where there is a point system to keep track of the relationship.
Currently the points are added as such:

Code: Select all

menu meetings:
        "Who do you want to talk to?"
        
        "Meet Saturn.":
            jump saturnmeeting
            $ sat_points += 1
            
        "Meet Mercury.":
            jump mercurymeeting
            $ mer_points += 1
            
        "Meet Mars.":
            jump marsmeeting
            $ mar_points += 1
        
        "Meet Venus.":
            jump venusmeeting
            $ ven_points += 1
            
        "Meet Jupiter.":
            jump jupitermeeting
            $ jup_points += 1
            
        "Meet Neptune.":
            jump neptunemeeting
            $ nep_points += 1
But, my command returns the user to this menu meetings after each interaction, which means if a user clicks on the same interaction, it will add another point to the system.
How would I make it so it only adds it the first time the interaction happens?
Is that possible or will I need to change the way the introductions work?

Bellrose980
Newbie
Posts: 2
Joined: Tue Jul 17, 2018 8:56 pm
Projects: Distinction
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#2 Post by Bellrose980 »

If you want you could use an if statement so that it will only add a point if the variable equals zero.

Code: Select all

menu meetings:
        "Who do you want to talk to?"
        
        "Meet Saturn.":
            jump saturnmeeting
            if sat_point == 0 :
  	          $ sat_points += 1
            
        "Meet Mercury.":
            jump mercurymeeting
            if mer_point == 0 :
  	          $ mer_points += 1
            
        "Meet Mars.":
            jump marsmeeting
            if mar_point == 0 :
  	          $ mar_points += 1
        
        "Meet Venus.":
            jump venusmeeting
            if ven_point == 0 :
  	          $ ven_points += 1
            
        "Meet Jupiter.":
            jump jupitermeeting
            if jup_point == 0 :
  	          $jup_points += 1
            
        "Meet Neptune.":
            jump neptunemeeting
            if nep_point == 0 :
  	          $ nep_points += 1
Hope this helps!!

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#3 Post by MaydohMaydoh »

Using set feature of menu, will remove each option from the menu as it's selected so it won't appear next time the menu is shown.

Code: Select all

default meetings_list = []
menu meetings:
	set meetings_list
        "Who do you want to talk to?"
        
        "Meet Saturn.":
            jump saturnmeeting
            $ sat_points += 1
            
        "Meet Mercury.":
            jump mercurymeeting
            $ mer_points += 1
            
        "Meet Mars.":
            jump marsmeeting
            $ mar_points += 1
        
        "Meet Venus.":
            jump venusmeeting
            $ ven_points += 1
            
        "Meet Jupiter.":
            jump jupitermeeting
            $ jup_points += 1
            
        "Meet Neptune.":
            jump neptunemeeting
            $ nep_points += 1
My guess is you're also using jump to return to the menu after each choice? If so you should place variables before you jump to each label or it won't work.

Code: Select all

"Meet Saturn.":
            $ sat_points += 1
            jump saturnmeeting

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#4 Post by trooper6 »

Maydoh, where did you find the documentation on the set feature of menu? I couldn't find it anywhere in the Ren'Py documentation and that feature is awesome!
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#5 Post by MaydohMaydoh »

Can't find any documentation on it, but I saw someone mention it a few days ago in another thread.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#6 Post by xavimat »

- Maydoh has pointed it out, but it should be stressed more: You CANNOT put a line after a jump, it will never be executed:

Code: Select all

menu:
    "Option1":
        jump option1
        $ points += 1  # This line won't be executed
- On the other hand, answering your question. I'm not sure if I understand the flow of your game. There are different options:
1. Is it possible in your game to select more than once the same option in that menu? If not, you can use the solution Maydoh with the "set" feature BUT you need something after the menu when there are no more options. When renpy finds a menu with conditions in a way that there is no option available, it simply skips the entire menu (to avoid blocking the game: a menu without buttons to click!).

2. If the player can click more than once in the same option, and you still want to add one point only the first time, you need another variable (a flag, or a list) to know that.
Bellrose's solution only works if the variable points is 0 before the menu. But what happens if it isn't? (maybe this menu is in the middle of the game, and there are other parts of the game that have added points already).
This example uses a list (meetings_selected) to keep track of the options already selected. It adds the point only if it's the first time:

Code: Select all

default meetings_selected = []
menu meetings:
        "Who do you want to talk to?"
        
        "Meet Saturn.":
            if  "Saturn" not in meetings_selected:
                $ meetings_selected.append("Saturn")
                $ sat_points += 1
            jump saturnmeeting  # remember, this line must be the last one of the block
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#7 Post by Remix »

trooper6 wrote: Wed Jul 18, 2018 12:20 am Maydoh, where did you find the documentation on the set feature of menu? I couldn't find it anywhere in the Ren'Py documentation and that feature is awesome!
MaydohMaydoh wrote: Wed Jul 18, 2018 1:07 am Can't find any documentation on it, but I saw someone mention it a few days ago in another thread.
Might have been me who mentioned it and, like Maydoh I only stumbled onto it from a forum post a good many months ago...
One caveat with it though, if you use the same list for multiple menus keep in mind that the hidden choices are based on the caption, so having the words "Choice 1" in two menus would hide from both. (even that might be useful for some games though, else use different lists)
Frameworks & Scriptlets:

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#8 Post by Showsni »

xavimat wrote: Wed Jul 18, 2018 5:46 am BUT you need something after the menu when there are no more options. When renpy finds a menu with conditions in a way that there is no option available, it simply skips the entire menu (to avoid blocking the game: a menu without buttons to click!).
Either that, or have one of your menu options run some code that will make its choice (or even another choice) show up in the list again. Something like

Code: Select all


$ girlchoices = ["I think I've talked to everyone."]

label pickgirl:
	pass
menu:
	set girlchoices
	"Talk to Ann":
		ann "Hi!"
		if len(girlchoices)>2:
			$ girlchoices.remove("I think I've talked to everyone.")
	"Talk to Beth":
		beth "Hello."
		if len(girlchoices)>2:
			$ girlchoices.remove("I think I've talked to everyone.")
	"Talk to yourself":
		"You don't have anything to say."
		$ girlchoices.remove("Talk to yourself")
	"I think I've talked to everyone.":
		jump elsewhere
jump pickgirl

phongtinh
Newbie
Posts: 3
Joined: Tue Jul 31, 2018 3:26 am
Tumblr: imaejsc
Deviantart: imaehao
Github: imaehao
Skype: imaehao
Soundcloud: imaehao
itch: imaehao
Contact:

Re: Only allowing a variable to add to a value the first time an option is clicked?

#9 Post by phongtinh »

If so you should place variables before you jump to each label or it won't work.

Post Reply

Who is online

Users browsing this forum: Google [Bot]