How do I pick random sentences from external .txt files

Get help using Construct 2

Post » Wed Feb 08, 2017 7:55 pm

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.
B
8
S
4
Posts: 17
Reputation: 792

Post » Wed Feb 08, 2017 8:18 pm

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.
B
61
S
20
G
56
Posts: 1,077
Reputation: 35,986

Post » Thu Feb 09, 2017 2:55 am

Hello, the .json file has been created.
B
8
S
4
Posts: 17
Reputation: 792

Post » Thu Feb 09, 2017 3:30 am

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.
B
61
S
20
G
56
Posts: 1,077
Reputation: 35,986

Post » Thu Feb 09, 2017 3:54 am

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
Image
B
8
S
4
Posts: 17
Reputation: 792

Post » Thu Feb 09, 2017 4:20 am

As a test, move the Set Text to Array... action up right under Load from JSON string and see if it works.
B
61
S
20
G
56
Posts: 1,077
Reputation: 35,986

Post » Thu Feb 09, 2017 5:02 am

WOOORKED like a chaaarm. Thank you.You're my life savior.
B
8
S
4
Posts: 17
Reputation: 792

Post » Thu Feb 09, 2017 5:07 am

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.
B
61
S
20
G
56
Posts: 1,077
Reputation: 35,986


Return to How do I....?

Who is online

Users browsing this forum: Armench, Jackriduk, kiki4construct and 25 guests