+1 vote

I would like to add a .json file in my project to store data.
The problem is that I can't add such files from the editor. And if I put it directly in the project's folder I can't see it in my editor...

in Engine by (173 points)

Having the same problem, it seems that the language (or engine) simply can not handle json correctly. Which is really annoying. =/

1 Answer

+1 vote
Best answer

This is intended, Godot's Filesystem dock only shows built-in resources (the resource base class and all classes inheriting from it). JSON files are not built-in resources since they don't really need to be imported. You can still open them from code thought if you create them in your project's folder normally and get the data contained in them. Godot provides two helper classes as well as a couple of built-in GDScript functions. The 'Saving Games' tutorial is also worth checking out as it teaches using JSON to save game data.

by (299 points)
selected by

thx for your answer but the problem is that I can't have access to this json file (i put it in the project folder)... When I parse the file and ask for a certain value it just shows an error message saying that this value does not exist

Could you quickly paste the code you're using so that I can see if I am able to find a mistake?

i checked the tutorial you send to me and I think it's better to incorporate the dictionnary into a script. So I've something like :

extends Node

const ANIMALS = {
1:{
"class":1,
"name":"dog",
"rarity":2
},
2:{
"class":2,
"name":"cat",
"rarity":1
}

(...)

}

Is it a clean way of doing this ?

It is possible to do this, yes. However I personally would generally use it for smaller databases or data that directly originates from your gameplay code (such as save data).
It mostly comes down to personal preference, I myself am not a big fan of having huge dictionaries as a database, I usually use either JSON or a custom file format.
There is not really one distinct way to do this, storing data often depends on use case and personal preference. If this works for you then use it. I'd recommend to maybe research a bit about different ways to store data in games.

(This example with animals in particular kind of screams 'Classes' for me though xD)

ok thx ^^. But is there a way to use something else than a class ?

Sorry, I was busy and then forgot to answer. There is generally never only one way to solve a specific problem. Storing your animal data in a dictionary or a data format is completely valid. Programming is often about finding the most efficient solution for a problem.
Learning about OOP is somewhat essential in GameDev though, I do recommend learning about classes and such.

But man, json is a pretty comum format to save stuff in a quick and easy way. Why just don't let developers use it?! Just cant understand whats the big deal with it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories