Random label from list

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
eoloe
Newbie
Posts: 1
Joined: Wed Feb 24, 2010 11:02 pm
Contact:

Random label from list

#1 Post by eoloe » Tue Mar 02, 2010 12:15 am

Hello !

I'm trying to jump or call labels at random.


let's say I have a list like this :

Code: Select all

list = ["forest", "town", "mountain", "swamp", "sea"]
I would like to be able to do a

Code: Select all

$ surprise = renpy.random.choice(list)
And then I would like to be able to something like this

Code: Select all

call surprise
or like this

Code: Select all

jump surprise
Of course the two last lines are not working. Can you please help me to figure this out ?

Thank you

User avatar
Asphodel
Regular
Posts: 28
Joined: Sat Jan 17, 2009 1:40 am
Contact:

Re: Random label from list

#2 Post by Asphodel » Tue Mar 02, 2010 12:34 am

Try something like this:


In your init block, put:

Code: Select all

$ import random
and then in the main script where you want it to happen:

Code: Select all

$ surprise = int(random()*5) # There are 5 choices.
if surprise == 0:
  jump forest
elif surprise == 1:
  jump town
elif surprise == 2:
  jump mountain
elif surprise == 3:
  jump swamp
elif surprise == 4: # We end at N-1 because we started at zero.
  jump sea
If you prefer counting from 1 to N instead of 0 to N-1, then you can instead write:

Code: Select all

$ surprise = int(random()*5+1)
if surprise == 1:
  jump forest
and so forth.


Good luck!

User avatar
denzil
Veteran
Posts: 293
Joined: Wed Apr 20, 2005 4:01 pm
Contact:

Re: Random label from list

#3 Post by denzil » Tue Mar 02, 2010 1:35 am

eoloe wrote:And then I would like to be able to something like this

Code: Select all

call surprise
I believe what you actually want is:

Code: Select all

call expression surprise
Practice makes purrrfect.
Finished projects: Broken sky .:. colorless day .:. and few more...

Post Reply

Who is online

Users browsing this forum: _ticlock_