How do I choose from a list of random words

0 favourites
  • 4 posts
From the Asset Store
Project File of my C3 Tutorial Customized DropDown List
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can use tokenat and tokencount system expressions.

    Import your list as a single string with separators

    List="Andr�,Val�rie,Robert,Jean,Luc,Jos�"

    Instead of a number, make ChooseName a text variable. Assign it :

    ChosenName = tokenat(List,int(random(tokencount(List,","))),",")

    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.

  • ...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.

  • This is exactly what I was looking for, thanks so much!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)