JSON Branch Dialogue Example

2
  • 1 favourites

Attached Files

The following files have been attached to this tutorial:

.c3p

json-branch-dialogue-example.c3p

Download now 182.52 KB

Stats

128 visits, 168 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

JSON Branch Dialogue Example

Preview:

Dialog File

using Array editor / Excel to prepare dialogue data.

The same ID will be considered the continuous conversation.

ID Name Text
Screen1_Intro Alice Hi, Bob! How are you today?
Screen1_Intro Bob I'm ok, tdank you, Alice. How about you?
Screen1_Intro Alice Perfect!

If the dialog has options, use Choice1-Text and Choice1-Next to mark them.

ID Name Text Choice1-Text Choice2-Text Choice1-Next Choice2-Next
Screen1_Fruit Alice I was just wondering, do you like apples or oranges better? Apples Oranges Screen1_Apples Screen1_Oranges
Screen1_Apples Bob I love apples! Tdey're my favorite fruit.
Screen1_Oranges Bob Oranges are great too, especially for a refreshing snack.

Dialog Parse

Next, the Array File will be processed into a JSON format.The same ID will merged into an array.

  • JSON
    	[
    		   {
    		       "ID": "Screen1_Intro",
    		       "Name": "Alice",
    		       "Text": "Hi, Bob! How are you today?"
    		   },
    		   {
    		       "ID": "Screen1_Intro",
    		       "Name": "Bob",
    		       "Text": "I'm ok, thank you, Alice. How about you?"
    		   },
    		   {
    		       "ID": "Screen1_Intro",
    		       "Name": "Alice",
    		       "Text": "Perfect!"
    		   }
    	]
    	
  • Array.json file

    Do you know? In fact, the C3 Array file has such a data structure inside it, so we can convert it.

    		{
    		  "c2array": true,
    		  "size": [7, 9, 1],
    		  "data": [
    		    [["ID"], ["Screen1_Intro"], ["Screen1_Intro"], ["Screen1_Intro"], ["Screen1_Fruit"], ["Screen1_Apples"], ["Screen1_Oranges"], [""], [""]],
    		    [["Name"], ["Alice"], ["Bob"], ["Alice"], ["Alice"], ["Bob"], ["Bob"], [""], [""]],
    		    [["Text"], ["Hi, Bob! How are you today?"], ["I'm ok, thank you, Alice. How about you?"],["Perfect!"], ["I was just wondering,  do you like apples or oranges better?"], ["I love apples! They're my favorite fruit."], ["Oranges are great too,  especially for a refreshing snack."],[""],[""]],
    		    [["Choice1-Text"], [""], [""], [""], ["Apples"], [""], [""], [""], [""]],
    		    [["Choice2-Text"], [""], [""], [""], ["Oranges"], [""], [""], [""], [""]],
    		    [["Choice1-Next"], [""], [""], [""], ["Screen1_Apples"], [""], [""], [""], [""]],
    		    [["Choice2-Next"], [""], [""], [""], ["Screen1_Oranges"], [""], [""], [""], [""]]
    		  ]
    		}
    		
  • Javascript Code
    		const c3ArrayFile = JSON.parse(localVars.source);
    		let result = {};
    		
    		for (let i = 1; i < c3ArrayFile.data[0].length; i++) {
    				  let id = c3ArrayFile.data[0][i][0];
    				  if (id && !result[id]) {
    				      result[id] = [];
    				  }
    		
    				  let obj = {};
    				  for (let j = 0; j < c3ArrayFile.data.length; j++) {
    				      let key = c3ArrayFile.data[j][0][0];
    				      let value = c3ArrayFile.data[j][i][0];
    				      if (value !== "") {
    				          obj[key] = value;
    				      }
    				  }
    		
    				  if (id && Object.keys(obj).length > 0) {
    				      result[id].push(obj);
    				  }
    		}
    		
    		runtime.setReturnValue(JSON.stringify(result));
    		
.C3P

json-branch-dialogue-example.c3p

Download now 182.52 KB
  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!