Random choice ONLY from True variables

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
Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Random choice ONLY from True variables

#1 Post by Exiscoming »

I have a scene where my characters are returning from the beach and I'd like them to randomly equip one of their swimsuits. But only from the ones they've bought from the shop.

Code: Select all

default swimsuit = 0			# 0 character wears no swimsuit. 1 character wears swimsuit1, etc.

default swimsuit1 = True
default swimsuit2 = True
default swimsuit3 = False		# Has not been purchased yet

label randomSuit:
	$ randSwimsuit = renpy.random.choice(['swimsuit1', 'swimsuit2', 'swimsuit3'])
		if randSwimsuit == 1:
			$ swimsuit = 1
		elif randSwimsuit == 2:
			$ swimsuit = 2
		elif randSwimsuit == 3:
			$ swimsuit = 3	
In the above example, it randomly picks from 3 choices. Is there a way to exclude swimsuit3 because it hasn't been acquired yet?

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Random choice ONLY from True variables

#2 Post by Per K Grok »

Exiscoming wrote: Wed Nov 13, 2019 12:25 pm I have a scene where my characters are returning from the beach and I'd like them to randomly equip one of their swimsuits. But only from the ones they've bought from the shop.

Code: Select all

default swimsuit = 0			# 0 character wears no swimsuit. 1 character wears swimsuit1, etc.

default swimsuit1 = True
default swimsuit2 = True
default swimsuit3 = False		# Has not been purchased yet

label randomSuit:
	$ randSwimsuit = renpy.random.choice(['swimsuit1', 'swimsuit2', 'swimsuit3'])
		if randSwimsuit == 1:
			$ swimsuit = 1
		elif randSwimsuit == 2:
			$ swimsuit = 2
		elif randSwimsuit == 3:
			$ swimsuit = 3	
In the above example, it randomly picks from 3 choices. Is there a way to exclude swimsuit3 because it hasn't been acquired yet?
Your code will not work.

randSwimsuit will be 'swimsuit1', 'swimsuit2' or 'swimsuit3', not 1, 2 or 3. So swimsuit will remain at it's default value of 0.

There are different ways of doing what you want to do. I would go with a list that contains the suits available at the time, and use append to add suits that are purchased later.

This is a code that does that.

Code: Select all

default swimsuit=0
default su=0
default swimsuits=[1,2]

label start:

label randomSuit:
    $ su = renpy.random.randint(0,len(swimsuits)-1)   # pick a number between 0 and the length of the list - 1
    $ swimsuit= swimsuits[su]                   # pick the item in the list at the randomly chosen position, 0 being the first position
    "Availabel swimsuits [swimsuits]"
    "Swimsuit [swimsuit] is chosen."


    if swimsuits.count(3)==0:    #adding suit 3 if it is not in the list
        $ swimsuits.append(3)
        "Swimsuit number 3 has been added."

    jump randomSuit    # running the code again



Post Reply

Who is online

Users browsing this forum: No registered users