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

0 favourites
  • 5 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • 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):

    {
    	"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\"]]]}"]
    		]
    	]
    }
    [/code:3334wj70]
    
    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!
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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 "".

  • 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!

  • 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).

    {
    	"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."]
    		]
    	]
    }
    [/code:32g4u4ru]
    
    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!
  • Good luck, and let me know if you have any other questions.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)