How do I choose from a list of random words

Get help using Construct 2

Post » Tue Feb 28, 2012 6:01 pm

Here's what I'm looking to do
have a button that when you press it, it pulls a random name from a list of about 1,000 names.

Currently here's how I'm doing it(I have a feeling there is a much better way)

I have a global variable named ChooseName it's a number
when the button is clicked it picks a random number between 1-1000

if the random number is 1 it sets the text to name1, if it's 2 it sets it to name2 etc...

The problem is I have to do this for each name, it technically works but is incredibly inefficient. So here's what my ideal situation would be.

I somehow would import a list of 1000 names.(I was thinking maybe the csv plugin might be a way to go with this)

and then do the same thing with the ChooseName variable, except instead of creating a new event per name, it would just say to go to the line of the text file and set the text in the textbox to that name, this way it could be done in just one event instead of 1000.

I have a feeling there is a somewhat simple way to do this, but I'm stuck at the moment and can't seem to figure it out. If anyone has any ideas I would truly appreciate it.


B
24
S
4
G
5
Posts: 19
Reputation: 4,193

Post » Tue Feb 28, 2012 6:15 pm

You can use tokenat and tokencount system expressions.

Import your list as a single string with separators
[code]List="Andr,Valrie,Robert,Jean,Luc,Jos"[/code]
Instead of a number, make ChooseName a text variable. Assign it :
[code]ChosenName = tokenat(List,int(random(tokencount(List,","))),",")[/code]

This will put a single name in ChosenName out of List.
Check the last page of this topic they pretty much propose/explain this solution.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Tue Feb 28, 2012 7:04 pm

...and what about with the "choose" expression ?

Values

choose(a, b [, c...])
Choose one of the given parameters at random. E.g. choose(1, 3, 9, 20) randomly picks one of the four numbers and returns that. This also works with strings, e.g. choose("Hello", "Hi") returns either Hello or Hi. Any number of parameters can be used as long as there are at least two.
vidi2012-02-28 19:04:52
B
21
S
5
G
5
Posts: 163
Reputation: 5,028

Post » Tue Feb 28, 2012 10:23 pm

This is exactly what I was looking for, thanks so much!
B
24
S
4
G
5
Posts: 19
Reputation: 4,193


Return to How do I....?

Who is online

Users browsing this forum: mercuryus, Yahoo [Bot] and 14 guests