How do I pick random sentences from external .txt files

0 favourites
  • 8 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • Hi guys.

    Construct new user trying to make a test game, progressing slowly. I would like to make a list of hundreds of sentences inside a .txt file.

    For example ''the sky is blue'' or ''the flowers are beautiful" etc. I will add more text later as updates. Then i would like to pick random sentences from this .txt file and display as a text when the layout is started.

    I've took the time to search , everyone is talking about array etc but i have no idea how to make it.

    As a non text programmer I'm only using the sheet. Could someone help me to create the sheet?

    Please only answer if you post a sheet screenshot, i cannot understand any form of programming languages for the moment. I'm ready to learn but step by step.

    So to make it short i would like to see

    1.the sheet screenshot

    2.the .txt file form ( comas or brackets, line spacing etc etc)

    Thanks in advance.

  • First you need to make your array file. Open a blank notepad and paste in the following:

    {

    "c2array":true,

    "size":[1,3,1],

    "data":[ [ ["sentence 1"],

    ["sentence 2"],

    ["sentence 3"],

    ["sentence 4"]

    ]

    ]

    }

    Just replace sentence 1 etc with your sentences. Then change the 1,3,1 part at the top so the 3 is equal to the number of sentences that you have -1.

    When you're finished save the .txt file as .json

    After you are finished with that, let me know and I'll show you how to put it into your project.

  • Hello, the .json file has been created.

  • Ok, the first thing you'll want to do is in your project click on the layout editor (Just like if you were going to add a new sprite to the project) and add the AJAX object and the Array object.

    Then go to the bottom right of the screen and in the Projects panel right click on Files then click on Import Files. Import the .json file to the project.

    Then you'll need to add some events to your eventsheet like this:

    System, On start of layout -> AJAX Request yourfilename.json (tag "sentences")

    In another event under it add this:

    AJAX, On "sentences" completed -> Array, Load from JSON string AJAX.LastData

    Now your array is set up and ready to use. To choose a random sentence to display, you'll use something like this:

    Add the Text object to your level layout (the same way you would add a new sprite). Resize the text object so it's large enough to display your sentences, then add the following to your level eventsheet.

    System, On start of layout -> Text, Set Text to Array.At(0,(random(100))

    The 100 is the number of sentences that you have setup.

    Try that out and once you have it set up if it doesn't work then I'll help you tweak it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks you for you time and support, big hug for you if you're a lady, smile if you're a man.

    Anyway i've managed to create the sheet but when launching the game, the text object only display ''0''.

    I don't know if it's related to the array dimension setting or something else.

    Thanks

  • As a test, move the Set Text to Array... action up right under Load from JSON string and see if it works.

  • WOOORKED like a chaaarm. Thank you.You're my life savior.

  • No problem. The reason it didn't work the first time is because the array wasn't loaded in time. If you want to call the array from another layout, after the one with this code is on it runs, you can set the text on the start of the new layout without any issues.

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