Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 24, 2024, 09:26:24 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Entity Component Systems + JSON in C++
Pages: [1]
Print
Author Topic: Entity Component Systems + JSON in C++  (Read 1521 times)
badrobit
Level 0
**



View Profile
« on: September 14, 2016, 05:56:10 AM »

Hello All,

I am currently building my first Engine from the ground up and I want to build it around the ECS(S) pattern. In order to do so, I am hoping to leverage JSON as my method for storing and loading information about the various components that the game will employ.

My reasoning behind this is two-fold, one having JSON as the method for storing information means that the data is a bit more decoupled from the code and second this allows for easier modding later on down the line.

My question is this: IS this a good idea? If so has anyone seen something that implements this? I have been looking but cannot find anything yet.

Thanks in advance for your help!
badrobit
Logged
roguesleipnir
Level 0
**



View Profile
« Reply #1 on: September 14, 2016, 07:07:16 AM »

What kind of game are you making?

I've done something similar in Cocos2dx (C++) for a tactics game.
I built the base units and abilities in code, then we just assemble the parts in JSON to make unique units.

I doubt it was true ECS, though.
Then again, it's just a pattern and almost no one follows it exactly in production.
Logged
badrobit
Level 0
**



View Profile
« Reply #2 on: September 14, 2016, 07:11:51 AM »

The game is going to be a Space-based 4X with a bit more of a "city building" focus and less on the "fighting" aspect of the 4X.

Resource gathering and management are going to play a big part in the system and is something that I would like to be able to expose to users through being able to add anything they want really to the game so long as they follow the standard I provide for the JSON files (which will be used to create components).

I am also like you not 100% dead set on following the ECS(S) pattern to the letter, there are parts of it I like and others I am not so sure of which is why I am asking people for their opinions Smiley.

I hope this helps clarify my question a little bit.
Logged
roguesleipnir
Level 0
**



View Profile
« Reply #3 on: September 14, 2016, 07:23:31 AM »

One thing we were also able to do was to go from Spreadsheets before turning those into JSON for input.
That made comparing and balancing units more streamlined since we would only need to change values on one source file.
You can even host the sheet on google docs.
Logged
badrobit
Level 0
**



View Profile
« Reply #4 on: September 14, 2016, 07:39:26 AM »

Yeah I had thought about doing something like that, maybe I could even get lucky and find a system that could go directly from tables to JSON.

The more I talk about this out loud (read: chat here) the more it makes sense to me anyway.

How did you guys handle persistence?
Logged
roguesleipnir
Level 0
**



View Profile
« Reply #5 on: September 14, 2016, 05:06:23 PM »

For units, we just basically kept a JSON with their id's and EXP.
Our level up system was very linear so we just recalculated their stats when they're spawned.
Logged
badrobit
Level 0
**



View Profile
« Reply #6 on: September 14, 2016, 06:58:58 PM »

Thanks for all the info! Was there a particular JSON library that you guys found useful in C++?
Logged
roguesleipnir
Level 0
**



View Profile
« Reply #7 on: September 15, 2016, 12:20:26 AM »

I think it was rapidjson.

For parsing sheets to json, we just made our own in C++.
We also made one in Python.
Logged
djr
Level 0
***


Smith and Winston Coder


View Profile WWW
« Reply #8 on: September 15, 2016, 05:06:11 AM »

This is exactly what (any why) we used in the Editor for Smith and Winston.

The only wrinkle was storing the data for the voxel blocks which we needed to compress and encode for text files because it was massive.

It made merging multiple level changes easy in SVN as well which was a bonus.

We used jsoncpp but we didn't put a lot of thought in to it tbh.
Logged

badrobit
Level 0
**



View Profile
« Reply #9 on: September 15, 2016, 08:16:24 AM »

@roguesleipnir

Thanks for the info, I assumed I would probably have to roll my own solution to go from sheets to JSON  Smiley

@djr

Just read through your dev log super interesting and I love the look of your system! I am actually still very much in the planning stage and I am not sure if I am going to go voxel or low poly for my look its a bit of a toss up between the two but where it is "space" based I think I may have to focus more on low poly no matter how much I love the look of voxels  Shrug

I was wondering if you could talk a little bit about the wrinkle you mentioned when it came to storing the game state that is one of the things I am worried about because my game area is going to be on the scale of a solar system with a lot in it so storing the game state is a big concern for me.

Thanks all for your feedback!
Logged
oahda
Level 10
*****



View Profile
« Reply #10 on: September 16, 2016, 12:23:29 AM »

Here's a thread I read on this a while ago:

https://www.quora.com/What-is-the-best-C-JSON-library

Didn't seem very positive. Various issues with all of the option. But maybe you won't bump into those.
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #11 on: September 16, 2016, 04:46:22 AM »

I'll throw in a vote for rapidjson. I don't remember having any issues with in the past. Got the job done Smiley
Logged

badrobit
Level 0
**



View Profile
« Reply #12 on: September 16, 2016, 05:05:00 AM »

Thanks, it is looking from all your help and from the good old Dr. Google that rapid JSON might be my best option.

Now I just need to think through persistence before I get too far down this road.
Logged
djr
Level 0
***


Smith and Winston Coder


View Profile WWW
« Reply #13 on: September 16, 2016, 09:10:25 AM »

@djr

I was wondering if you could talk a little bit about the wrinkle you mentioned when it came to storing the game state that is one of the things I am worried about because my game area is going to be on the scale of a solar system with a lot in it so storing the game state is a big concern for me.

The problem we faced was that the voxel data is too big to be stored as a massive json array so we 7zip and base64 encode it as a string so we can store it in the JSON file. This makes it a lot smaller but it takes time to do.

For a whole universe that is constantly changing you'd be better off storing changed chunks of the voxel world as binary data so you can load the changes quickly.

Never underestimate how much memory voxels can consume!

Good luck.
Logged

badrobit
Level 0
**



View Profile
« Reply #14 on: September 16, 2016, 11:32:53 AM »

The problem we faced was that the voxel data is too big to be stored as a massive json array so we 7zip and base64 encode it as a string so we can store it in the JSON file. This makes it a lot smaller but it takes time to do.

For a whole universe that is constantly changing you'd be better off storing changed chunks of the voxel world as binary data so you can load the changes quickly.

Never underestimate how much memory voxels can consume!

Good luck.

I think that you are correct on how I will have to proceed. I was hoping I could get your feedback on my thoughts on how to approach this.

  • Use JSON as a method for having component in their RAW form be human readable
  • These would then be read in on game load so that a library of all currently available components is known.
  • Game world and game state would be saved in some binary format(non-human readable) for speed and on game load the data in the "game save" would be validated against the currently available list of components, and recreated so that they can continue to be used once the game resumes.

I think this would give me the best of both worlds making the system easier to understand and in the future expand (mods) while not taking up large amounts of resources in order to save the current game state in a human readable format. To be honest, the more I think about it having the game save be human readable seems a bit of a waste of time and effort and doesn't really provide an upside.

Thanks again for your input it is so good to start to get some of this stuff out of my head and onto "paper"!
Logged
badrobit
Level 0
**



View Profile
« Reply #15 on: September 17, 2016, 01:03:58 PM »

To anyone still following this:

I have gotten pretty far into the implementation of my ECS framework and now I am focusing on the loading of components from JSON into the engine's "component pool".

The idea I had was that I would define the basic component types that I want my game to be able to implement. This would be a 1:1 mapping from code to JSON. The question I am now facing is how do I load all of the various types of components into memory so that I can create instances of them.

For example:
The game will be based around colonizing various parts of a solar system and obtaining, processing, using and trading resources. As such I will have a "ResourceComponent" that will define the various aspects of a what a resource can be in code. I will then have a series of JSON files that describe the various resources that are available in game (dirt, crystals, etc...). I have written the component so that it has all of the attributes that I would like for a resource component to have I am just not 100% sure how to bridge this gap.

Note: The problem isn't loading the JSON either that has been handled using a 3rd party library that was suggested earlier in this thread: https://github.com/miloyip/rapidjson

Thanks again for your help.
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #16 on: September 20, 2016, 04:55:09 PM »

So a quick answer is you could add Serialize/Deserialize methods to your components interface. In there you do your validation and parsing of json then construct the object.

So you take in an entity definition json file and as your parser hits each component block it hands it off to some specific parser/construction function.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic