How do I randomly spawn a text out of a predefined text-pool?

  • Hi everybody!

    I would like to spawn a random text out of a text-pool, how can I implement that?

    Thank you in advance!

  • You can set an array, then randomly choose an index in the array.

    Array.Push value "Text1",

    Array.Push value "Text2",

    Then,

    Text.Set( Array.At( floor(random( Array.Width ))))

  • If it's a small set you could use choose.

    choose("This text","That text","The other text")

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If your following the beta channel you can use the AdvancedRandom.Weighted expression that was added in r149.

    It works by creating a table of values with different probabilities, which you can then randomly select from.

    For simple use you can do something like the following:

  • another option I use a lot is to use TokenCount and TokenAt...

    put your text in a variable with a delimiter and then use TokenCount to see how many choices there are, and TokenAt to extract a random one.

    if you want any control over how often a particular text is chosen, or want to make sure it doesn't repeat until all the possibilities have appeared, then an array is a good choice - you can delete the entry that is chosen.

    (Using TokenAt is a great way to load up the array, and then delete each entry as it is used to make sure it doesn't repeat)

    EDIT: Nepeo can you remove an entry from the Advanced Random to eliminate duplicates? That would be awesome!

  • AllanR we don't have an option to remove an entry at the moment, but it would be fairly simple to add.

    Another tool which AdvancedRandom adds which could be useful here is the permutation table. It creates a list of unique numbers in a random order. If you create a permutation table the size of your word array you can just increment through the permutation table and use the value to select a word that hasn't been used before ( effectively it's a round about way to "shuffle" your word array ).

  • Thank you very much for all your answers!

    I now created an Array via the array editor as json file.

    I managed to load the data via Ajax into a text object.

    But now I don't know how I can access the data from the array at a specific xy position.

    Can anybody help?

  • in the AJAX On Complete event (event 7) you are setting a text object to AJAX.LastData

    you should be loading LastData into the array (Array Load AJAX.LastData)

    then you can access the array like this:

    TextObject Set Text Array.At(random)

    (Where random is a random number between 0 and the last entry)

  • AllanR I tried th load the data into an array, as you can see in the two screenshots, but unfortunately it returns a 0 in my Textboject (r1r2) when I run it.

    Thanks in advance!

  • Could it be possible, that the round1_array isn't visible anymore in another event?

    When I set the text in the AJAX on completed event it works, but I would rather do that in an other event.

    How could I make it visible for other events too?

  • the array wont be loaded yet when event 8 executes (at start of layout)

    it will take a while before the AJAX on complete executes, after that the array will be loaded and available to other events...

    if you assign the text object in the AJAX on complete event (after loading the array), I would expect that to work.

  • You can use a signal to wait while AJAX load the file :

    in Start layout, Wait for signal "AJAX done", and when AJAX is completed, Send signal "AJAX done"

    Lazzy version : Wait 1 second, it should work.

  • Nicolas Wak

    Is this the way you ment? (See picture below)

    Unfortuately neither this does work nor if I just Use System Wait in the on Start of layout.

    Can you assume why? Or you could imagine another solution?

  • You have 2 Start Layout, you need one :

    Start layout > Load game > AJAX Request > Wait, and then set text

    Why? Because Signal "stop" code execution until a go, so all the code blocked need to be "sticked" together

    Exemple code below :

    dropbox.com/s/tram2xj4r3jtfut/Signal.c3p

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