How do I effectively use JSON strings & the C2 Data Editor

Get help using Construct 2

Post » Fri Jan 29, 2016 2:10 pm

Hello everyone,
I was searching for a solution to create a dialogue system that will be included in various adventure games / rpg's. And decided to purchase the C2 data editor to do the leg work, and it's awesome!
But, I'm having a complete blank moment, and have gotten a bit stuck with how to deal with accessing the nested data within the structure (in this instance, an array of strings that sit inside a conversation object).

Now some of the helper functions are useful in that they return certain row numbers based on your query, so that's great. But the main problem is what to do after that really.

For example (apologies if it looks a bit messy):
Code: Select all
{
   "c2array": true,
   "size": [2, 2, 1],
   "data": [
      [
         [1],
         ["{\"c2array\":true,\"firstRowIsColumnNames\":true,\"size\":[4,2,1],\"data\":[[[\"belongsTo\"],[\"str\"]],[[\"player\"],[\"Hi, I don't suppose you have any materia?\"]],[[\"npc\"],[\"Er, we're fresh out actually. But feel free to take a look anyway.\"]],[[\"player\"],[\"Oh, ok. Let's see what you have.\"]]]}"]
      ],
      [
         [2],
         ["{\"c2array\":true,\"firstRowIsColumnNames\":true,\"size\":[1,2,1],\"data\":[[[\"belongsTo\"],[\"str\"]]]}"]
      ]
   ]
}


Here I'd like to access the first set of data in the c2array, and, from within that, access each individual string. You might notice the 'belongsTo' property, that's basically where I want to end up.

So my conclusion was to store that nested data within a different array, then I can cycle through it and show it on the page using the array.at() expression.

But, my struggle comes when making the link between the JSON string, and then getting that sub data in to an array to access...

Hope this all makes sense, thanks for your time!
B
5
Posts: 22
Reputation: 283

Post » Sat Jan 30, 2016 4:51 pm

The Array object is naturally a 3D array, so you can access any point in the C2 Array by calling Array,At(x, y, z).

To import the data, you simply feed that string into Array.Load (From JSON) method.

NOTE: If you are feeding the string directly into C2 (i.e. typing it into the Json box yourself), then you need to escape all of the internal quotation marks with double quotation marks, like "".
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,349
Reputation: 4,692

Post » Wed Feb 03, 2016 9:30 am

Thanks for the reply mate, sorry it's taken a while to reply! I think I'm finally starting to understand. My brain was in a strange limbo between using recursion to get what I need, and the way C2 does things. But the 3D array stuff is making more sense now.

Hopefully it'll work out okay when it all clicks in to place! :) I'll post up on here how I achieved the result when I get something working properly.

Thanks again!
B
5
Posts: 22
Reputation: 283

Post » Wed Feb 03, 2016 1:53 pm

Alright then let's get down to business. So I've managed to split out what I need from the data structure I've had auto-generated by the C2 data editor. As I'm still a newbie, it doesn't 100% make sense yet, but I think I'm getting there. So I'll break it down in to sections in case it's useful to anyone else:

Task 1: Generate initial data structure and exported (not perfect, and has some un-required bits, but it's a start).
Code: Select all
{
   "c2array": true,
   "firstRowIsColumnNames": true,
   "size": [4, 2, 1],
   "data": [
      [
         ["belongsTo"],
         ["str"]
      ],
      [
         ["player"],
         ["Hi, I don't suppose you have any materia?"]
      ],
      [
         ["npc"],
         ["Er, we're fresh out actually. But feel free to take a look anyway."]
      ],
      [
         ["player"],
         ["Oh, ok. Let's see what you have."]
      ]
   ]
}


Task 2: As a test, extract two things a) the name of the entity saying the sentence, b) the actual sentence:

1) JSON string above is stored in a variable,

2) Load that string in to an array using 'load json string' action,

3) Set a new variable that gets the 'array.At(0, 1). This will basically go a level inwards and get us a sub array of entity names and strings.

4) Make a secondary array that will store the current indexed data from step 3

5) Within that new array, you can now access the data at (1, 0) and (1, 1), which will return the entity name and the string next to it.

It's a bit rough and ready, and still have to get my head around it some more (hence why it may not make sense just yet), but it's getting there.

I hope this helps anyway. Cheers!
B
5
Posts: 22
Reputation: 283

Post » Thu Feb 04, 2016 7:36 am

Good luck, and let me know if you have any other questions.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,349
Reputation: 4,692


Return to How do I....?

Who is online

Users browsing this forum: andersonaraujodl, imhotep22, orinab and 4 guests