How do I use the new JSON addon to call data from csv cells?

1 favourites
  • 13 posts
From the Asset Store
Kids Game
$49 USD
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • Hi there!

    I'm currently in the process of deprecating some 3rd party plugins from my game so i can move over to the new C3 runtime. One of which is rex's CSV addon, which served me greatly in the past to call DialogueLines for Cutscenes in my game.

    Why not just use an array?

    Now, people often suggest i should just use an array and the array editor but here's the problem:

    I tried that but i can only call upon data based on an Index value whereas with the CSV Addon i can call upon date based on their column- and row-names. So let's say i have Dialogue_Line_1 through 8. I just call upon a variable that determines the dialogue name and add a "_1" to it. So it always starts the right one. Each dialogue line then calls a function when the user presses space. That can either be advancing to the next line or ending the dialogue or whatever function you might want (i.e. the character moving around the screen after a dialogue has ended etc.).

    Now let's assume i've written 300 lines of dialogue (which is what i about have by now) and want to add a line to the Dialogue_1 row. I just insert a row, renumber the cells in the csv and everything still works, because it calls upon the Cell Name and not an index. If i would do this in an array, it would obviously screw up the index numbers of all following lines of dialogues and i would have to go through the entire game and renumber the index which is being called upon. This, of course, is very unfeasable. So CSV worked really great for me thus far.

    Now to the actual Question:

    I've seen some CSV to JSON converters online. Is there a way, with which i could use the new JSON addon to call upon individual "cells" of the csv (now in JSON) form? In that case, i wouldn't need the CSV addon anymore. Unfortunately, i have zero experience with any JSON addon from Construct and the documentation for it doesn't seem to be available by now.

    Hope this makes sense and i'd be glad to elaborate if something's unclear. Kind of a difficult problem to explain but maybe someone can just give me a crash rundown on how to use the JSON addon (calling upon data (i think i would need the Get Path or something?)etc.) and i might be able to figure it out myself :)

    Thank you in advance!

  • You can create an array with the same structure you had in CSV, and just make a couple of helper functions. See this demo I made for some other post:

    dropbox.com/s/rplr7qobzsds9qf/InventorySystem.c3p

    The array data is stored in allitems file, first column contains item properties, first row contains item codes. There are several functions in "Items Array" group which can return any property for any item. You can also make a universal function similar to CSV.At() expression, which will take two parameters (item code and property), scan the array and return the value.

  • joelmayer The documentation should be out when the r121 updates move to a stable release - which should clear up how to use the plugin.

  • joelmayer as mentioned above we aim to do the documentation for new features when the following stable release comes out.

    As a pure data format JSON is much nicer and a lot more flexible than CSV, and isn't too hard to work with. You may find it easier in the long term to just rewrite your dialog file in the JSON format instead of converting it from CSV.

    all references to arrays here are talking about JSON arrays, not the array plugin

    Crash course in JSON

    JSON is structured similar to how you would think of folders and files. There are 2 types of "folder"; arrays and objects. You start an arrays use square brackets and an object with a curly braces. Arrays have no label, so you access their contents by index. Objects have a label for each "file", these labels are strings. You can put any type of "file" or "folder" in any "folder". Every "file" or "folder" in JSON has a unique path that you can access it with.

    Crash course in JSON plugin

    If your trying to read data the first thing you want to do load it in. Get your data with the AJAX plugin and use the JSON.Parse action to load it into the plugin. If it's not valid JSON the on parse error condition will trigger, otherwise your ready to go.

    If you know the path of the "file" you want you can just get the data using the JSON.Get expression. You can make a path by joining the names of the "folders" with a full stop. As arrays don't have names for their contents you can use a number like so "data.enemies.3.name".

    It can get a bit tedious writing the full path all the time, so you can move your current location using the SetPath action. Now if you start your path with a full stop it will be relative to the current location. Paths that don't start with a full stop will be absolute paths and ignore the current location.

    Working with arrays you might not know how many entries there are, so you can use the ArraySize expression to find out. Also there is a foreachcondition that loops over "folders". Inside the loop you can use the CurrentKey expression to get the label and CurrentValue to get the value.

    I'm not going to cover it here but the plugin also has methods of creating and modifying JSON data.

    Putting this together

    Say you have a series of named dialogs, each having a title and a few pages of text you could have something like:

    {
     "welcome": {
     "dialog-title": "Welcome",
     "pages": [
     "Hello new player",
     "welcome to my game",
     "I hope you enjoy it"
     ]
     },
     "gameover": {
     "dialog-title": "Game Over",
     "pages": [
     "You seemed to have died",
     "Maybe you should try again?",
     "Or you know, go look at a tree or something"
     ]
     }
     }
    

    Then in you can have an function for "show dialog" that just takes the name of the dialog and do:

    System | Set DialogName to Function.Param(0)

    JSON | Set path to DialogName

    Title | Set text to JSON.Get(".dialog-title")

    JSON | Set path to ".pages"

    // some magic here to increment through the pages

    System | set PageCount to JSON.ArraySize(".")

    Content | Set text to JSON.Get("." & PageNumber)

  • Can't you just keep the first line of each colunm to name it? The same for the lines, meaning each Y of your array is a dialog, and again, you can save the first colunm to name it. Scan the array and it's done.

  • Hi guys thanks for all the replies!

    I think i found a solution using Arrays, seems i was kinda stupid plus even though i used Construct intensely for a while now, still learning ;) Will definitely keep an eye on JSON to learn further down the line but i think i found a way of doing it with Arrays. Now if only there was a way to import my CSV to the Array editor :D

    EDIT: Actually, this is slightly off-topic, but i got another question regarding the Array Editor:

    Since i'm having all my dialogue proofread and everything, to send it to an external translator it would of course be much more convenient to be able to export this JSON as a CSV, so, same story again :D Anyone having any experience with a good tool to convert the Array JSON to CSV and back?

  • joelmayer importing/exporting array data to CSV is probably a bit awkward, as array data is always 3D but CSV is only 2D. I tried passing an empty 10x10x10 array file through an online json > csv converter and ended up with 30 values instead of 1000.

    It should be possible to convert a "layer" of an array to CSV fairly painlessly, but I'm not aware of any tools that do it. If there's enough interest I would consider adding the ability to the array editor itself for import/export. As MS excel supports CSV it could be handy in some situations. However, CSV isn't really standardised, which is a bit of pain.

  • Hi Nepeo

    That would be incredible!! It would basically substitute for the CSV plugin by rex that i used up until now and of course, the very convenient array editor would be great to use in the engine itself with just the ability to export and import stuff from it (preferably from a CSV).

    I think sticking to just the most widespread standard would suffice and as long as you can open and edit it in MS Excel or Google Spreadsheets i think everybody would be happy :)

    Thanks for considering it!

    Edit: Also just an idea, i really don't know if it would be easier/harder to implement than a full-blown csv export/import function: i just found out that i can copy and paste cells between Google Spreadsheet and Microsoft Excel and vice-versa. Maybe, if it is easier to do, it would be cool if you could select multiple cells in the Array Editor and just copy paste them over to a Spreadsheet program as well as the other way around... ? Just a thought ;)

  • TL;DR - the data editor will support ranged cut/copy/paste in r123. Compatible with G Sheets and MS Excel(probably)

    The copy and paste was a good suggestion joelmayer As ranged cut/copy/paste is something that has been requested a fair few times for the data editor that would cover 2 bases, and avoids writing special import/export system.

    I've done some experimenting and matched the format that google sheets uses, so in the next release it should be possible to copy/paste between the 2. If pasting from excel into sheets works then in theory it the same format should also work for excel. But I can't be 100% sure ( I don't have excel on my development machine to test ).

    If your interested sheets uses a slightly awkward variation on the TSV ( tab separated values ) format. There's some very special treatment of double quotes, which has been a pain to handle.

    Hopefully this should cover your bases for converting to/from CSV files!

  • Oh wow thank you so much Nepeo, i look forward to trying this out!! :D

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi Nepeo

    Thank you so much for the implementation of the Copy pasting from and to the Array Editor :) I'm happy to report that it works with Microsoft Excel as well (at least on my Mac, which is kind of ironic :D Wasn't able to test it on a Windows machine).

    I'd have a couple of improvement suggestions for the future maybe:

    - This one i actually expected: If i copy and paste an area from my spreadsheet software to the data editor, it would be awesome, if the editor resized to the pasted data area. Meaning, if i have a 3x3 cells area in the C3 editor and my copied data from the spreadsheet is 4x2 it would widen the data editor to 4x3 (only for expanding the editor it wouldn't have to shrink it down. Hope that makes sense.

    - Right now i don't seem to be able to delete multiple selected cells at once. When i select an area with the shift key and then hit delete with the selected cells, it only deletes the active one, not the whole selection area, which would be the expected behaviour.

    - When i copy and paste an empty area from a spreadsheet over to the data editor (with multiple rows), it only pastes the first row and doesn't overwrite anything underneath it. Meaning, if i got a filled up 5x5 cells sheet in the data editor and i copy and paste a 5x5 empty cells area over it, it only overwrites the first row with empty cells.

    Thank you though for a great initial (and extremely fast) implementation for this request!

    Best,

    Joel

  • Hey no worries, thanks for the speedy feedback! This release seems to be getting a lot of love, which is nice 😎

    1. I had kinda pondered on this one. There's a few odd cases and things around this behaviour which I didn't want to make a hasty decision over. For instance: if you do a select all copy paste the data set is truly massive, because excel and sheets both start with very large data sets. I presume they ignore the empty boxes in their save format, but we can't do that with JSON unfortunately... So there's a risk of encouraging very large files etc.

    We could perhaps have a resize warning if it exceeds the size of the editor? Might be the best option.

    2. Yup... I totally forgot that! I will implement it for the next release, in the mean time you could probably abuse cut for this.

    3. I'm not seeing that with sheets, have you tried excel and sheets in that situation? Might be that they have a slightly different behaviour...

    There is one other behaviour I noticed that I hadn't implemented, because it seemed complicated and not particularly useful. In sheets if you copy say a 3x3, then select a larger area and paste it will tile that data. Can you see that ever being useful? Not sure if I should keep that on my radar or forget about it.

  • Hi Nepeo

    Thanks for your feedback:

    About 1. , it's really more a nice to have feature than anything else and i guess, since this a niche request anyway your resources are probably better used elsewhere :) The fact that i can copy and paste in any way is already a huge plus.

    3. You're right, i just tried with Sheets and there it works but for some reason it doesn't with Excel, which is really weird. I downloaded and tried LibreOffice as well now and it seems there it doesn't reckognize empty cells at all. Copy pasting cells with content though works :) Don't know what's up with the empty ones in either Excel or Libre Calc...

    Thanks!

    Edit: Sorry overlooked the last part: i think the tiling is probably an overkill and i personally wouldn't see any use for it.

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