2020-05-11 10:34:48

Hello,
how can I deserialize my JSON file to a vector, array or some other kind of list? I need it for a school project so any help is greatly appreciated.
I really tried to like C++, I really did, believe me haha.

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-11 10:38:25

Oh, and if it will help someone help me, here's my JSON:
{"levelName":"Mroczny Las","LevelAuthor":"Nuno","BackgroundMusic":"music.mp3","Ambience":"pao.wav","Tiles":[{"TileName":"Na skraju lasu","TileDescription":"Stoisz sobie elegancko na skraju mrocznego lasu. Wokół siebie widzisz niekończące się łany zielonej... trawy, rosnącej sobie tutaj i wyglądającej tak, jakby to ona była panią tego miejsca","TileStepSound":"test.wav","TileSpriteFile":"test.ico","TileEffect":{"EffectName":"Nic","EffectDescription":"Podśpiewujesz sobie w rytm swych kroków","ModifyByValue":0}},{"TileName":"W mrocznym lesie","TileDescription":"Przekroczyłeś właśnie kurtynę drzew która zasłaniała ci dokładny widok na otaczający cię las. Doznania wzrokowe w istocie są piękne, górujące nad tobą drzewa nadają mrocznemu klimatu temu miejscu, natomiast rosnąca sobie w dole zielona trawa sprawia, że czujesz dziwny spokój na duszy podrużując przez ten teren. W oddali słychać od czasu do czasu krzyk jakiegoś ptaka lub nawet wycie wilka","TileStepSound":"test2.wav","TileSpriteFile":"las.ico","TileEffect":{"EffectName":"PulapkaNaNiedzwiedzia","EffectDescription":"Cholera! Wpadasz w pułapkę na niedźwiedzia, zastawioną tu pewnie przez jakiegoś kłusownika","ModifyByValue":30}}]}

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-11 15:06:53

picojson

I don't speak as good as I write, and I don't listen as good as I speak.

2020-05-11 15:22:29

You go down this list, namely under the JSON heading, and pick one you like. Then you use it.

JSON in C++ is difficult by the nature of C++, so don't expect this to be as easy as Python or anything like that.  Because of the static types, you usually have to specify the schema in some fashion, or deal with a lot of extra boilerplate.

I can say at least tolerably good things about Json-C, but it's C and not C++, and thus a much less convenient API (though one of the tolerably good things I can say is that it's not miserable to use).

My Blog
Twitter: @ajhicks1992

2020-05-11 16:44:01

Thank you both. Oh god, maybe I will just hardcode the thing and be done with it?

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-11 16:50:39

If hardcoding is an option, it's probably best to do so.  Most C++ serialization stuff will go through something like protocol buffers to avoid some of the "fun" here.  That or you get one of the libraries where you register your data structure with macros.  I'd have suggested hardcoding to start with but I'd assumed that this was part of the assignment.

Synthizer is literally preprocessing some data, then writing it directly to a C++ file as the proper data structures that I'd get if I parsed it, in part because I want to avoid this particular nightmare.

it's possible to do somewhat nice things with some creativity and/or if you find the right libraries but none of the shortcuts are the kind of thing I'd expect someone new to C++ to be able to handle.  There is nothing like complex preprocessor macros and operator overloading like you wouldn't believe for fun and profit.  I'm being 100% sarcastic and 100% serious at the same time, which should say a lot about C++.

My Blog
Twitter: @ajhicks1992

2020-05-11 17:54:02

I am sorry for my overall negativity here, but writing code for 3 other people and explaining it to them and writing it in the language you hate is not fun. I will just hardocde the game in the array.

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-11 18:18:52

I mean, I get it.  There's a reason I'm one of the most vocal don't use C++ voices around here despite using it myself.

My Blog
Twitter: @ajhicks1992

2020-05-11 21:03:26

So yeah, I need some kind of help, or something, I guess.
My game's gonna be a simple board game, so 1D only.
I have created a class Tile, which has the properties
string Name, string Description, std::Vector<item> Items.
Then a level class, which originally contained a name, author and vector of tiles, but as I decided to simplify the thing (I have a week or so to complete it) only the vector of tiles remained.
Then an item class which has a name and a description.
Then a player class which has a position, number of dice, and dice value (6 by default) and a vector of type item to store the inventory. There're even some methods, like move which moves the player by a given value, GetItem which apparently removes the item from the tile and puts it in the player's inventory and a method called HasItem which takes an item as an argument. It compares it with the inv and returns true if the player has the item.
Is that approach good or I should change something?

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-11 21:38:46

Your question cannot be answered with the information we currently have.
You say that you need help, but you don't actually tell us your problem. We know about your player class, which is fine and all, but we don't know where you're running into issues.

2020-05-11 21:52:36

I dont have any specific issues. My help request iss more of the advice type, whether my approach is good

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-11 22:40:36

It's probably fine.

If it's not fine, you have a week, after which you will never see this code again.  SO the proper question is is it fine for a week and then you will never see this code again.  Only you can answer this question since only you are taking the class, but in general there is such a thing as write once maintain never, and in a write once maintain never situation it doesn't matter as long as the approach gets you to the finish line.

If you're asking will your teacher like it: we don't know.  I and those I know in high school/college/whatever doing programming have had experiences that range from you have to do it in the most idiotic way possible because the teacher isn't good at programming, to you have to do it in the most idiotic way possible because the point of the assignment is to learn something specific, to wonderfully amazing classes where you're given full creative freedom and the teacher has the experience to not let their preferences get in the way of grading.

So it's kind of a pointless question as things stand.

My Blog
Twitter: @ajhicks1992

2020-05-14 11:15:06

WHy I have to be so fucking stupid? here's my problematic function:
int player::move_player(int move_by_value, direction d)
{
    switch (d)
    {
    case _d.up:
        player::player_position ++move_by_value;
        return player::player_position;
        break;
    case d.down:
        player::player_position -= move_by_value;
        break;
}
And direction.h
//direction.h
//ENumeracja opisuje kierunek, w którym porusza się gracz
#pragma once
enum direction
{
    up,
    down
};And the error I get:
Severity    Code    Description    Project    File    Line    Suppression State
Error (active)    E0020    identifier "_d" is undefined    Gra Na Informatykę    C:\Users\nuno6\source\repos\Gra Na Informatykę\Gra Na Informatykę\player.cpp    65

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-14 15:10:02 (edited by amerikranian 2020-05-14 15:16:59)

Change your declaration to int.  Enums create integers.  Also, adjust your case statements to look like so.
int player::move_player(int move_by_value, int d)
{
    switch (d)
case up:
//...
}

2020-05-14 15:40:58

That kind of enum is unscoped, meaning that you just refer to them as up and down without a prefix.

Using the name of the enum as the type in the function is correct and shouldn't be changed to int.

Also you will notice that _d is not in fact defined anywhere: the parameter is d, the header is direction. Your first case label tries to use _d.  If you haven't turned punctuation up in the screen reader so that you can hear _, you need to.

My Blog
Twitter: @ajhicks1992

2020-05-14 18:13:38

Ok that one's fixed, but I have another porblem which is hard to debug. The program launches just OK, but when I enter a player's name, it exits without warning.
The project can be found here. I share it so you can tell me what I did wrong
https://nunosoft.pl/dl/sample_game_c++.zip

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-14 18:21:18

I'm not going to debug your whole project. You want to figure out how to use VSCode or GDB on it and find out what went wrong.

My Blog
Twitter: @ajhicks1992

2020-05-14 18:29:22

So where I can find a guide how to debug with VS? I use the version 2019.

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-14 18:41:37

I don't know.  I use VSCode.  Compile in WSL (if you can) and then you can do gdb which is entirely command line and for which you can get many tutorials via Google.  But if this isn't console that's probably a nonstarter and you'll need to figure out how to get VS or VSCode working.  There isn't anything blind specific on these topics, but there are tons of tutorials for the sighted for any of them, read one and figure out how to translate it into screen reader with a hotkeys list or something.  You're not going to be able to just ask a bunch of small questions here and learn what you need to learn, you need to put in the time to do research.  Google is right there.  Start with C++ debugging.

My Blog
Twitter: @ajhicks1992

2020-05-14 18:50:41

OK... smile, I will research

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-14 18:53:30

Actually, one small tip. You can just stick printf everywhere and see which printf ran last, that can sometimes do the trick.

My Blog
Twitter: @ajhicks1992

2020-05-14 19:07:47

BTW what's the difference between printf and std::cout? I mean a noticeable difference

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2020-05-14 19:23:26

I believe cout is C++ specific.  Printf also uses modifiers for formatting  whereas cout doesn’t. At least, not in the examples that I have seen.

2020-05-14 19:40:06

The difference is that cout is an overnegineered mess.  There are good reasons to use C++ iostreams from time to time, but in general I attempt to avoid it and it's definitely overkill for printing out some debug info.

Also, yes, it does have format specifiers.  But because cout is an overengineered mess they work something like cout << hex() << mynumber and stupidity like that.

Unfortunately in C++20 we get std::path, which is a filesystem path.  Unlike C file I/O this handles unicode in a cross-platform manner, and it only works with iostreams, so synthizer sadly uses that for reading audio files from disk.  At least if I understood it right--ask me again in a couple months when I've actually tested Synthizer on unicode paths.  Fortunately in C++ 20 we are getting Python style string formatting and printing, but unfortunately no one has implemented it yet, so you can't actually use it.  But they are at least making this a bit better.

The one big advantage to cin/cout is type safety, but the trade-off is that they're not nearly so nice w.r.t "I need to quickly print a few variables on oneline" and you can get printf type checking with -Wall in GCC/Clang and something-or-other in VC++, so it's not like that's a big advantage.

My Blog
Twitter: @ajhicks1992

2020-05-15 14:41:34

How can I pass a newly created object to the function in C++? I mean, to initialize the object with its corruction inline of the function, in C# its something like MyFunction(new MyClass(property1, Property2));

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com