2018-06-16 22:04:52

Hi.
In bgt you only had one option when sending data over the network or to a webserver. You had a string.
When trying to communicate with a webserver *for a rest api or something( offen it would send back a json response which we couldn\t use for anything.
Well not any more>
I give you: JSON in BGT.

It can convert a dictionary to a json formatted string and back. An example is in the GitHub repository. This can both be used, to send data over the bgt network or to a webserver. It can also be used to decode  responses send from a webserver or from another place.
You can use this to save maps, game data or what ever you want.

Feel free to add new features to it, just if you do please make a pr back, so everyone can benefit from it.
Here's the link (with download / examples and more):
https://github.com/NicklasMCHD/BGTJson

Hope you enjoy smile
- NicklasMCHD

If you like what I do, Feel free to check me out on GitHub, or follow me on Twitter

2018-06-16 23:37:30

Hate to say it... but I'm honestly not impressed. BGT just needs to go down the drain. Good thing about Python? You can write JSON in Python dictionaries since the syntax of JSON matches Python dictionaries.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-06-17 00:38:10

What ever Ethin. I kind of agree (because I know other languages) but still... Nothing gaming wise still works. Pygame, PyAudiogame, game libraries for java... Honestly even knowing that bgt isn't getting updated, it still works --- for now...

If you like what I do, Feel free to check me out on GitHub, or follow me on Twitter

2018-06-17 01:56:00

@3, Pygame works if you no how to use it, and there are lot of tutorials around to show you how. smile

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-06-17 19:58:33

Maybe @4. But it is still abandoned, just like bgt.

If you like what I do, Feel free to check me out on GitHub, or follow me on Twitter

2018-06-21 21:58:59

At ethin, we get it, you don't like bgt. Stop talking down and shoving your oppinions down someone's throat. Please and thanks.

Ivan M. Soto.
Feel free to check out my work and services.
http://ims-productions.com

2018-06-21 22:19:16

@6, shoving my opinion down your throat? Oh please. Ever heard of "skipping a post"? Honestly your more confrontational than I am.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-06-21 22:32:57 (edited by Munawar 2018-06-21 22:35:28)

Hi,
I took a look at your code and I like what you've done with it. BGT looks exactly like other C-Style languages and since those types of languages are my background it was easy enough to follow.

I have one suggestion for you that will give you quicker run-time results.

In lines 36 and 37 you call the get_keys() method. I would suggest to change this so that before you execute your loop, you store the result of get_keys() into a String array (or whatever the return type of get_keys() is.) The reason for this is that in other languages, dictionaries are implemented as hash tables, so while value lookup is constant time given some key, collecting keys is an expensive process because the methods will actually allocate new memory for the keys array.

Now, I don't know how BGT implements get_keys(), but if it went with the "safe" option, it's allocating memory twice per iteration: one for your loop condition, and once inside your loop on line 37: string key = unparsed_data.get_keys()[x];

The former might be eliminated if the BGT compiler does any sort of optimization, but in worst case this means you're allocating 2n memory, not bad for small dictionaries but imagine if you wanted to convert thousands of records to JSON?

So, remove the calls to get_keys() and replace them with an array reference. Something like this:

String[] keys = unparsed_data.get_keys();
uint n = keys.length; // Use constant here so we're not calculating the length of the array on every iteration.
for (uint x = 0; x<n; x++) {
string key = keys[x];
...

Otherwise, looks good and I hope people can use it!

2018-06-21 23:25:06

Maybe he did it that way because if storing into a dictionary, BGT is god awfully slow with dictionaries, unlike Python and other languages. I think BGT devs will go out of their way to not use them, unless its going to remain fairly small.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2018-06-22 01:54:38

Hi Munawar.
You know that you just can make a pull request on the git repository with the fixes right smile
All that aside I will take a look at it if you don't do smile
- NicklasMCHD

If you like what I do, Feel free to check me out on GitHub, or follow me on Twitter

2018-06-22 02:48:35

@ironcross32: He's iterating over a dictionary, so he is actually using a dictionary. In fact, I had no idea there was a noticeable performance penalty with BGT's dictionary implementation, so my post holds tenfold now because he will avoid the penalty by implementing my suggestion.

@NicklasMCHD: I never improve people's code for them as a matter of principle. The best way to learn is by doing, and I'm a firm believer in giving someone pointers so that they can improve on their own. Either way, I've given sample code there that you can pretty much copy-and-paste. smile

Never, ever let anyone code for you.

2018-06-22 02:51:57

I'm not good enough with it to ever have the need of doing it, but I know other BGT devs avoid them like the plague, certainly not storing game maps in them.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2018-06-22 03:02:16

Yes, storing maps in a dictionary has been proven to be slower than alternatives. Especially because they are generally stored 1 tile at a time. Take this example:
"1,1" => 3 (or "grass" if you are taking the strings route)
"1,2" => 2
Generally better practice in BGT is to store maps in arrays.

Sincerely,
Lucas.

2018-06-22 03:14:59

Yup, but he still wrote the library to convert a dictionary to JSON. Whether it's generally used or not is beside the point. I don't think it's appropriate to discuss using or not using a dictionary on this thread, since his library is general purpose right.

And, really, JSON is much easier to work with when represented as a map / dictionary structure. Storing data that will, in future, be converted to JSON in parallel arrays is a little bit more difficult and error-prone than using a map structure and converting that, because you don't have the problem of running out of bounds with respect to mismatched array sizes and all that.

I think it's awesome OP has tried his hand at building a library that someone besides the naysayers will find useful. Even seemingly irrelevant code on GitHub gets used from time to time. Let's not discourage people here.

2018-06-24 00:53:18

Hi.
So I've implemented the code changes suggestions by @Munawar in post 8.
Which basicly mean, that if you're working with a big set of json data, it should now save (encode) a bit faster.
- NicklasMCHD

If you like what I do, Feel free to check me out on GitHub, or follow me on Twitter

2018-06-24 22:05:49

dictionaries are rather well performing unless you do the one tile at a time, thing. An because of the way JSON works, really a dictionary is the only equivalent bgt thing. It's basically a key/value pair. Currently though if you try to retrieve as an int from any JSON dump, even if it's supposed to be an int, you get nothing becuase it does everything as strings...

----------
An anomaly in the matrix. An error in existence. A being who cannot get inside the goddamn box! A.K.A. Me.