Arrays to / from scripts.

1 favourites
  • 9 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Is there a simple way, or what is the best way, to pass array information between C3 and a JS script? If it's a full array or lots of data, will I need to pack / unpack it, with JSON or something?

  • What kind of operations you need to do with your array data ? If it's nothing fancy, you might opt to not pass anything between JS and C3, and rely entirely on C3 Arrays and its API.

    If not however, I believe JSON is your only recourse.

  • I'm re-writing a complex game (Soccer management). I love C3 for what it does, I set up a little turret game in about 3 minutes before I even knew how the turret behaviour worked. That woulda taken my a month in Unity (mainly because I'm useless at Unity and I can't get anything to work).

    But then I'm also very old-school and started programming in the 80's. I love Javascript. So for my large/complex game, my plan was to write it almost entirely in Javascript but just use C3 for what C3 is really good at, i.e everything on the screen. So C3 = screen, JS = guts.

    Since the JS would have lots of information that it needs to pass to C3 to display, I need to know if I can pass large amounts at once. If there's no built in way, perhaps I'd convert it to JSON or a string and then have a C3 function to reconstruct it in C3?

    I realise that C3 doesn't sound like the ideal engine for a screen packed with numbers and text but it works really well for the graphical elements. Plus I just prefer it in general to Unity or Godot and it'll deploy directly to a webpage.

    And another addition, I've done a fair amount already using C3 arrays but I really don't like the way they work. If the stuff gets too complex, it starts to feel like I'm wrestling with C3 rather than coding.

  • And another addition, I've done a fair amount already using C3 arrays but I really don't like the way they work. If the stuff gets too complex, it starts to feel like I'm wrestling with C3 rather than coding.

    I agree, that's why I prefer using object instances instead of arrays. A bunch of instances with instance variables basically represent a multi-dimensional array, and it's much easier to work with it.

    For example, if you need an array of football players, create 11 sprite instances for each team (they may be invisible or off-screen). You can define many variables (name, score, salary etc), and then easily do whatever you want with events - pick any player or a group of players, filter them by many parameters, make bulk changes, find min/max values, sort, remove etc.

    If you still prefer to work with Javascript array, you can return its data back to C3 events as a JSON string, but I believe it will not be compatible with C3 Array object. You can load it into JSON object though. Another option is to return a comma-separated string, which you can then parse with tokenat/tokencount expressions, or use CSV addon to convert to array. Or create a function in the script which will update C3 array with the data from your JS array:

    construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/plugin-interfaces/array

  • I gave this a try to see how it would work... it does, but it seems a little bit clunky to pass things between C3 arrays and JS arrays.

    https://www.dropbox.com/s/i6kezzhia0g57do/ArraysToAndFromScripts.c3p?dl=0

    This is a very contrived example, it only prints to console, you'll need to bring up dev tools to view the results.

  • dop2000, yep, that's exactly how I was doing things at first. As you say, it's actually very clean and easy to work with that way. I didn't even need a sprite, I had an array as each player and then the array can itself have attached variables for easy reading (salary etc) and the array can be used for any junk numbers. I suspect that arrays might perform a bit better.

    Another real advantage is that it's super easy to find, for example, all the players in one team just by picking against the 'Team' variable.

    My worry was that I really need a league with 64 teams and ~50 players per team, so that's 3200 objects. C3 doesn't really seem to mind that but I'm worried that down the line it might get a bit heavy.

    But now I think about it, I can't really remember why I decided against this approach. I had a few screens half working but abandoned it (in C2) because it wasn't looking very good. Then recently I saw this JS thing and realised that I really like this idea. I think I just like raw coding when it comes to complex arrays and loads of calculations. But now you mention it, maybe I should give it another crack this way in C3 and see how it handles it. Maybe 3200 objects won't bother C3 at all.

    Diego, thanks, I'll take a look through that and see how it works out. Either way, I like experimenting and seeing how things work. I might try to go raw C3 or I might go mostly JS, need to play around and see what works out best.

    Thanks guys.

  • Another thing you might want to keep up your sleeve, the C3 JSON object can parse and stringify complex Javascript objects. I don't know about its performance in terms of speed, but it shouldn't be that much of an issue if you limit your back and forth between JS and C3.

    Here's a basic example : drive.google.com/open

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That's true, performance doesn't matter in a management game (well not much unless it's realllllly bad). I'll look into all the approaches but somehow I always feel like something isn't right and end up starting from scratch. With Construct, I always felt like it was a struggle doing complex calculations with large numbers of objects / array so I thought JS would solve that. It still might be the way forward, I'll try both approaches and see what works best.

    If I can copy large arrays back and forth, that might help. But thinking about it, I should be ok just grabbing one rating at a time anyway.

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