I have some Json files representing enemies in a game that I am trying to access and and copy into C++ variables.
{
"Wolf": {
"Type": 0,
"ID": 0,
"Level": 1,
"Name": "Wolf",
"Health": 100,
"Strength": 20,
"Speed": 35,
"Exp": 20,
"Defense": 30,
"Sprite": "Assets/Wolf_Sprite.png",
"Status": "Normal"
}
}
And here's the relevant part of my code
#pragma once
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
/******************************
* The Base Values of the enemy.
*******************************/
using namespace rapidjson;
class EnemyType
{
private:
std::string Name;
std::string FileName;
int ID;
int Level;
double expGiven;
double Health;
double Speed;
double Strength;
double Defense;
Document Doc;
public:
EnemyType()
{
FILE* pFile = fopen("Assets/Enemy_List/0.json", "rb");
char buffer[65536];
FileReadStream is(pFile, buffer, sizeof(buffer));
Doc.ParseStream<0, UTF8<>, FileReadStream>(is);
assert(Doc.IsObject());
assert(Doc.HasMember("Type"));
assert(Doc.HasMember("ID"));
assert(Doc.HasMember("Level"));
assert(Doc.HasMember("Name"));
assert(Doc.HasMember("Health"));
Health = Doc["Health"].GetDouble();
}
The issue is the file itself open correct and passes the isObject assert, however anything past that will crash without failure... Any help would be appreciated.
From the Stack window ucrtbased.dll!issue_debug_notification(const wchar_t * const message) Line 125 C++ Non-user code. Symbols loaded.
the error in the terminal: Assertion failed: Doc.HasMember("Type"), file c:\users\timothy\documents\visual studio 2017\projects\musungo game\musungo game\enemytype.h, line 36
edit: I found the answer was the .HasMember i was specifying the wrong word instead it should have been Doc.HasMember("Class")) instead
the error in the terminal: Assertion failed: Doc.HasMember("Type")
The reason for this error - Type is child of Wolf so it should be accesses as Doc["Wolf"]["Type"] and the assert should look like
assert(Doc.HasMember("Wolf"));
assert(Doc["Wolf"].HasMember("Type"));
Or with suggestion from user2350585
assert(Doc.HasMember("Wolf"));
auto Wolf = Doc["Wolf"];
assert(Wolf.HasMember("Type"));