How to get random text from .txt file to game text field?

I tried this option I found on GD forum, but I could not make it work…Maybe I do something wrong?

1 Like

Hi,
I would advise you to split your complex process into several simpler processes. That way you can learn how each of them works and fix your mistakes along the way.
1st - display a text stored in a variable
2nd - create several variables and pick one randomly to display it
3rd - load one string from a text file and display it.

When you succeed in doing all that, try again and see if you find your mistakes. :blush:

First of all this is not mine “complex” process…I found that on GD forum, but for my side it does not work. I would like to learn how that works if any documentation exist!..Concerning my mistakes, If I know where my mistakes are I would never place question on the forum!..I am absolute beginner with basic knowledge, and what now?

It looks like you misread my message, so feel free to read it again and follow the advice I wrote… or not. :man_shrugging:
The full documentation is here: GDevelop 5 [GDevelop wiki]

It is possible that I misunderstood you, that happens to me sometimes. I am sorry for that…
Thank you for documentation link, it is very useful. I found so many things there which I have to learn, but I did not find any documentation for option /read the text from the file/…Thank you anyway…
I will read your previous message again to try to understand what you meant by that…

Regards…

No problem. What I meant is that you should decompose your problem. It will help you solve it.

read/write is here: Storage action explained [GDevelop wiki]

I checked before that link, but there is just explanation about storage. I just have .txt file and I just want to read from it. I succeeded to load text from my Test1 text file with option /load text from the file/ but I would like to have many words in .txt file and choose them randomly. One thing I also do not know how .txt file should be formatted inside for random reading.
I understand what you mean with decomposing problem, but my only problem is my limited knowledge and lack of documentation, because I know what I want to do but just do not know how. That is also possible that I misunderstand documentation :slight_smile:

Regards…

I don’t know much about storage, but the documentation says that you can only read what you saved through the game, it does not work like the filesystem functions where you can access files external to the game.
Going back to that idea of decomposing, if you try to load a simple phrase with that storage action, without any randomness, it should fail all the same.

I do not know what documentation say :slight_smile: …I am already told you that I found out how to read .txt file from game root folder…Take look in image…
I want to read text in it just randomly and I do not know how, that is my problem now…

Yes, there is a difference between filesystem (access to filesystem files) and storage (internal to GDevelop)

This what I did is just load .txt file…How to read?..:slight_smile:

You will need to remove the ( ) on the first line, in blue.

I’m sorry if I’m interrupting or if you already know what I’m about to say, but I think a simpler way to do this would be to just… use the “Write into Storage file” actions to write all the values you need separately, and then you can use expressions to retrieve them - without needing to write the text file yourself outside of GD. ^~^

Something like:

At the beginning of the scene
  write "sample" in "1" of storage "Options"

And you can store many values like this, in “numbered” groups. And then, in order yo read them randomly, you can just use a variable with a random value set into it, in the way you did in the example you showed above!

// Assign random value to variable //

do = Random(3) to scene variable "Random"

This will assign it a random variable from 0 to 3. In case you want it to be from 1 to 3, you can use RandomInRange(1, 3) !

// Read random group from storage //

read VariableString(Random) from storage "Options" and store as text in "Value"

This way, you will be picking a random group from the storage file and reading its content into the “Value” scene variable as text. ^v^
Is this what you wanted? I’m sorry if it isn’t! I hope it helps, though!

The same can also be done with structures and things of the sort, which may turn out to be a bit easier than using the storage constantly if you have to randomly generate these texts a lot!

I hope this helps!

Hi,

Thank you for your tip!..I know that it could be done that way, but that what I want to do is to make random reading of .txt file saved in game root folder. I plan to use around 100-200 words, it is a bit too much…

Thank you very much…

Regards…

Where is the difference?..:slight_smile:

Thank you for the info!..I make it just for my kid and I did not plan to use storage for the same reason you mentioned, because game will be just on computer. Anyway storage is not solution for 100-200 words…

Thank you very much…

Regards…

This is not true. When you choose to publish your game to desktop or mobile, you game is being wrapped inside the Chromium web browser so it is still running inside a web browser and the storage events do work on all platforms.

Why not? It is not different than using external file except you can not edit the file outside the game. Using external file can be useful only if you do want to edit the file outside the game, otherwise storage is perfectly fine.

If you prefer to use external files, what you would normally do is read the data line by line which I believe is not supported in GD at the moment, than store each line inside a structure then pick a random structure. Or name each text file as 0,1,2,3…etc and pick a random text file to read… So there is not much benefit to use text files really unless you want to edit the file outside the game or you want to bring data in to the game from outside GDevelop.

However, If you prefer to use external file, your best option I think is to use JSON format to store and read the data.

2 Likes

Hi,

Your tips are great as usual!..That is great idea JSON file…Just tell me how I should store text in JSON file like array [“test1”, “test2”, “test3”] ?..I am just not sure how to create correct JSON file.

Thank you very much for your help!

Regards,

Then the wiki should probably be more clear. It was explained that way somewhere else too.

Ah yes, that’s how it came across here:

The JSON string could look like this:

{
“1”: “string1”,
“2”: “string2”,
“3”: “string3”,
“4”: “string4”,
“5”: “string5”,
“6”: “string6”,
“7”: “string7”,
“8”: “string8”,
“9”: “string9”,
“10”: “string10”,
“11”: “string11”,
“12”: “string12”,
“13”: “string13”,
“14”: “string14”,
“15”: “string15”,
“16”: “string16”,
“17”: “string17”,
“18”: “string18”
}

You need to load this text as a jsonString in to a variable, then you need to parse the string as a json object in to another variable (let call it jsonObject) which is essentially going to be a structure then you should be able to pick a random string like so:

VariableString(jsonObject[ToString(RandomInRange(1,18))])

Keep that in mind though, it may not going to work in preview, you need to build a desktop WIndows, Linux or Mac app in order to get it working.
I did not test it though, to be honest I never used the file system events, so don’t quote me on this but an example is included with GDevelop to help you figure this out.

Yeah It is called “web storage” but it doesn’t require to host the game online, it is store data in the web browser and since the game runs inside the browser even if you publish to desktop and mobile, you can use the storage events on those platforms too to store data in “web storage”. It is also often referred to as “browser storage” since it makes more sense but as far as I know officially it is called “web storage”:

Also note that, originally the event was referring to a “file” because back in GD4 it was reading and writing file on desktop platform and web storage in HTML5 platform, but since GD5 is HTML5 only, the “file” have been renamed to “storage”. So you might going to find more confusing replies and topics from before when the event was referring to a “file” and if it was about GD4…

1 Like