[SOLVED]How do I push an object to array using JSON?

0 favourites
  • 6 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hi all,

    I'm trying to push an object like:

    	{name: "John", age: 35, hobbies:["music","books","videogames"]}
    

    to selected path which is an array, I couldn't find a way to push objects in C3, I do not want to push simple values like only string or numbers without an object. There is an action "Push value" but I tried it and it won't work according this screenshot:

    i.imgur.com/Sr8KosN.png

    I clicked "Ok" button to push several times and it won't add another index of this value.

    Tagged:

  • Hi Joannesalfa, as you've discovered the "PushValue" action can only push numbers or strings. But what you want to do is possible. If you take a look at the description for the "SetArray" action you will see that it can also resize an existing array. So you can "push" a complex value by resizing the array to the array_size + 1, then setting the index of old_array_size to the complex object. You can either compose the object using events or use the SetJSON action to create the object ( SetJSON allows you to parse a JSON string and insert it at a given index ).

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Nepeo Thanks for your solution, it seems a rare way to implement it. Anyway it's solved now.

  • Nepeo Is there a way to make it persistent? I notice it does reset since is not saving new objects when I go to different layout.

  • Nepeo Thanks for your solution, it seems a rare way to implement it. Anyway it's solved now.

    Well it's similar to how push would be implemented at a programming language level. It would be nice to support more types in the push action, but as the expression system only supports strings and numbers that becomes much harder.

    When designing the JSON plugin we did have a prototype that separated the creation of values from inserting them. While this worked and allowed performing push/pop with complex types it was confusing to use and was awkward to implement so it was ultimately scrapped in favour of a simpler system.

    With the current system we could plausibly implement additional actions for pushing other types ( PushObject, PushArray, PushBoolean, PushNull, PushJSON ), I'll review how feasible that is.

    Nepeo Is there a way to make it persistent? I notice it does reset since is not saving new objects when I go to different layout.

    The instances of the JSON plugin are not modified or deleted when the layout is changed, so it is already persistent. I checked with the debugger and you can see the instances persisting between layout changes, perhaps you are referencing the wrong instance of the plugin?

  • Nepeo Oh I see, I think you could base "Function" plugin where you can add parameters could be similiar to adding key and value not sure how it can adapt an array or recursive array.

    Nevermind about the problem since Array is set as 0 which it forces to reset values, so I got rid of it from method function.

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