Page 1 of 1

Reusing Random Choice Code Effectively?

Posted: Fri Aug 06, 2021 1:45 am
by krt_org
Hi! I'm having issues with random choice code that I want to use in my dating sim. I have a couple of different times in the dialogue you can have with the characters where I'd like the responses to be randomly generated. An example of the code I'm using is below:

Code: Select all

$ chance = renpy.random.randint(1,100)
if chance <= 100:
	$ choice = renpy.random.choice(("8trivia1, 8trivia2, 8trivia3, 8trivia4"))

	if choice == "8trivia1":
		jump triv18
	elif choice == "8trivia2":
      		jump triv28
	elif choice == "8trivia3":
       		jump triv38
	elif choice == "8trivia4":
       		jump triv48
	else:
      	 	jump triv18
The issue with the code is that it doesn't seem to run the random choice, I get triv18 every single time. I had the "else: jump triv18" clause on there because I was thinking that you needed to have an else for an if, but I took it off (and deleted the persistent data) and I still always get the same choice. Is there something that needs to be changed to let it actually run a randomized choice every time? Is this intended to be only used once? The code would be run every time you talk to the character and select the trivia option.

I apologize if this question has been solved on the forum already; there were so many threads about random choice that didn't really seem to apply to what my issue is.

At the end of the day, I don't care much about randomization as much as I do that all the options have a chance to be displayed sometime.

I appreciate any help anyone has to offer! Thank you.

Re: Reusing Random Choice Code Effectively?

Posted: Fri Aug 06, 2021 4:45 am
by Ocelot
See this:

Code: Select all

choice = renpy.random.choice
(
    (
        "8trivia1, 8trivia2, 8trivia3, 8trivia4"
    )
)
Do you see the problem now? If you want more, add $ print "choice: " + str(choice) after selecting random chice and after jump press Shift+O to see console putput.

Also, there is a more concise way to do what you want to do:

Code: Select all

$ choice = renpy.random.choice(("triv18", "triv28", "triv38", "triv48"))
jump expression choice

# Or even:
jump expression renpy.random.choice(("triv18", "triv28", "triv38", "triv48"))