Generate Random from local storage

Hey everyone, I just finished testing around examples on Gdevelop 5

I found the Parse JSON example. and that is what I am looking for

the problem is, instead of parsing data from a website, I want to create my own data and putting it on local storage where the game located. and so the game can be played offline.

Any help is appreciated.

What i mean is, I write a list of names on maybe excel/notepad.

Then I hope i can pick one of that names on the list randomly to characters.
How do i do that ?

I don’t know if I missed something, I did try to read the documentation about storage but I guess that’s just for saving state?

sorry for bumping, I really hope someone can point me somewhere.

I think this might be what you’re looking for

Hi, thanks for replying. I did read that.

but can you tell what file is that? the extension, and how can you make that particular file?

Hi, honestly, I’m not entirely sure about all of the extensions you can use or how to implement this into your game (mine is on the web), but definitely text or JSON. Someone else may be able to explain better, sorry.

Yeah, if only there is more explanation.

Thanks btw.

What do you mean “create your own data” and “store it so the game can be played offline”? Do you mean create your own file with the data and ask the game to read from that file? What kind of data, exactly?

The Parse JSON expression can be used in several ways, it doesn’t necessarily need to parse data from a website. You can save variables into JSON through expressions and make variables out of JSON strings with the Parse JSON action, there’s a lot you can do with it.

But also, I don’t understand what “data” you want to store. What kind of “data”? Can you elaborate more on that? If you give more details on what exactly you want to store, it’ll be easier to help you with it! ^^

Hey man, thanks for the reply.
sorry that my explanation looking really weird.

let’s just go for example to make it rather easy.
Have you heard and played Rimworld? Crusader Kings? or The guild? XCOM?

In those games, they generate characters with their own set of skills and as far as I experienced all is random. Names, looks, stats, etc.
I know it complex and not for a beginner, that’s why I want to start with random names for sets of characters. so each playthrough the characters other than player have different names.

I hope you understand my explanation better :smiley:

Oooh, I do! I get it now!
Yes, that CAN be quite a handful, but there are ways of doing it. ^^ I’m sure my ways are not the best, but here it goes!

Two of the ways I can think of right now are through the STORAGE and through STRUCTURES. Both can be used together, actually.

Storage Only:

In the “Read from storage” actions, you can use variables and expressions to assign values to the “Storage name” and “Group” fields. Through that, you can randomize what you get.

This being said, the way to do this would be to write the possible names you want inside a storage file, and give a NUMBER VALUE to the group each name is stored in. This is very important - a number value.

Make a scene variable, and do = RandomInRange(1,20) to the value of said variable. The last number should be the total number of groups you have in the storage - the total number of possible names you have. Let’s call that variable “GenerateName”.

Then, when you’re creating the Character object, call the “Read a text from storage” action and assign the expression VariableString(GenerateName) to the “Group” field.
The variable will return a random number, from 1 to 20 (or whatever limit you set to it), and the group corresponding to that number will be read.

Then, just assign that value to another variable - let’s call it “NameGenerated” - and assign it to the object.

This can be done with names, stats and whatever else you want. ^^;

Structure Variables

This one is quite similar.

Make a STRUCTURE and call it “Names”. Then, make Child Variables - numbered, starting from 0. Assign a number to each child variable. By the end of it, if you have 20 names, you should have 20 child variables inside the “Names” structure, each of them holding a number in their string. As we started with 0, the last child variable should be numbered “19”.

Then, make another scene variable - NOT inside the structure - and call it “RandomName”. This will be the variable we will use to generate a random value.
Now we are ready to randomly generate the name. We can do:

// Getting a Random Number Value //

If the timer "Randomize" is greater than 1 seconds
Trigger Once
  do = Random(19) to scene variable "RandomName".
  reset the timer "Randomize"

// Generating a Random Name From The Structure //
If the text of variable "Name" of Character = "" (empty)
  do = VariableString(Names[VariableString(RandomName)]) to the text of variable "Name" of Character

This way, you will pick a random child variable from the “Names” structure, and assign its string to the “Name” variable of the Character. The conditions prevent multiple names from being assigned to the same character.

Making It Easier - Combining Structures and Storage

You can make all of this much easier by SAVING the Structure variable into the Storage, and reading it from the Storage whenever you want.
Structures are a bit tricker to save, so what we have to do is:

// Save //
If scene variable "Save" = 1
  write ToJSON(Names) in "Names" of storage "A"

( Remember to pick the "Write a text" action, not "Write a value". )

// Load //
If "Names" exists in storage "A"
  read "Names" from storage "A" and store as text in "NamesJSON"
  parse JSON string VariableString(NamesJSON) and store it into variable Names

This way, the Structure will be written into the storage as a JSON string and can be loaded back into the game as a Structure anytime a character needs to be created again. ^^
If you want to store these values in the local storage from the beginning, maybe keep them in the first scene of the game and save them automatically into the local storage when the game starts? I’m sure there are better ways of doing it, but this is what I can think of right now!

I hope this helps!


This is great!
Man, this is probably what I was looking for.

I’ll try it first.

1 Like