Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411431 Posts in 69363 Topics- by 58417 Members - Latest Member: gigig987

April 20, 2024, 04:18:15 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)General thread for quick questions
Pages: 1 ... 7 8 [9] 10 11 ... 69
Print
Author Topic: General thread for quick questions  (Read 134720 times)
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #160 on: August 27, 2015, 12:37:08 AM »

It doesn't help that teh simplejson example are in unityscript which hide the type Sad

4h37 going to sleep, what a waste of time ...
Logged

zleub
Level 0
**


View Profile WWW
« Reply #161 on: August 27, 2015, 12:44:05 AM »

I get it. I'm sorry not to have Unity right now, i can't do anything. I hope someone else can help you more precisely.
Logged
Layl
Level 3
***

professional jerkface


View Profile WWW
« Reply #162 on: August 27, 2015, 01:50:40 AM »

It always baffles me how unaware Unity developers tend to be of the larger C# ecosystem. The general standard for JSON parsing in C# is Json.NET, an amazingly efficient JSON implementation for .NET that makes use of Reflection.

http://www.newtonsoft.com/json

It parses directly from and to objects and has plenty of options for adding custom serializing. That's good because in Unity you'll need those options to serialize the Unity types. Especially Vector3 tends to break down as it tries to serialize all properties, but it's straightforward to add a custom converters for those.

When you've got that up and running, you can do this:

Code:
class SaveData
{
    public string PlayerName {get;set;}
    public int Score {get;set;}
}

var data = new SaveData { PlayerName = "Steve", Score = 9001 }
var json = JsonConvert.SerializeObject(data);
var dataAgain = JsonConvert.DeserializeObject<SaveData>(json);

Next to that, if you actually need to work with raw JSON key-value tables, it's got helper objects for that as well.
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #163 on: August 27, 2015, 04:48:16 AM »

It always baffles me how unaware Unity developers tend to be of the larger C# ecosystem. The general standard for JSON parsing in C# is Json.NET, an amazingly efficient JSON implementation for .NET that makes use of Reflection.

http://www.newtonsoft.com/json

It parses directly from and to objects and has plenty of options for adding custom serializing. That's good because in Unity you'll need those options to serialize the Unity types. Especially Vector3 tends to break down as it tries to serialize all properties, but it's straightforward to add a custom converters for those.

When you've got that up and running, you can do this:

Code:
class SaveData
{
    public string PlayerName {get;set;}
    public int Score {get;set;}
}

var data = new SaveData { PlayerName = "Steve", Score = 9001 }
var json = JsonConvert.SerializeObject(data);
var dataAgain = JsonConvert.DeserializeObject<SaveData>(json);

Next to that, if you actually need to work with raw JSON key-value tables, it's got helper objects for that as well.

Does it work with Unity and its anemic .net implementation?


EDIT : A quick google search seems to indicate it doesn't work with Unity and while some people have managed to wrangle it, even then it still fails on AOT IOS compiles. Someone made a version that does work for 25$ https://www.assetstore.unity3d.com/en/#!/content/11347
Logged

Layl
Level 3
***

professional jerkface


View Profile WWW
« Reply #164 on: August 27, 2015, 05:02:16 AM »

EDIT : A quick google search seems to indicate it doesn't work with Unity and while some people have managed to wrangle it, even then it still fails on AOT IOS compiles. Someone made a version that does work for 25$ https://www.assetstore.unity3d.com/en/#!/content/11347

I have used it with Unity (pre-5) before. That 25$ plugin is a total ripoff. It's currently in the game From the Depths loading in ship saves. I don't know if it has iOS support on the version of Mono shipped with Unity though.

Edit: Again the tricky thing is that you need custom converters for Unity's types. They're easy to implement though.
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #165 on: August 27, 2015, 05:38:27 AM »

The issue seems to be with the AOT compiler. Does JSON.net use expression trees or some other form of code generation at runtime?
Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #166 on: August 27, 2015, 09:05:05 AM »

it doesn't matter because nobody adress my point directly, nobody pay attention to my use case Cry

It's not a case of serialization then deserialization (which is plenty documented) there is no coupling to begin with

but load unknown json to look at POST conversion from text.

Json is sold as "just load it works", but it's not true, there is a LARGE amount of dependent coupling in the general use case of the format.

I just need a format that I can load and freely access its structure's data

I think xml allow me to do just that, list the existing node THEN access their value, too bad my data isn't in xml (and while xml to json is guarantee, the reverse is not)

Json is just useless if does not allow that to me

EDIT

ThemsallTook mention accessing the root, how I can do that?

I want to list the (first level) key first, then access their value later (in string at least)

edit:
How to read all keys in Json without specifying keyname
« Last Edit: August 27, 2015, 09:22:52 AM by Jimym GIMBERT » Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #167 on: August 27, 2015, 10:28:50 AM »

it doesn't matter because nobody adress my point directly, nobody pay attention to my use case Cry

It's not a case of serialization then deserialization (which is plenty documented) there is no coupling to begin with


I think you've misuderstood. If you just want something that is structured similarly to how CSV is, it's be like this in JSON.

{code}
[
{'col1': 'value1', 'col2': 'value2'},
{'col1': 'value1', 'col2': 'value2'},
...
]
{code}

And you can easily load it into an Dictionary<string, string>[], and "freely access" it in the totally obvious way.Virtually all json libraries let you load JSON into Dictionary or Dictionary like things if you need it. Do you need a more concrete example of how to do it?

Anyway, the point is you'll be missing half the advantage of using the format, which is that it maps nicely to actual classes.
Logged
Layl
Level 3
***

professional jerkface


View Profile WWW
« Reply #168 on: August 27, 2015, 11:14:15 AM »

edit:
How to read all keys in Json without specifying keyname

This sounds like a library issue, not a JSON issue. XML does nothing that JSON can't, they're just languages.
What I think you're looking for is supported by Json.NET like this:

Code:
var json = "{'key1': 'value1', 'key2': 'value2'}";
var jobj = JObject.Parse(json);

foreach (var jchild in jobj.Children())
{
    Console.WriteLine(jchild);
}

This will output:

Code:
"key1": "value1"
"key2": "value2"

Within Json.NET, JObject lets you look at the actual structure of the Json.
Keep in mind that this isn't really meant for heavyweight loading of data, you're expected to just load directly into a structure with that. There's not really anything stopping you from using this, there's just no good reason to when you can directly deserialize into an object.

Edit:
Though really I'm not fully understanding what your use case is
Logged
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #169 on: August 27, 2015, 11:57:47 AM »

The use case is that I'm parsing an "unknown" "database" (conceptnet 5.3 http://conceptnet5.media.mit.edu/ Json sample http://conceptnet5.media.mit.edu/data/5.3/c/en/abdomen)

I want to go through the database line by line and look at pattern of data per field (with automated check) to be able to parse it to my liking. Basically having a function that compare all similar field to each other, extract unique elements into a list, etc ... ie do some analysis on the data structure first.

I want to skip parsing the text and go straight for the data because I have notice inconsistency with at least the csv format (varying member length, multiple data in a single field).

The type is the json field need to be further parse because they are custom type ("/a/[/r/wordnet/adjectivePertainsTo/,/c/en/abdominovesical/a/of_or_relating_to_the_abdomen_and_the_urinary_bladder/,/c/en/abdomen/n/the_region_of_the_body_of_a_vertebrate_between_the_thorax_and_the_pelvis/]")

however What you just post seems exactly what I need.

I understand the generic use case of JSON, but I have good reason because the structure isn't known in advance and I need to analyse first before to be sure I make a structure that conform the actual data.

EDIT:
does Json.net allow me to look at depth of structures? Cursory look at the JSON sample of conceptnet show that it has a field I overlooked previously "edge" and numFound, which is exactly why I want to automate the data analysis
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #170 on: August 27, 2015, 03:20:04 PM »

yeah that's a pretty straightforward fragment of json. Just iterate through the array of objects in the object called "edges".

Why do they bother to add a field for the number of elements?
Logged

gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #171 on: August 27, 2015, 03:38:02 PM »

I think it's the query structure not the downloadable flat database.
I'll try a light parser on the json
but I think a code that match and locate string, like for example finding key, register it, and place the cursor after it to detect arbitrary key. I also need to compare all the keys to each structure to see if its consistent in number of key (I expect more than the csv).
But I would like to stay as hi level as possible, the goal is not to build a generic parser
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #172 on: August 28, 2015, 12:22:56 AM »

Jimym, if I've understood you, you are considering reading certain fields without parsing the entire file by writing your own parser. You cannot do this in JSON. You cannot do this in XML. If you attempt to do it, it'll probably break in some circumstances.

Why bother? Parsing JSON is pretty fast. And once parsed, all these manipulations are easy.

Also, why bother checking the keys. The documentation literally tells you all the ones of interest.
Logged
Halogen
Level 0
**



View Profile WWW
« Reply #173 on: August 28, 2015, 07:15:27 AM »

Alright, so I'm going through a book right now to learn java, just to get general programming knowledge. I chose it because it is similar to C++ which I plan to learn after getting a handle on Java. I've never learned a programming language before, besides some minor scripting in GM. Do you guys think Java would be a good option for me making some simple game-like programs until I have a good enough grip on coding to move on to C++?
Logged
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #174 on: August 28, 2015, 07:50:05 AM »

Jimym, if I've understood you, you are considering reading certain fields without parsing the entire file by writing your own parser. You cannot do this in JSON. You cannot do this in XML. If you attempt to do it, it'll probably break in some circumstances.

Why bother? Parsing JSON is pretty fast. And once parsed, all these manipulations are easy.

Also, why bother checking the keys. The documentation literally tells you all the ones of interest.

The documentation is not as thorough as it seems, it's not reliable enough, the order of data don't match, some field are sometimes omitted randomly in practice, and argument may have multiple proposition which is not mention. There is also redundancy that threw out the parsing, especially with multiple proposition. It doesn't exhaustively list all type of data ex: edge type and most notably beyond the 4th parameter of concepts which is important to me, I need to reconstruct a list of those parameter too.

That's why I freak out initially.

I'm also not a real programmer with real training, basic 1st year stuff like parsing, sorting, database, etc ... thing that just need rot learning of basic pattern ... that's what is hard to me. I'm just educated on programming because I like it (I'm a designer first though)

I found out how to access the meat after running all night a series of checks in the sample data.
ie: ignore the first element, get the 2nd to 5th elements, ignore beyond where the instability happen (assuming it's generalizable to all data)

Problem: I leave out surface text because it's harder to parse and in the unstable area Huh? but it's important too

Regarding the Json xml, that's not it, I want to cycle freely through the structure to be able to compare consistency, if possible without having to write a full parser (ie using existing libraries), the idea is that json should be more consistent that the csv they give, because field would be null instead omitted and labelled, they are also likely to retain the same structure across all data, which put the burden on the value structure instead of the whole data.

edit:

1 data line
Quote
/a/[/r/Antonym/,/c/en/abdomen/,/c/en/torso/]   /r/Antonym   /c/en/abdomen   /c/en/torso   /ctx/all   0.014355292977070055   /s/site/verbosity   /e/0d01785baa93174b791991457c86f313a18054b8   /d/verbosity   [[abdomen]] is not [[torso]]

CSV parsed and annotated (manually green = desirable)
Quote
/a/[/r/Antonym/,/c/en/abdomen/,/c/en/torso/] uri

/r/Antonym relation

/c/en/abdomen parameter 1

/c/en/torso parameter 2

/ctx/all context

0.014355292977070055 weight

/s/site/verbosity source (can be complex with nested data and /or/ and /and/ operator)

/e/0d01785baa93174b791991457c86f313a18054b8 id

/d/verbosity dataset

[[abdomen]] is not [[torso]] surface text

features?
licenses?


Also I'm parsing 5.3 now they have moved to 5.4

edit:
you can also have multiple data line for the same concept with minor variation (surface text for example)

Json sample

Code:
{
      "context": "/ctx/all",
      "dataset": "/d/conceptnet/4/en",
      "end": "/c/en/drink_water",
      "features": [
        "/c/en/cat /r/CapableOf -",
        "/c/en/cat - /c/en/drink_water",
        "- /r/CapableOf /c/en/drink_water"
      ],
      "id": "/e/54e238cbf42cb02560abee949ff39ce8eeafde92",
      "license": "/l/CC/By-SA",
      "rel": "/r/CapableOf",
      "source_uri": "/or/[/and/[/s/activity/omcs/omcs1_possibly_free_text/,/s/contributor/omcs/clburke/]/,/and/[/s/activity/omcs/omcs1_possibly_free_text/,/s/contributor/omcs/cralize/]/,/and/[/s/activity/omcs/omcs1_possibly_free_text/,/s/contributor/omcs/meganraby/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/chaizzilla/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/craleb/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/dragonjools/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/kurt_woloch/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/leighman/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/logjac/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/mcandag1/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/rossjesse/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/rspeer/]/,/and/[/s/activity/omcs/vote/,/s/contributor/omcs/scarfboy/]/]",
      "sources": [
        "/s/activity/omcs/omcs1_possibly_free_text",
        "/s/activity/omcs/vote",
        "/s/contributor/omcs/chaizzilla",
        "/s/contributor/omcs/clburke",
        "/s/contributor/omcs/craleb",
        "/s/contributor/omcs/cralize",
        "/s/contributor/omcs/dragonjools",
        "/s/contributor/omcs/kurt_woloch",
        "/s/contributor/omcs/leighman",
        "/s/contributor/omcs/logjac",
        "/s/contributor/omcs/mcandag1",
        "/s/contributor/omcs/meganraby",
        "/s/contributor/omcs/rossjesse",
        "/s/contributor/omcs/rspeer",
        "/s/contributor/omcs/scarfboy"
      ],
      "start": "/c/en/cat",
      "surfaceEnd": "drink water",
      "surfaceStart": "Cats",
      "surfaceText": "[[Cats]] can [[drink water]]",
      "uri": "/a/[/r/CapableOf/,/c/en/cat/,/c/en/drink_water/]",
      "weight": 4.523561956057013
    },

Notice it use a weird order (alphabetical not conceptual closeness) and they shift the naming completely for whatever reason, so you have to guess which is what, but I don't need to parse surface text anymore!
« Last Edit: August 28, 2015, 08:15:36 AM by Jimym GIMBERT » Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #175 on: August 28, 2015, 08:54:54 AM »

Alright, so I'm going through a book right now to learn java, just to get general programming knowledge. I chose it because it is similar to C++ which I plan to learn after getting a handle on Java. I've never learned a programming language before, besides some minor scripting in GM. Do you guys think Java would be a good option for me making some simple game-like programs until I have a good enough grip on coding to move on to C++?

I don't see why not. Take a look at this

https://libgdx.badlogicgames.com/
Logged

Layl
Level 3
***

professional jerkface


View Profile WWW
« Reply #176 on: August 28, 2015, 11:47:53 AM »

Alright, so I'm going through a book right now to learn java, just to get general programming knowledge. I chose it because it is similar to C++ which I plan to learn after getting a handle on Java. I've never learned a programming language before, besides some minor scripting in GM. Do you guys think Java would be a good option for me making some simple game-like programs until I have a good enough grip on coding to move on to C++?

If you're planning to just make your own stuff, it's unlikely you'll benefit from switching to C++. It's a good idea to look around the different languages and see what you like most though. (I'm personally very happy with Rust as an ex-C/C++ and C# developer)
Logged
Afinostux
Level 2
**

Forgotten Beats


View Profile
« Reply #177 on: August 28, 2015, 05:06:14 PM »

Anyone here good with math?

I need to convert a desired jump height into a velocity. I'm working with a very simple euler motion (dy += gravity, y += dy).

So far what I've come up with "works", but is definitely not right.
Code:
inline
float jumpVelocityFromHeight(float pixels, float gravity)
{
   float jeq = (pixels + 8) * gravity * 2;
   return sqrt(jeq);
}

the pixels + 8 is in there because it undershoots by half a tile per 30 tiles of distance. With that in there, it pretty consistently overshoots by a little bit, which is good enough to get it working for now. Ideally, I'd like to have the player come to a stop in the air a precise number of pixels above the ground. Normally not terribly important, but I'm getting into some procedural platform placement code, and I'd like to be able to place platforms to avoid unintentional sequence breaking. Thanks!
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #178 on: August 28, 2015, 05:08:46 PM »

Remove that sqrt() and you should be good to go. Why do you even have that in there?
Logged

Afinostux
Level 2
**

Forgotten Beats


View Profile
« Reply #179 on: August 28, 2015, 05:34:43 PM »

Jumps are parabolas. That needs to be there.
Logged

Pages: 1 ... 7 8 [9] 10 11 ... 69
Print
Jump to:  

Theme orange-lt created by panic